T-SQL |将分组聚合汇总为字符串

时间:2016-02-01 21:49:22

标签: sql sql-server sql-server-2012

代码:

CREATE TABLE #Tab1 (Tag1 varchar(5), Tag2 varchar(5), Tag3 varchar(5), InvalidFlag bit, JunkFlag bit, Old int, New int, Combined int)
INSERT INTO #Tab1
SELECT 'NK','DJ','Name1',0,0,5,2,7
UNION
SELECT 'NK','IJ','Name2',0,0,4,2,6
UNION
SELECT 'NK','KJ','Name3',0,0,4,1,5
UNION
SELECT 'NK','KJ','Name3',0,1,0,1,1
UNION
SELECT 'PK','EK','Name4',0,0,2,2,4
UNION ALl
SELECT 'PK','EK','Name4',1,0,1,0,1

查询

SELECT * FROM #Tab1

输出

Tag1    Tag2    Tag3    InvalidFlag JunkFlag    Old New Combined
NK      DJ      Name1   0           0           5   2   7
NK      IJ      Name2   0           0           4   2   6
NK      KJ      Name3   0           0           4   1   5
NK      KJ      Name3   0           1           0   1   1
PK      EK      Name4   0           0           2   2   4
PK      EK      Name4   1           0           1   0   1

期望的输出

Tag1    Tag2    Tag3    Old                                     New
NK      DJ      Name1   | Valid - 5 |Invalid - 0 | Junk - 0     | Valid - 2 |Invalid - 0 | Junk - 0
NK      IJ      Name2   | Valid - 4 |Invalid - 0 | Junk - 0     | Valid - 2 |Invalid - 0 | Junk - 0
NK      KJ      Name3   | Valid - 4 |Invalid - 0 | Junk - 1     | Valid - 1 |Invalid - 0 | Junk - 1
PK      EK      Name4   | Valid - 2 |Invalid - 1 | Junk - 0     | Valid - 2 |Invalid - 0 | Junk - 0

目标

我想基于Tag1,Tag2和Tag3对记录进行分组,并根据InvalidFlag和JunkFlag聚合旧记录和新记录。即对于第一个记录,对于旧字段,如果InvalidFlag = 0并且如果JunkFlag = 0那么旧(输出)字段=有效 - 5 |无效 - 0(因为InvalidFlag = 1的相同分组的Tag1,Tag2和Tag3记录没有其他记录)垃圾 - 0(因为RejectedFlag = 1的同一组Tag1,Tag2和Tag3记录没有其他记录)

我的尝试到目前为止

SELECT DISTINCT Tag1, Tag2, Tag3
, '| Valid - ' + CASE WHEN InvalidFlag = 0 AND JunkFlag = 0 THEN ISNULL(CAST(SUM(Old) OVER(Partition by Tag1, Tag2, Tag3, InvalidFlag, JunkFlag) AS varchar(10)),'0') END
+ ' | Invalid - ' + CASE WHEN InvalidFlag = 1 THEN ISNULL(CAST(SUM(Old) OVER(Partition by Tag1, Tag2, Tag3, InvalidFlag, JunkFlag) AS varchar(10)),'0') END
+ ' | Junk - ' + CASE  WHEN JunkFlag = 1 THEN ISNULL(CAST(SUM(Old) OVER(Partition by Tag1, Tag2, Tag3, InvalidFlag, JunkFlag) AS varchar(10)),'0') END
AS Old
, '| Valid - ' + CASE WHEN InvalidFlag = 0 AND JunkFlag = 0 THEN ISNULL(CAST(SUM(New) OVER(Partition by Tag1, Tag2, Tag3, InvalidFlag, JunkFlag) AS varchar(10)),'0') END
+ ' | Invalid - ' + CASE WHEN InvalidFlag = 1 THEN ISNULL(CAST(SUM(New) OVER(Partition by Tag1, Tag2, Tag3, InvalidFlag, JunkFlag) AS varchar(10)),'0') END
+ ' | Junk - ' + CASE  WHEN JunkFlag = 1 THEN ISNULL(CAST(SUM(New) OVER(Partition by Tag1, Tag2, Tag3, InvalidFlag, JunkFlag) AS varchar(10)),'0') END
AS New
FROM #Tab1
GROUP BY Tag1, Tag2, Tag3

2 个答案:

答案 0 :(得分:1)

这样的东西有用,我会留给你把整数组合成文本,我认为显着的部分是使用case和sum(它叫做条件聚合),它看起来像这样:

SELECT
    tag1
    ,tag2 
    ,tag3
    ,SUM(CASE WHEN invalidflag = 0 and junkFlag = 0 then old else 0 end) as oldvalid
    ,SUM(CASE WHEN invalidflag = 1 and junkFlag = 0 then old else 0 end) as oldvinvalid 
    ,SUM(CASE WHEN invalidflag = 0 and junkflag = 1 then old else 0 end) as oldjunk

    ,SUM(CASE WHEN invalidflag = 0 and junkFlag = 0 then new else 0 end) as newvalid
    ,SUM(CASE WHEN invalidflag = 1 and junkFlag = 0 then new else 0 end) as newinvalid 
    ,SUM(CASE WHEN invalidflag = 0 and junkflag = 1 then new else 0 end) as newjunk
FROM 
    #tab1
GROUP BY 
    tag1, tag2, tag3

答案 1 :(得分:1)

我很难确切地说出你正在尝试做什么,但使用SUMMAXCONCAT(以节省一些铸造和空检查似乎非常接近:

SELECT Tag1, Tag2, Tag3
    ,CONCAT('| Valid - ', MAX(Old), ' | Invalid - ', SUM(Cast(InvalidFlag AS INT)), ' | Junk - ', SUM(Cast(JunkFlag AS INT))) as Old
    ,CONCAT('| Valid - ', MAX(New), ' | Invalid - ', SUM(Cast(InvalidFlag AS INT)), ' | Junk - ', SUM(Cast(JunkFlag AS INT))) as New
FROM #Tab1
GROUP BY Tag1, Tag2, Tag3