由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - SQL copy a table into a new table and add a new column (转载)
相关主题
sort two same tables SQL but different results (转载)如何在PROC SQL里面象SQL 一样设置index使join更快?
A R questionSQL check likeness between two large tables
SQL combine two tables into one table and add a new column (转载)急需高人指点!!
[合集] 如何用SAS把几个colume的值连接在一起问个PROC SQL中INNER JOIN的问题
[合集] 请教:SAS helpMySQL语句请教 (转载)
如何将SAS DATA中的变量名改名(不知道原变量名的前提下)问SAS code怎么写
给duplicate加flagSAS新手问一个做很多次比较的问题
操作pair data加flag一个sas问题
相关话题的讨论汇总
话题: table1话题: table话题: new话题: table2话题: col4
进入Statistics版参与讨论
1 (共1页)
l******9
发帖数: 579
1
【 以下文字转载自 Database 讨论区 】
发信人: light009 (light009), 信区: Database
标 题: SQL copy a table into a new table and add a new column
发信站: BBS 未名空间站 (Fri May 23 12:05:22 2014, 美东)
need to copy a table into a new table on SQL server 2008. Also, add a new
column into the new table.
The values of the new column depends on the compare result between the new
table and another table.
Example,
Table1:
col1 col2 col3
abc 346 6546
hth 549 974
Table1_new:
col1 col2 col3 col4
abc 346 6546 1
hth 549 974 0
Table2:
col1
abc
sfsdf
If Table2's col1 appear in Table1 col1, mark col4 as 1 in Table1_new, else
mark as 0.
The code does not work
SELECT *,
(
SELECT 1 as col4
FROM Table2 as a
INNER JOIN Table1 as b
on b.col1 = a.col1
SELECT 0 as col4
FROM Table2 as a
INNER JOIN Table1 as b
on b.col1 <> a.col1 # I do not know how to do this !!!
)
INTO table1_new
FROM table1
Any help would be appreciated.
l****u
发帖数: 529
2
select table1.*, case when table1.col1=table2.col1 then 1 else 0 end as col4
from table1 left join table2 on table1.col1=table2.col1;
1 (共1页)
进入Statistics版参与讨论
相关主题
一个sas问题[合集] 请教:SAS help
为什么结果是这样?如何将SAS DATA中的变量名改名(不知道原变量名的前提下)
SQL中啥时候用group by, 啥时候用self-join?给duplicate加flag
请问一个小问题,现在需要重复拷贝相同数据到同一文件内,如何把数据重复100遍写到文本文件。操作pair data加flag
sort two same tables SQL but different results (转载)如何在PROC SQL里面象SQL 一样设置index使join更快?
A R questionSQL check likeness between two large tables
SQL combine two tables into one table and add a new column (转载)急需高人指点!!
[合集] 如何用SAS把几个colume的值连接在一起问个PROC SQL中INNER JOIN的问题
相关话题的讨论汇总
话题: table1话题: table话题: new话题: table2话题: col4