Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 对不同事件的代码排序_Sql_Sql Server_Tsql_Sql Server 2012 - Fatal编程技术网

Sql 对不同事件的代码排序

Sql 对不同事件的代码排序,sql,sql-server,tsql,sql-server-2012,Sql,Sql Server,Tsql,Sql Server 2012,我正在努力完成以下工作。这里是我所说的一小段数据。我们有一个人可以在不同的日期使用不同的代码 DECLARE @t TABLE (PersonId INT, CDate DATE, Code VARCHAR(20)) INSERT INTO @t VALUES (2345,'20161010','V1'),(2345,'20170104','V2'),(2345,'20170320','V3'),(2345,'20170612','V4'), (2421,'20161005','UNS'),(

我正在努力完成以下工作。这里是我所说的一小段数据。我们有一个人可以在不同的日期使用不同的代码

DECLARE @t TABLE (PersonId INT, CDate DATE, Code VARCHAR(20))
INSERT INTO @t VALUES 
(2345,'20161010','V1'),(2345,'20170104','V2'),(2345,'20170320','V3'),(2345,'20170612','V4'),
(2421,'20161005','UNS'),(2421,'20161102','V1'),(2421,'20170118','V2'),(2421,'20170418','V3'),
(2421,'20170712','V4'),(2421,'20171004','V5'),(2421,'20171220','V6'),(2421,'20180113','LFM'),
(2421,'20180321','V7'),(2421,'20180822','V8'),(2421,'20190220','V9'),(2421,'20190315','EOT'),
(2430,'20161020','V1'),(2430,'20170116','V2'),(2430,'20170413','V3'),(2430,'20170726','EOT'),
(3004,'20161110','V1'),(3004,'20170131','V2'),(3004,'20170425','V3'),(3004,'20170503','LFM'),
(3004,'20170512','LFM'),(3004,'20170718','V4'),(3004,'20170725','LFM'),(3004,'20171010','V5'),
(3004,'20180102','V6'),(3004,'20180108','LFM'),(3004,'20180115','LFM'),(3004,'20180125','UNS'),
(3004,'20180328','V7'),(3004,'20180406','LFM'),(3004,'20180911','V8'),(3004,'20190227','V9'),
(3004,'20190306','LFM'),(3004,'20190313','UNS'),
(31740,'20190514','V1')
我们正在寻找的结果如下。我在右边添加了PersonId作为额外信息,这样你就可以看到哪些应该被计算。最终结果中的代码应按照所有不同人员的CDate顺序排列。代码EOT应该是最后一个

RowNr   Code    Total   |
========================|
1       UNS     1       |   2421
2       V1      5       |   2345,2421,2430,3004,31740
3       V2      4       |   2345,2421,2430,3004
4       V3      4       |   2345,2421,2430,3004
5       LFM     1       |   3004
6       LFM     1       |   3004
7       V4      3       |   2345,2421,3004
8       LFM     1       |   3004 
9       V5      2       |   2421,3004
10      V6      2       |   2421,3004
11      LFM     2       |   2421,3004
12      LFM     1       |   3004
13      UNS     1       |   3004
14      V7      2       |   2421,3004
15      LFM     1       |   3004
16      V8      2       |   2421,3004
17      V9      2       |   2421,3004
18      LFM     1       |   3004
19      UNS     1       |   3004
20      EOT     2       |   2421,2430

有谁能给我们一个起点或帮助我们实现这一目标

您可以在2012版的
stuff()函数中使用
xml路径
结构

select
       row_number() over( order by code) RowNr,
       code, count(*) as total,
       stuff ((select ',' +  cast(p2.PersonId as varchar)
                 from
                 (
                  select code, PersonId,
                     row_number() over(partition by PersonId, code order by code) rn,
                     row_number() over(partition by PersonId order by code) rnk   
                    from @t
                  ) p2 
          where p2.code = p1.code
            and p2.rn = 1
          group by p2.PersonId, rn, rnk
          order by rnk
          for xml path('') ), 1,1,'') as comma_sep_string
from @t p1
group by code;


但是请注意,示例数据和所需结果不兼容。

为什么第1、13和19行在预期结果中没有组合在一起,为什么第2行中个人ID的组合在一起?您需要SQL Server 2018中提供的
listag()/GROUP_CONCAT()
功能。或者,您也可以使用
XML\uu
操作,但这很难看。@Deepshikha:row 2:所有人都有V1;第1行:此UNS引用的是V1之前的UNS代码,此情况仅发生在2421人身上;第19行:此UNS位于V9之后,仅适用于人员3004;你需要更详细地解释你是如何分组的。我尝试了几个“逻辑”选项,但不断得到不同的组。嗨,Barbaros,结果实际上是我们想要的输出。更多细节。第2行:所有人都有一个V1;第1行:此UNS引用的是V1之前的UNS代码,此情况仅发生在2421人身上;第19行:此UNS位于V9之后,仅适用于人员3004;