我正在尝试使用Case
语句将行转换为列,并且可以,但是我有重复的ID。
000001 NULL NULL NULL 12075000.000 NULL NULL NULL NULL NULL NULL NULL NULL
000001 NULL NULL NULL NULL 10500000.000 NULL NULL NULL NULL NULL NULL NULL
000001 NULL NULL NULL NULL NULL 10500000.000 NULL NULL NULL NULL NULL NULL
000001 NULL NULL NULL NULL NULL NULL 10500000.000 NULL NULL NULL NULL NULL
000001 NULL NULL NULL NULL NULL NULL NULL 10500000.000 NULL NULL NULL NULL
000001 NULL NULL NULL NULL NULL NULL NULL NULL 10500000.000 NULL NULL NULL
第一列是id
列,其他列代表月份。如您所见,id
是相同的。有没有一种方法可以显示所有12个月内每个ID的数据在一行中?
SELECT CID,
(CASE WHEN EOMDATE = '2018-08-16' THEN BALANCE end) AS SHRAWAN,
(CASE WHEN EOMDATE = '2018-09-16' THEN BALANCE end) AS BHADAU,
(CASE WHEN EOMDATE = '2018-10-17' THEN BALANCE end) AS ASHOJ,
(CASE WHEN EOMDATE = '2018-11-16' THEN BALANCE end) AS KARTIK,
(CASE WHEN EOMDATE = '2018-12-15' THEN BALANCE end) AS MANGSHIR,
(CASE WHEN EOMDATE = '2019-01-14' THEN BALANCE end) AS POUSH,
(CASE WHEN EOMDATE = '2019-02-12' THEN BALANCE end) AS MAGH,
(CASE WHEN EOMDATE = '2019-03-14' THEN BALANCE end) AS FALGUN,
(CASE WHEN EOMDATE = '2019-04-13' THEN BALANCE end) AS CHAITRA,
(CASE WHEN EOMDATE = '2019-05-14' THEN BALANCE end) AS BAISHAKH,
(CASE WHEN EOMDATE = '2019-06-16' THEN BALANCE end) AS JETH,
(CASE WHEN EOMDATE = '2019-07-17' THEN BALANCE end) AS ASHAD
FROM
(SELECT R.BR, R.CID, E.EOMDate, SUM(E.BALANCEAMT) AS BALANCE
FROM T_EOMHIST E
LEFT JOIN T_RELACC R ON E.Acc = R.Acc AND E.BR = R.BR AND E.APPTYPE IN ('1','3')
LEFT JOIN T_GLLINK G ON G.BR = E.BR AND G.Code = E.GlCode AND G.CoopRep01 = 'Y' and G.TableId in ('10','30')
LEFT JOIN T_GLCONTROL H ON H.BR = E.BR
WHERE H.FinYear = '2018' AND E.EOMDate BETWEEN H.FinYrStartDate AND H.FinYrEndDate
AND R.TYPE = '010' AND R.AppType IN ('1','3')
AND CID = '000001'
GROUP BY R.BR, R.CID, E.EOMDATE)T
WHERE CID = '000001'
这是选择查询的代码。
预期结果是同一列的单行记录。如果仅没有任何数据,请显示NULL
。我也尝试过使用GROUP BY
子句,但是它在显示聚合函数时显示了一些错误。
答案 0 :(得分:2)
SELECT CID,
SUM(ISNULL(CASE WHEN EOMDATE = '2018-08-16' THEN BALANCE end, 0)) AS SHRAWAN,
SUM(ISNULL(CASE WHEN EOMDATE = '2018-09-16' THEN BALANCE end, 0)) AS BHADAU,
SUM(ISNULL(CASE WHEN EOMDATE = '2018-10-17' THEN BALANCE end, 0)) AS ASHOJ,
SUM(ISNULL(CASE WHEN EOMDATE = '2018-11-16' THEN BALANCE end, 0)) AS KARTIK,
SUM(ISNULL(CASE WHEN EOMDATE = '2018-12-15' THEN BALANCE end, 0)) AS MANGSHIR,
SUM(ISNULL(CASE WHEN EOMDATE = '2019-01-14' THEN BALANCE end, 0)) AS POUSH,
SUM(ISNULL(CASE WHEN EOMDATE = '2019-02-12' THEN BALANCE end, 0)) AS MAGH,
SUM(ISNULL(CASE WHEN EOMDATE = '2019-03-14' THEN BALANCE end, 0)) AS FALGUN,
SUM(ISNULL(CASE WHEN EOMDATE = '2019-04-13' THEN BALANCE end, 0)) AS CHAITRA,
SUM(ISNULL(CASE WHEN EOMDATE = '2019-05-14' THEN BALANCE end, 0)) AS BAISHAKH,
SUM(ISNULL(CASE WHEN EOMDATE = '2019-06-16' THEN BALANCE end, 0)) AS JETH,
SUM(ISNULL(CASE WHEN EOMDATE = '2019-07-17' THEN BALANCE end, 0)) AS ASHAD
FROM FROM
...
GROUP BY CID
或者,如果您需要NULL而不是0:
NULLIF(SUM(ISNULL(CASE WHEN EOMDATE = '2018-08-16' THEN BALANCE end, 0)), 0) AS SHRAWAN
答案 1 :(得分:0)
尝试这样:
SELECT CID,
sum(isnull(SHRAWAN, 0)) AS SHRAWAN,
sum(isnull(BHADAU, 0)) AS BHADAU,
sum(isnull(ASHOJ, 0)) AS ASHOJ,
sum(isnull(KARTIK, 0)) AS KARTIK,
sum(isnull(MANGSHIR, 0)) AS MANGSHIR,
sum(isnull(POUSH, 0)) AS POUSH,
sum(isnull(MAGH, 0)) AS MAGH,
sum(isnull(FALGUN, 0)) AS FALGUN,
sum(isnull(CHAITRA, 0)) AS CHAITRA,
sum(isnull(BAISHAKH, 0)) AS BAISHAKH,
sum(isnull(JETH, 0)) AS JETH,
sum(isnull(ASHAD, 0)) AS ASHAD
from (
SELECT CID,
(CASE WHEN EOMDATE = '2018-08-16' THEN BALANCE end) AS SHRAWAN,
(CASE WHEN EOMDATE = '2018-09-16' THEN BALANCE end) AS BHADAU,
(CASE WHEN EOMDATE = '2018-10-17' THEN BALANCE end) AS ASHOJ,
(CASE WHEN EOMDATE = '2018-11-16' THEN BALANCE end) AS KARTIK,
(CASE WHEN EOMDATE = '2018-12-15' THEN BALANCE end) AS MANGSHIR,
(CASE WHEN EOMDATE = '2019-01-14' THEN BALANCE end) AS POUSH,
(CASE WHEN EOMDATE = '2019-02-12' THEN BALANCE end) AS MAGH,
(CASE WHEN EOMDATE = '2019-03-14' THEN BALANCE end) AS FALGUN,
(CASE WHEN EOMDATE = '2019-04-13' THEN BALANCE end) AS CHAITRA,
(CASE WHEN EOMDATE = '2019-05-14' THEN BALANCE end) AS BAISHAKH,
(CASE WHEN EOMDATE = '2019-06-16' THEN BALANCE end) AS JETH,
(CASE WHEN EOMDATE = '2019-07-17' THEN BALANCE end) AS ASHAD
FROM
(SELECT R.BR, R.CID, E.EOMDate, SUM(E.BALANCEAMT) AS BALANCE
FROM T_EOMHIST E
LEFT JOIN T_RELACC R ON E.Acc = R.Acc AND E.BR = R.BR AND E.APPTYPE IN ('1','3')
LEFT JOIN T_GLLINK G ON G.BR = E.BR AND G.Code = E.GlCode AND G.CoopRep01 = 'Y' and G.TableId in ('10','30')
LEFT JOIN T_GLCONTROL H ON H.BR = E.BR
WHERE H.FinYear = '2018' AND E.EOMDate BETWEEN H.FinYrStartDate AND H.FinYrEndDate
AND R.TYPE = '010' AND R.AppType IN ('1','3')
AND CID = '000001'
GROUP BY R.BR, R.CID, E.EOMDATE)T
WHERE CID = '000001'
) as t
group by t.CID