22,181
社区成员




select a.sj,a.cl,b.cl
from a join b on a.sj=b.sj
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([type] varchar(1),[sj] datetime,[cl] int)
insert [tb]
select 'A','2009-01-03',50 union all
select 'A','2009-01-05',50 union all
select 'A','2009-01-07',80 union all
select 'A','2009-01-08',50 union all
select 'A','2009-01-09',10 union all
select 'B','2009-01-03',50 union all
select 'B','2009-01-05',50 union all
select 'B','2009-01-07',80 union all
select 'B','2009-01-08',50 union all
select 'B','2009-01-09',10
---查询---
select
distinct
sj,
(select cl from tb where sj=t.sj and [type]='A') as [A.cl],
(select cl from tb where sj=t.sj and [type]='B') as [B.cl]
from tb t
---结果---
sj A.cl B.cl
------------------------------------------------------ ----------- -----------
2009-01-03 00:00:00.000 50 50
2009-01-05 00:00:00.000 50 50
2009-01-07 00:00:00.000 80 80
2009-01-08 00:00:00.000 50 50
2009-01-09 00:00:00.000 10 10
(所影响的行数为 5 行)