按付款类型分组的销售金额

时间:2020-09-29 08:16:42

标签: sql sql-server sql-server-2016

以下是使用“现金和卡”完成的销售数据的输出。保存现金和卡交易有一项新要求,其中将保存CashAmount和CardAmount中的内容以标识不同的金额,如下所示。

    Sales   Payment_type    CashAmount  CardAmount
    9.00    Card                0.00    0.00
    10.00   Cash                0.00    0.00
    8.80    Cash_Card           5.00    3.80
    9.35    Cash_Card           5.00    4.35

现在在月度销售报表中,我们将显示现金,卡,支票交易金额。现金与卡交易现金金额也应立即添加到“现金”列,而卡销售金额应添加到“卡”列。最终输出

     Sales  Payment_type    
     17.15          Card                
     20.00          Cash     
select 
    sum(a.total_amount) as Sales,
    b.Payment_type          
from
    sl_sales_trans_master a
    inner join sl_payment_master b on a.payment_type_id = b.payment_type_id            
where
    a.reading_master_id=@ReadingMasterID
group
    by b.Payment_type  

您能指导我如何处理吗?

谢谢。

4 个答案:

答案 0 :(得分:3)

您可以编写一个查询,其输出具有以下样式:

| Sum of Card Payments | Sum of Cash Payments |
-----------------------------------------------
|                17.15 |                20.00 |

该查询如下:

SELECT
    SUM(
        CASE
            WHEN t.Payment_Type = 'Cash_Card' THEN t.CardAmount
            WHEN t.Payment_Type = 'Card' THEN t.Sales
            ELSE 0.0
        END
    ) AS "Sum of Card Payments",
    SUM(
        CASE
            WHEN t.Payment_Type = 'Cash_Card' THEN t.CashAmount
            WHEN t.Payment_Type = 'Cash' THEN t.Sales 
            ELSE 0.0
        END
    ) AS "Sum of Cash Payments"
FROM
    sl_sales_trans_master t

答案 1 :(得分:2)

另一种变化。您尚未发布表结构的精确细节,但希望该方法对于您可以根据需要进行调整是相当明确的。本质上,它将Cash_Card组合视为两个独立的行。

with cte as (
select payment_type, sales as amount from sl_sales_trans_master where payment_type in ('Cash', 'Card')
union all
select 'Cash', cashamount from sl_sales_trans_master where payment_type = 'Cash_Card'
union all
select 'Card', cardamount from sl_sales_trans_master where payment_type = 'Cash_Card'
)
select sum(amount) as amount, payment_type from cte group by payment_type

答案 2 :(得分:2)

基本上,您需要从sales列或相应的列中提取每种付款类型的数据。您可以使用带有一些条件聚合的横向联接来做到这一点:

select v.payment_type, sum(v.sales) as sales
from sl_sales_trans_master stm cross apply
     (values ('Cash', (case when stm.payment_type = 'Cash' then stm.sales else stm.cashamount end),
             ('Card', (case when stm.payment_type = 'Card' then stm.sales else stm.cardamount end)
     ) v(payment_type, sales)
group by v.payment_type;

或者,您可以将值放在单独的列中:

select sum(case when stm.payment_type = 'Cash' then stm.sales else stm.cashamount end) as cash,
       sum(case when stm.payment_type = 'Credit' then stm.sales else stm.creditamount end) as credit 
from sl_sales_trans_master stm
             

答案 3 :(得分:1)

我已经创建了fiddle。虽然不如@deHaar的回答有效或简短,但我发现此验证容易。

select type, sum(amount)
from (
select
sum(sales) as amount, 'total_card_only' as detail, 'card' as type
from sl_sales_trans_master 
where Payment_type = 'Card'
union 
select sum(cardAmount), 'total_card_mixed', 'card' as type
from sl_sales_trans_master 
where Payment_type = 'Cash_Card'
union
select
sum(sales), 'total_cash_only', 'cash' as type
from sl_sales_trans_master
where Payment_type = 'Cash'
union 
select sum(cashAmount), 'total_cash_mixed', 'cash' as type
from sl_sales_trans_master 
where Payment_type = 'Cash_Card') as temp
group by type