[英]Rank with group
我的table1是这样的
Dept Class Dept1 Class1 date
NULL NULL a history 02-2020
NULL NULL a bio 02-2020
a math NULL NULL 02-2020
a chemi NULL NULL 02-2020
a history NULL NULL 02-2020
b PE NULL NULL 03-2020
b Music NULL NULL 03-2020
b Sport NULL NULL 03-2020
NULL NULL b Cook 03-2020
c Psy NULL NULL 04-2020
我们可以查询看起来像这样的吗
Dept Class Dept1 Class1 date
a math a history 02-2020
a chemi a bio 02-2020
a history NULL NULL 02-2020
NULL NULL NULL NULL 02-2020
NULL NULL NULL NULL 02-2020
b PE b Cook 03-2020
b Music NULL NULL 03-2020
b Sport NULL NULL 03-2020
NULL NULL NULL NULL 03-2020
c Psy NULL NULL 04-2020
目标尝试按 Dept、Dept1 和日期获取记录组
您可以使用 nvarchar 或整数的任何字段
我的查询并不能正常工作
select coalesce(sm.dept, su.dept1) as dept,
sm.class,
su.class1,
sm.date
from
(select distinct w.*,
row_number() over (partition by dept order by [date] asc) as seqnum
from table1 w
) sm
full join
(select w.*,
row_number() over (partition by dept1 order by [date] asc) as seqnum
from table1 w
) su
on sm.dept = su.dept1 and sm.seqnum = su.seqnum
是的。 . . 您似乎希望“组”中的列按日期对齐。 这是一种方法:
select date, max(dept), max(class), max(dept1), max(class1)
from ((select date, row_number() over (order by dept) as seqnum,
dept, null as class, null as dept1, null as class1
from t
where dept is not null
) union all
(select date, row_number() over (order by class) as seqnum,
null as dept, class, null as dept1, null as class1
from t
where class is not null
) union all
(select date, row_number() over (order by dept1) as seqnum,
null, null as class, dept1, null as class1
from t
where dept1 is not null
) union all
(select date, row_number() over (order by class1) as seqnum,
null as dept, null as class, null as dept1, class1
from t
where class1 is not null
)
) x
group by date, seqnum;
我了解您想按日期订购,然后按部门、class、部门1、类1 列订购。 在 SQL 服务器中,默认情况下 NULL 出现在顶部。 因此,我将 NULL 值替换为 ZZZZZ 以使它们最终出现。 您可以将 ZZZZZ 替换为大的 varchar 字段,该字段不会出现在您的部门、class、dept1、class1 字段中。
DECLARE @dept table(dept varchar(50),class varchar(50),dept1 varchar(50),class1 varchar(50),datev varchar(50))
insert into @dept VALUES
(NULL ,NULL ,'a' ,'history' ,'02-2020')
,(NULL ,NULL ,'a' , 'bio' ,'02-2020')
,('a' ,'math' ,NULL , NULL ,'02-2020')
,('a' ,'chemi' ,NULL , NULL ,'02-2020')
,(null ,NULL ,NULL , NULL ,'02-2020')
,(null ,NULL ,NULL , NULL ,'02-2020')
,('a' ,'history' ,NULL , NULL ,'02-2020')
,('b' ,'PE' ,NULL , NULL ,'03-2020')
,('b' ,'Music' ,NULL , NULL ,'03-2020')
,('b' ,'Sport' ,NULL , NULL ,'03-2020')
,(NULL ,NULL ,'b' ,'Cook' ,'03-2020')
,(null ,NULL ,NULL , NULL ,'03-2020')
,('c' ,'Psy' ,NULL ,NULL ,'04-2020');
SELECT DEPT, class, dept1, class1, datev
from
(
SELECT *,ROW_NUMBER() over(order by datev,isnull(dept,'ZZZZZ'),isnull(class,'ZZZZZ'),isnull(dept1,'ZZZZZ'),isnull(class1,'ZZZZZ') ) as rnk FROM @dept
) AS T
order by rnk
+------+---------+-------+---------+---------+
| DEPT | class | dept1 | class1 | datev |
+------+---------+-------+---------+---------+
| a | chemi | NULL | NULL | 02-2020 |
| a | history | NULL | NULL | 02-2020 |
| a | math | NULL | NULL | 02-2020 |
| NULL | NULL | a | bio | 02-2020 |
| NULL | NULL | a | history | 02-2020 |
| NULL | NULL | NULL | NULL | 02-2020 |
| NULL | NULL | NULL | NULL | 02-2020 |
| b | Music | NULL | NULL | 03-2020 |
| b | PE | NULL | NULL | 03-2020 |
| b | Sport | NULL | NULL | 03-2020 |
| NULL | NULL | b | Cook | 03-2020 |
| NULL | NULL | NULL | NULL | 03-2020 |
| c | Psy | NULL | NULL | 04-2020 |
+------+---------+-------+---------+---------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.