双聚合旋转

时间:2016-10-18 18:41:32

标签: sql sql-server pivot aggregate

我有SQL下,可以根据各自的群组调出我的帐户。我只需返回我的详细信息表并更新行,然后删除详细信息表中的重复项。

TempAccountRollup是我的汇总表

TempAccount是我的详细信息表

此代码的新要求已经下降,我还需要计算各个部分。所以我会...

ACCOUNT CONTRIBUTIONS  Donation  Gifts
1        205             20       50

我想要的是:

ACCOUNT  CONTRIBUTION DONATION GIFTS CCOUNT DCOUNT GCOUNT
1           205          20     50     6      2      5

这可以使用下面的代码吗?

非常感谢。

SELECT      ACCOUNTID, 
            [1] as Contribution, 
            [2] as Donation, 
            [3] as Gifts
INTO #TempAccountRollup
    FROM 
    (SELECT ACCOUNTID, amount,
        ROW_NUMBER() OVER (PARTITION BY AccountID ORDER BY ACCOUNTID)
AS ROWNUM
FROM (SELECT accountID, namex , SUM(payment_amount) as amount
        from #TempAccount
        WHERE accountID IN (SELECT accountID from #TempAccount GROUP BY ACCOUNTID HAVING COUNT(accountID) > 5 
        )
        GROUP BY accountId, namex
        ) a ) test
PIVOT (SUM(amount) FOR RowNum IN ([1], [2], [3])) AS  pvt

需要更清楚一些。上面的代码生成第一个样本。这很好。我需要它来制作第二个样本。

由于

0 个答案:

没有答案