SQL和sum行中的Pivot函数

时间:2017-02-03 13:44:06

标签: sql sql-server

我有这个查询: 与cte as

(
Select distinct
Account,
Avg(TotalME_AUA)over (partition by sub.account) as avgAUA
from
(

SELECT Distinct  wf_pm_asset_db.account,
       Sum(wf_pm_asset_db.me_aua) OVER(PARTITION by wf_pm_asset_db.account, wf_pm_asset_db.yearmonth)  AS TotalME_AUA, wf_pm_asset_db.yearmonth
FROM   wf_pm_asset_db  

WHERE  (( ( wf_pm_asset_db.yearmonth ) > '201512' )) 
and wf_pm_asset_db.account in ('XXXXX0E','XXXXX0F','XXXXX0G', 'XXXXX0H')
GROUP  BY wf_pm_asset_db.account  , wf_pm_asset_db.yearmonth, me_aua
)  sub
group by Account ,totalME_AUA, sub.account,TotalME_AUA
)
Select distinct CASE
                 WHEN (LTRIM(RTRIM(ISM_HH_KEY)) <> '')--OR --(LTRIM(RTRIM(v.ISM_HH_KEY)) IS NOT NULL)
                    THEN LTRIM(RTRIM(ISM_HH_KEY))
                    ELSE LEFT(LTRIM(RTRIM(WF_PM_ACCT_det_DB.ACCOUNT)), 6)
                 END AS 'HH',
CTE.account,
SUM(AvgAUA) as 'Avg_AUA'
FROM CTE
left join wf_pm_acct_det_db
on CTE.account = wf_pm_acct_det_db.Account
Group By wf_pm_acct_det_db.ISM_HH_Key, LEFT(LTRIM(RTRIM(WF_PM_ACCT_det_DB.ACCOUNT)), 6), cte.account

,结果如下:

HouseID Account number  Balance
HHID    Account 1   627.01
HHID    Account 2   829633.1317
HHID    Account 3   0
HHID    Account 4   -771703.1858

我想要一个输出,我得到这个:

HouseID Account 1   Account 2   Account 3   Account 4   Total
HHID    627.01      829633.1317     0      -771703.1858  58556.95583

我试过转动:

Select distinct CASE
                 WHEN (LTRIM(RTRIM(ISM_HH_KEY)) <> '')--OR --(LTRIM(RTRIM(v.ISM_HH_KEY)) IS NOT NULL)
                    THEN LTRIM(RTRIM(ISM_HH_KEY))
                    ELSE LEFT(LTRIM(RTRIM(WF_PM_ACCT_det_DB.ACCOUNT)), 6)
                 END AS 'HH',
account,
SUM(AvgAUA) as 'Avg_AUA'
From CTE
FROM CTE
left join wf_pm_acct_det_db
on CTE.account = wf_pm_acct_det_db.Account
Pivot
(
SUM(AvgAUA) 
for CTE.account in (account1, account2)
) as PT

然而我得到错误。任何人都可以建议如何解决这个问题。 我使用的是SQL Server 2012

2 个答案:

答案 0 :(得分:1)

从概念上讲,您只需要获取当前查询,该查询为您提供跨行的帐户信息,并以帐号为中心。为此,我们可以定义第二个CTE然后转向它。

WITH cte2 AS (
    SELECT DISTINCT CASE WHEN (LTRIM(RTRIM(ISM_HH_KEY)) <> '')
                         THEN LTRIM(RTRIM(ISM_HH_KEY))
                         ELSE LEFT(LTRIM(RTRIM(WF_PM_ACCT_det_DB.ACCOUNT)), 6)
                    END AS 'HH',
           account,
           SUM(AvgAUA) AS 'Avg_AUA'
    FROM CTE
    LEFT JOIN wf_pm_acct_det_db
        ON CTE.account = wf_pm_acct_det_db.Account
    GROUP BY wf_pm_acct_det_db.ISM_HH_Key,
             LEFT(LTRIM(RTRIM(WF_PM_ACCT_det_DB.ACCOUNT)), 6),
             account
)

SELECT t.HH,
       MAX(CASE WHEN t.account = 'Account 1' THEN t.Avg_AUA END) AS [Account 1],
       MAX(CASE WHEN t.account = 'Account 2' THEN t.Avg_AUA END) AS [Account 2],
       MAX(CASE WHEN t.account = 'Account 3' THEN t.Avg_AUA END) AS [Account 3],
       MAX(CASE WHEN t.account = 'Account 4' THEN t.Avg_AUA END) AS [Account 4],
       (SELECT SUM(Avg_AUA) FROM cte2
        WHERE account IN ('Account 1', 'Account 2', 'Account 3', 'Account 4')) AS [Total]
FROM cte2 t
GROUP BY t.HH

答案 1 :(得分:0)

使用结果表,可以使用下一句话进行转动:

declare @pv table (HouseID varchar(20), AccountNumber varchar(20),  Balance decimal(18,4));

insert into @pv values
('HHID','Account 1', 627.01),
('HHID','Account 2', 829633.1317),
('HHID','Account 3', 0),
('HHID','Account 4', -771703.1858);

select HouseID, Total, [Account 1], [Account 2], [Account 3], [Account 4]
from
(select HouseID, Total = SUM(Balance) over (partition by HouseID),
        AccountNumber, Balance from @pv) st
pivot
(
    SUM(Balance)
    FOR AccountNumber in ([Account 1], [Account 2], [Account 3], [Account 4])
) as PV;

+---------+------------+-----------+-------------+-----------+--------------+
| HouseID | Total      | Account 1 | Account 2   | Account 3 |   Account 4  |
+---------+------------+-----------+-------------+-----------+--------------+
| HHID    | 58556,9559 | 627,0100  | 829633,1317 | 0,0000    | -771703,1858 |
+---------+------------+-----------+-------------+-----------+--------------+

检查:http://rextester.com/NURP25818