根据货币获得总余额

时间:2016-07-15 12:14:00

标签: sql

所以我想为客户获得所有余额的总和。我想要做的是根据他们的货币获得所有价值的总和,例如,如果3个客户有1,2和3的英镑货币,基于货币的总价格将是6总计。

我尝试过什么

为了使数据看起来更具视觉吸引力,我已经能够使用Pivot功能,使Currency列显示为Columns而不是像这样的行:

SELECT *
FROM
(
    SELECT DISTINCT Customer AS Total 
    , Currency
    , Balances
    FROM #temptable
) SRC
PIVOT
(
    SUM(Balances)
    FOR [Currency] IN ([AUD],[CAD],[EUR],[GBP])
) AS P

但它会返回:

        AUD CAD EUR GBP
CUS1    451     122
CUS2         213
CUS3         211   154
CUS4               123

我只想拥有所有货币总数的行

2 个答案:

答案 0 :(得分:1)

如果要使用pivot功能,则必须在子查询中创建总计,然后才能对其进行透视,例如,可以使用GROUPING SETS进行,

-- DUMMY DATA
CREATE TABLE #temptable (Customer CHAR(4), Currency CHAR(3), Balances INT);
INSERT #TempTable (Customer, Currency, Balances)
VALUES 
    ('CUS1', 'AUD', 451), ('CUS1', 'EUR', 122), ('CUS2', 'CAD', 213), 
    ('CUS3', 'CAD', 211), ('CUS3', 'GBP', 154), ('CUS4', 'GBP', 123);

-- ACUTAL QUERY
SELECT ISNULL(CONVERT(VARCHAR(20), Customer), 'Column Total') AS Total 
, ISNULL(CONVERT(VARCHAR(20), Currency), 'RowTotal') AS Currency
, SUM(Balances) AS Balances
FROM #temptable t
GROUP BY GROUPING SETS ((Customer, Currency), (Customer), (Currency), ());

你获得总数(为了便于理解,我已将每个分组集分开)

Total           Currency    Balances
------------------------------------
-- Grouping set (Customer, Currency)
CUS1            AUD         451
CUS2            CAD         213
CUS3            CAD         211
CUS1            EUR         122
CUS3            GBP         154
CUS4            GBP         123
-- Grouping set (Customer)
CUS1            RowTotal    573     -- TOTAL FOR CUS1
CUS2            RowTotal    213     -- TOTAL FOR CUS1
CUS3            RowTotal    365     -- TOTAL FOR CUS1
CUS4            RowTotal    123     -- TOTAL FOR CUS1
-- Grouping set (Currency)
Column Total    AUD         451     -- TOTAL FOR AUD
Column Total    CAD         424     -- TOTAL FOR CAD
Column Total    EUR         122     -- TOTAL FOR EUR
Column Total    GBP         277     -- TOTAL FOR GBP
-- Grouping set ()
Column Total    RowTotal    1274    -- GRAND TOTAL

然后你做你的支点:

SELECT *
FROM
(
    SELECT ISNULL(CONVERT(VARCHAR(20), Customer), 'Column Total') AS Total 
    , ISNULL(CONVERT(VARCHAR(20), Currency), 'RowTotal') AS Currency
    , SUM(Balances) AS Balances
    FROM #temptable
    GROUP BY GROUPING SETS ((Customer, Currency), (Currency), (Customer), ())
) SRC
PIVOT
(
    SUM(Balances)
    FOR [Currency] IN ([AUD],[CAD],[EUR],[GBP],[RowTotal])
) AS P;

给出了:

Total           AUD     CAD     EUR     GBP     RowTotal
------------------------------------------------------------
Column Total    451     424     122     277     1274
CUS1            451     NULL    122     NULL    573
CUS2            NULL    213     NULL    NULL    213
CUS3            NULL    211     NULL    154     365
CUS4            NULL    NULL    NULL    123     123

我不确定你需要哪一个,所以添加了两个,但你应该能够根据需要进行调整。

实际上,一个更简单的查询与戈登所建议的一致:

SELECT  Total = ISNULL(CONVERT(VARCHAR(20), Customer), 'Total'),
        AUD = SUM(CASE WHEN Currency = 'AUD' THEN Balances END),
        CAD = SUM(CASE WHEN Currency = 'CAD' THEN Balances END),
        EUR = SUM(CASE WHEN Currency = 'EUR' THEN Balances END),
        GBP = SUM(CASE WHEN Currency = 'GBP' THEN Balances END),
        RowTotal = SUM(Balances)
FROM    #temptable
GROUP BY Customer WITH ROLLUP;

答案 1 :(得分:0)

只需使用聚合:

SELECT SUM(CASE WHEN Currency = 'AUD' THEN Balance ELSE 0 END) as aud,
       SUM(CASE WHEN Currency = 'CAD' THEN Balance ELSE 0 END) as cad,
       SUM(CASE WHEN Currency = 'EUR' THEN Balance ELSE 0 END) as eur,
       SUM(CASE WHEN Currency = 'GBP' THEN Balance ELSE 0 END) as gbp
FROM #temptable;

如果每个货币可以使用一行,GROUP BY更简单:

select currency, sum(balance)
from #temptable
group by currency;