C# SQl通过对视图数据二次查询,统计数据

问题描述: 原数据---------需要在原视图数据中,统计出每个Device_Num设备号下面的交易的总额和分别统计出微信支付宝的交易总额。

解决:从上图数据没办法使用直接查询出要求的数据。

         .1.首先查出满足需要的数据的类型,并进行分组

SELECT SUM(Amount) as TotleAmount ,Trade_type ,Device_Num FROM patientbookstatices GROUP BY Device_Num ,Trade_type;

         得到数据:

还是无法查出按要求的数据,

2.我们以上图数据作为临时表,在使用 case .... when... then  对临时表中的数据进行提取。

SELECT Device_Num, SUM(TotleAmount) as newTotleAmount ,
SUM(CASE Trade_type WHEN '支付宝' THEN TotleAmount ELSE 0 END ) AS Alitotle,
SUM(CASE Trade_type WHEN '微信' THEN TotleAmount ELSE 0 END ) AS Wxtotle
FROM
(SELECT SUM(Amount) as TotleAmount ,Trade_type ,Device_Num FROM patientbookstatices GROUP BY Device_Num ,Trade_type)
as TempTableDt
GROUP BY Device_Num;

结果:

猜你喜欢

转载自www.cnblogs.com/zhao-y/p/9897691.html
今日推荐