Redian新闻
>
有趣的Join问题,源于SQL和SAS比较。
avatar
有趣的Join问题,源于SQL和SAS比较。# Database - 数据库
n*6
1
tableA
id name salary06 ...
1 mike 1000
2 john 2000
4 tom 3000
tableB
id name income06 income07 ...
1 mike 1000 ...
2 john 3000
3 smith 3000
5 Leo 5000
Objective:
- Combine all the records from two tables into tableC(id, name, income)
- For a specific id, if B.income exists, use that for C.income.
- For a specific id, If B.income does not exist, use A.salary for C.income
Expected outcome tableC
id name income06 income07 ...
1 mike 1000 ...
2 john 3000
3 smith
avatar
B*g
2
I don't believe SAS invent merge.

【在 n********6 的大作中提到】
: tableA
: id name salary06 ...
: 1 mike 1000
: 2 john 2000
: 4 tom 3000
: tableB
: id name income06 income07 ...
: 1 mike 1000 ...
: 2 john 3000
: 3 smith 3000

avatar
j*n
3
Here is the core part in T-SQL, you finish the rest...
SELECT C.id, C.name, A.salary, B.income
FROM (
SELECT id, name
FROM tableA
UNION
SELECT id, name
FROM tableB
) C
LEFT JOIN tableA ON C.id = A.id
LEFT JOIN tableB ON C.id = B.id

【在 n********6 的大作中提到】
: tableA
: id name salary06 ...
: 1 mike 1000
: 2 john 2000
: 4 tom 3000
: tableB
: id name income06 income07 ...
: 1 mike 1000 ...
: 2 john 3000
: 3 smith 3000

avatar
B*g
4
你需要升级了

【在 j*****n 的大作中提到】
: Here is the core part in T-SQL, you finish the rest...
: SELECT C.id, C.name, A.salary, B.income
: FROM (
: SELECT id, name
: FROM tableA
: UNION
: SELECT id, name
: FROM tableB
: ) C
: LEFT JOIN tableA ON C.id = A.id

avatar
j*n
5
CTE SAS 用不了吧?

【在 B*****g 的大作中提到】
: 你需要升级了
avatar
B*g
6
google "sql server 2008 merge"

【在 j*****n 的大作中提到】
: CTE SAS 用不了吧?
avatar
j*n
7
Damn, i just need to retire....

【在 B*****g 的大作中提到】
: google "sql server 2008 merge"
avatar
o*5
8
Here is the Oracle code:
create table c
as
select nvl(a.id,b.id) id,nvl(a.name,b.name) name,nvl(b.income06,a.salary06)
income06
from a full outer join b
on a.id=b.id
order by 1;
I do not know whether you can use PROC SQL to achieve this same thing or not
.
avatar
B*g
9
oracle merge

)
not

【在 o******5 的大作中提到】
: Here is the Oracle code:
: create table c
: as
: select nvl(a.id,b.id) id,nvl(a.name,b.name) name,nvl(b.income06,a.salary06)
: income06
: from a full outer join b
: on a.id=b.id
: order by 1;
: I do not know whether you can use PROC SQL to achieve this same thing or not
: .

avatar
o*5
10
I gave it a little more thought on this. In fact, you can use PROC SQL to do
this.
proc sql;
create table c
as
select coalese(a.id,b.id) as id,coalese(a.name,b.name) as name,coalese(b.
income06,a.salary06) as income06
from a full outer join b
on a.id=b.id
order by 1;
quit;
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。