sum(case when ct.tradeTotal >= 0 then 1 else 0 end)的意思


 

String hql = "select new com.ks.admin.report.dto.ReportMonthWithDrawalDto(" +
				"count(*)," + "sum(ct.tradeTotal)," + "sum(case when ct.tradeTotal >= 0 then 1 else 0 end)," + "sum(case when ct.tradeTotal >= 0 then ct.tradeTotal else 0 end)" + ") " + "from CustTrade ct " + "where ct.tradeType = 'DR' " + "and ct.tradeTime between ? and ? " + "and ct.tradeDesc like '%提現%'";

count(*)返回的是Long類型,Sum(Double類型)返回Double類型, sum(case when ct.tradeTotal >= ? then ct.1 else 0 end) 返回Long類型。

 

sum(case when ct.tradeTotal >= 0 then 1 else 0 end)的意思是當ct.tradeTotal>=0的時候會返回1,否則返回0,而sum會累加,因為0累加之后還是0,所以只會返回1的累加之后才會改變值,因此這表示統計ct.tradeTotal>=0的數據總數。為什么不用count呢?因為試驗過,count里面無法用條件。

sum(case when ct.tradeTotal >= 0 then ct.tradeTotal else 0 end)的意思是當ct.tradeTotal>=0的時候返回ct.tradeTotal的值,否則返回0,同理,如果條件成立,會累加ct.tradeTotal>=0的值。

 

如果以上描述有錯誤,請提出,一起共同學習。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM