1. 程式人生 > >C# SQl通過對檢視資料二次查詢,統計資料

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;

結果: