简体   繁体   English

SQL中求和字段中的计算

[英]Calculation in Sum Field in SQL

I have a query which is returning me a sum group by mode of payment 我有一个查询,该查询按付款方式向我返回一笔总和

select
RTRIM(shopNo) as Shop_Number,
REPLACE(CONVERT(VARCHAR(10), DateTransaction, 103), '/', '') AS Todays_Date,
CodeModeDePaiement as Mode_Of_Payment,
SUM(convert(DOUBLE PRECISION,  Amount * 100 ) * 10) As Amount
from InvoiceSettlement
where  CodeModeDePaiement in 
(101,130,132,135,104,103,124,107,
136,117,131,410,106,122,109,102,105,112,133,999)
and   DateTransaction =  '2017/12/31' 
group by ShopNo,DateTransaction,CodeModeDePaiement

The output result is a below : 输出结果如下:

Shop_Number Todays_Date Mode_Of_Payment Amount
  2         31122017    102             18421610
  2         31122017    130             2332371390
  2         31122017    132             1082810
  2         31122017    106             66457640
  2         31122017    117             23925000
  2         31122017    133             5700000
  2         31122017    999             -490653940
  2         31122017    101             2404194870

I want the result to always display Sum Amount for Mode_of_Payment(101 + 999) as 101. I need the result as below : 我希望结果始终显示Mode_of_Payment(101 + 999)的总金额为101。我需要以下结果:

Shop_Number Todays_Date Mode_Of_Payment Amount
  2         31122017    102             18421610
  2         31122017    130             2332371390
  2         31122017    132             1082810
  2         31122017    106             66457640
  2         31122017    117             23925000
  2         31122017    133             5700000
  2         31122017    101             1913540930

Use a CASE expression to map 101 and 999 to the same thing: 使用CASE表达式将101和999映射到同一事物:

SELECT
    RTRIM(shopNo) AS Shop_Number,
    REPLACE(CONVERT(VARCHAR(10), DateTransaction, 103), '/', '') AS Todays_Date,
    CASE WHEN CodeModeDePaiement IN (101, 999)
         THEN 101
         ELSE CodeModeDePaiement END as Mode_Of_Payment,
    SUM(CONVERT(DOUBLE PRECISION,  Amount * 100 ) * 10) AS Amount
FROM InvoiceSettlement
WHERE CodeModeDePaiement IN (101, 130, 132, 135, 104, 103, 124, 107, 136, 117,
    131, 410, 106, 122, 109, 102, 105, 112, 133, 999) AND
    DateTransaction = '2017/12/31' 
GROUP BY
    ShopNo,
    DateTransaction,
    CASE WHEN CodeModeDePaiement IN (101, 999)
         THEN 101
         ELSE CodeModeDePaiement END;     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM