【问题标题】:Sales amount group by Payment Type按付款类型划分的销售额组
【发布时间】:2020-09-29 08:16:42
【问题描述】:

以下是使用 Cash & Card 完成的销售数据输出。有一个新的要求来保存 Cash&Card 交易,其中 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

现在在每月销售报表中,我们显示现金、卡、支票交易金额。 Cash&Card transaction 现金金额现在也应该添加到 Cash 列中,并且 Card sales Amount 应该添加到 card 列中。最终输出

     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  

你能指导我如何处理这个问题吗?

谢谢。

【问题讨论】:

  • 您的查询有什么问题?为什么 CardAmount 等于 18.35 对于 10.0 +9.0 的总和?
  • 抱歉更新卡销售额9+3.8+4.35 = 17.15
  • 您的样本数据中的 total_amount 列在哪里?
  • 为什么前两行的CashAmountCardAmount0.00
  • @Squirrel - 因为它只提供现金或只提供卡付款。但对于最后 2 行,客户使用现金和卡支付。因此 cashamount 和 cardamount 被保存。

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


【解决方案1】:

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

| 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

【讨论】:

  • @PrathapGangireddy 不客气...请考虑接受对您有帮助的答案之一。
  • @PrathapGangireddy 对不起,你没有。至少,您还没有点击此处接受任何答案的符号。
  • 好吧我不确定..对我来说它显示为点击..答案被接受
  • @PrathapGangireddy 好的,很奇怪......我看不到任何答案被接受。可能是错误或延迟。
【解决方案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

【讨论】:

    【解决方案3】:

    基本上,您需要从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
                 
    

    【讨论】:

      【解决方案4】:

      我创建了一个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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-03
        • 2021-11-12
        • 2017-11-04
        • 1970-01-01
        • 2020-02-21
        • 2012-05-01
        • 2018-08-25
        • 1970-01-01
        相关资源
        最近更新 更多