以下查询给出了我想要的结果,除了一个小问题。它以正确的顺序正确列出了所有内容,但我想将faccount
组中“实体”相同的行组合在一起,并且正在寻找有关如何执行此操作的建议。作为一个例子,我将得到
ROW1: 3010 (FACCOUNT) clienta (Entity) 0 0 3 0 0 0 0 0 ....
ROW2: 3010 (FACCOUNT) clienta (Entity) 0 2 0 0 0 0 0 0 ....
理想情况下,我想为同一个实体添加一行
ROW1: 3010 (FACCOUNT) clienta (Entity) 0 2 3 0 0 0 0 0 ....
感谢任何想法
SELECT CATEGORY,
ENTITY,
FACCOUNT,
SIGNEDDATA,
TIMEID,
SUM(CASE WHEN ABS(SIGNEDDATA) = '1' THEN 1 ELSE 0 END) AS "r1",
SUM(CASE WHEN ABS(SIGNEDDATA) = '2' THEN 1 ELSE 0 END) AS "r2",
SUM(CASE WHEN ABS(SIGNEDDATA) = '3' THEN 1 ELSE 0 END) AS "r4",
SUM(CASE WHEN ABS(SIGNEDDATA) = '4' THEN 1 ELSE 0 END) AS "r5",
SUM(CASE WHEN ABS(SIGNEDDATA) = '5' THEN 1 ELSE 0 END) AS "r6",
SUM(CASE WHEN ABS(SIGNEDDATA) = '6' THEN 1 ELSE 0 END) AS "r7",
SUM(CASE WHEN ABS(SIGNEDDATA) = '7' THEN 1 ELSE 0 END) AS "r8",
SUM(CASE WHEN ABS(SIGNEDDATA) = '8' THEN 1 ELSE 0 END) AS "r9",
SUM(CASE WHEN ABS(SIGNEDDATA) = '9' THEN 1 ELSE 0 END) AS "r10",
SUM(CASE WHEN ABS(SIGNEDDATA) = '10' THEN 1 ELSE 0 END) AS "r11",
SUM(CASE WHEN ABS(SIGNEDDATA) = '11' THEN 1 ELSE 0 END) AS "r12",
SUM(CASE WHEN ABS(SIGNEDDATA) = '12' THEN 1 ELSE 0 END) AS "r13",
SUM(CASE WHEN ABS(SIGNEDDATA) = '13' THEN 1 ELSE 0 END) AS "r14",
SUM(CASE WHEN ABS(SIGNEDDATA) = '14' THEN 1 ELSE 0 END) AS "r15"
FROM [db1].[dbo].[tbl1]
WHERE Category = 'rdata'
AND TIMEID BETWEEN 2013110000 AND 2013113100
GROUP BY ACCOUNT, ENTITY,CATEGORY, SIGNEDDATA, TIMEID
ORDER BY ACCOUNT, ENTITY
答案 0 :(得分:0)
1)结果不包含SIGNEDDATA, TIMEID
列(CATEGORY = 'rdata'
)的值。对于这些列,您会看到同一ACCOUNT
和ENTITY
的不同值。如果从GROUP BY
和SELECT
子句中删除了这些列,那么r1, r2, ... r15
的值将组合(SUM)。另外,我看到您的查询有两个Account
列:FACCOUNT
(GROUP BY
)和ACCOUNT
(SELECT
)。
SELECT CATEGORY,
ENTITY,
FACCOUNT,
/*SIGNEDDATA,
TIMEID,*/
SUM(CASE WHEN ABS(SIGNEDDATA) = '1' THEN 1 ELSE 0 END) AS "r1",
SUM(CASE WHEN ABS(SIGNEDDATA) = '2' THEN 1 ELSE 0 END) AS "r2",
SUM(CASE WHEN ABS(SIGNEDDATA) = '3' THEN 1 ELSE 0 END) AS "r4",
SUM(CASE WHEN ABS(SIGNEDDATA) = '4' THEN 1 ELSE 0 END) AS "r5",
SUM(CASE WHEN ABS(SIGNEDDATA) = '5' THEN 1 ELSE 0 END) AS "r6",
SUM(CASE WHEN ABS(SIGNEDDATA) = '6' THEN 1 ELSE 0 END) AS "r7",
SUM(CASE WHEN ABS(SIGNEDDATA) = '7' THEN 1 ELSE 0 END) AS "r8",
SUM(CASE WHEN ABS(SIGNEDDATA) = '8' THEN 1 ELSE 0 END) AS "r9",
SUM(CASE WHEN ABS(SIGNEDDATA) = '9' THEN 1 ELSE 0 END) AS "r10",
SUM(CASE WHEN ABS(SIGNEDDATA) = '10' THEN 1 ELSE 0 END) AS "r11",
SUM(CASE WHEN ABS(SIGNEDDATA) = '11' THEN 1 ELSE 0 END) AS "r12",
SUM(CASE WHEN ABS(SIGNEDDATA) = '12' THEN 1 ELSE 0 END) AS "r13",
SUM(CASE WHEN ABS(SIGNEDDATA) = '13' THEN 1 ELSE 0 END) AS "r14",
SUM(CASE WHEN ABS(SIGNEDDATA) = '14' THEN 1 ELSE 0 END) AS "r15"
FROM [db1].[dbo].[tbl1]
WHERE Category = 'rdata'
AND TIMEID BETWEEN 2013110000 AND 2013113100
GROUP BY FACCOUNT, ENTITY,CATEGORY/*, SIGNEDDATA, TIMEID*/
ORDER BY FACCOUNT, ENTITY
2)另一种解决方案可能是将SUM与OVER clause一起使用,但SIGNEDDATA, TIMEID
列的问题仍然存在:
SELECT CATEGORY,
ENTITY,
FACCOUNT,
SIGNEDDATA
TIMEID,
SUM(CASE WHEN ABS(SIGNEDDATA) = 1 THEN 1 ELSE 0 END) OVER(PARTITION BY FACCOUNT, ENTITY) AS "r1",
SUM(CASE WHEN ABS(SIGNEDDATA) = 2 THEN 1 ELSE 0 END) OVER(PARTITION BY FACCOUNT, ENTITY) AS "r2",
SUM(CASE WHEN ABS(SIGNEDDATA) = 3 THEN 1 ELSE 0 END) OVER(PARTITION BY FACCOUNT, ENTITY) AS "r3",
...
FROM [db1].[dbo].[tbl1]
WHERE Category = 'rdata'
AND TIMEID BETWEEN 2013110000 AND 2013113100
ORDER BY FACCOUNT, ENTITY
注意:在最后一种情况下,分组由PARTITION BY
完成。