[英]Self-referential join, multiple rows
我有一个tbl:
| user | crs | status |
| usr1 | crs1 | stat1 |
| user1 | crs1 | stat2 |
| user2 | crs2 | stat1 |
我怎样才能得到:
user | crs1.status | crs2.status?
我正在努力:
select distinct
mstr.[person username],
abt.[module name],
abt.[Lesson completion status],
eoc.[module name],
eoc.[Lesson completion status]
from
[aht16] as mstr
right join
[aht16] as abt on (mstr.[person username] = abt.[person username]
and abt.[module name] = 'sdfsdfsdfsfd)'
and abt.[Lesson completion status] = 'completed')
right join
[aht16] as EOC on (mstr.[person username] = EOC.[person username]
and eoc.[module name] = 'sdfsdfsdfs'
and eoc.[Lesson completion status] = 'completed')
where
mstr.[course title] = 'sdf'
一种方法是条件聚合:
select user,
max(case when crs = 'crs1' then status end) as crs1_status,
max(case when crs = 'crs2' then status end) as crs2_status
from aht16 as mstr
group by user;
注意:这使用表中描述的列名。 该查询使用了一组不同的名称。
简单易用!
我以为自我加入会奏效,但只会增加行数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.