在sql上使用Pivot来获得结果

时间:2018-04-25 12:12:51

标签: sql-server

我的表结构如下:

create table temp
(
    storeid int,
    txncount int,
    txntype nvarchar(10),
    paymode nvarchar(10),
    amount money
)

insert into temp values ('1099','1560', 'Buy','Cash', 1000.00)
insert into temp values ('1099','1', 'Sell' ,'Bank', 500.00)
insert into temp values ('1099','1618','Sell', 'Cash', 800.00)
insert into temp values ('1099','1','Sell', 'Cheque', 700.00)
insert into temp values ('1099','1','Sell', 'Wallet', 1100.00)

我一直在努力按预期显示结果。

Expected Output

我想我需要使用PIVOT来获得上述结果。但我无法分发结果列中所示的付款方式。

我使用了如下枢轴。但无法理解如何选择列。

(
sum(Amount)
FOR  paymode in(Cash,Bank,Cheque,Wallet)
) AS R
group by      
    storeid

2 个答案:

答案 0 :(得分:1)

这似乎接近你的要求。我们首先进行一些类型转换和unpivot,以便所有要转移的值都在列中,然后合成最终的列名并执行pivot

declare @temp table
(
    storeid int,
    txncount int,
    txntype nvarchar(10),
    paymode nvarchar(10),
    amount money
)
insert into @temp values
('1099','1560', 'Buy','Cash', 1000.00),
('1099','1', 'Sell' ,'Bank', 500.00),
('1099','1618','Sell', 'Cash', 800.00),
('1099','1','Sell', 'Cheque', 700.00),
('1099','1','Sell', 'Wallet', 1100.00)

select
    *
from
    (select
        storeid,txntype + CASE WHEN type='txncount' THEN 'Count'
                               ELSE paymode END as newtxntype,
        value
    from
        (select storeid,CONVERT(decimal(18,4),txncount) as txncount,txntype,paymode,
                        CONVERT(decimal(18,4),amount) as amount from @temp) t
            unpivot ([value] for [type] in (txncount,amount)) u
    ) x
        pivot
            (SUM(value) for newtxntype in (SellCount,SellCash,SellBank,SellCheque,
                      SellWallet,
                               BuyCount,BuyCash,BusBank,BuyCheque,BuyWaller)) y

结果:

storeid     SellCount                               SellCash                                SellBank                                SellCheque                              SellWallet                              BuyCount                                BuyCash                                 BusBank                                 BuyCheque                               BuyWaller
----------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
1099        1621.0000                               800.0000                                500.0000                                700.0000                                1100.0000                               1560.0000                               1000.0000                               NULL                                    NULL                                    NULL

答案 1 :(得分:1)

您也可以在不使用PIVOT的情况下转动数据。

DECLARE @temp TABLE
(
    storeid int,
    txncount int,
    txntype nvarchar(10),
    paymode nvarchar(10),
    amount money
)

insert into @temp values ('1099','1560', 'Buy','Cash', 1000.00)
insert into @temp values ('1099','1', 'Sell' ,'Bank', 500.00)
insert into @temp values ('1099','1618','Sell', 'Cash', 800.00)
insert into @temp values ('1099','1','Sell', 'Cheque', 700.00)
insert into @temp values ('1099','1','Sell', 'Wallet', 1100.00)



SELECT 
    storeid, 
    SellCount =SUM(CASE WHEN txntype = 'Sell' THEN txncount ELSE NULL END),
    SellCash = SUM(CASE WHEN txntype = 'Sell' AND paymode = 'Cash' THEN amount ELSE NULL END),
    SellCash = SUM(CASE WHEN txntype = 'Sell' AND paymode = 'Bank' THEN  amount ELSE NULL END),
    SellChecque = SUM(CASE WHEN txntype = 'Sell' AND paymode = 'Cheque' THEN  amount ELSE NULL END),
    SellWallet = SUM(CASE WHEN txntype = 'Sell' AND paymode = 'Wallet' THEN  amount ELSE NULL END),
    BuyCount = SUM(CASE WHEN txntype = 'Buy'  THEN txncount ELSE NULL END),
    BuyCash = SUM(CASE WHEN txntype = 'Buy' AND paymode = 'Cash' THEN amount ELSE NULL END),
    BuyCash = SUM(CASE WHEN txntype = 'Buy' AND paymode = 'Bank' THEN amount ELSE NULL END),
    BuyChecque = SUM(CASE WHEN txntype = 'Buy' AND paymode = 'Cheque' THEN amount ELSE NULL END),
    BuyWallet = SUM(CASE WHEN txntype = 'Buy' AND paymode = 'Wallet' THEN amount ELSE NULL END)
FROM
    @temp
GROUP BY
    storeid