需要資料統計頁面,肯定需要匯出資料,於是,邊學邊寫,完成了一段sql~最早的版本是這樣的:分三條sql查出三種不同的狀態的記錄數(總記錄,未支付,已支付)
select count(*) as recordcount from t_record where createtime >= '2019-01-01 00:00:00' and createtime < '2019-02-01 00:00:00'
select count(*) as unpayedcount from t_record where createtime >= '2019-01-01 00:00:00' and createtime < '2019-02-01 00:00:00' and ispayed = 0
select count(*) as payedcount from t_record where createtime >= '2019-01-01 00:00:00' and createtime < '2019-02-01 00:00:00' and ispayed = 1
但是,為什麼不能一條sql查出來呢?這樣在便利性還是效能上應該都會有所提公升,能否在count帶條件或者在sum做判斷?於是,最終的sql出現了:select count(*) as recordcount from t_record where createtime >= '2019-01-01 00:00:00' and createtime < '2019-02-01 00:00:00' group by convert(createtime,date)
select count(*) as unpayedcount from t_record where createtime >= '2019-01-01 00:00:00' and createtime < '2019-02-01 00:00:00' and ispayed = 0 group by convert(createtime,date)
select count(*) as payedcount from t_record where createtime >= '2019-01-01 00:00:00' and createtime < '2019-02-01 00:00:00' and ispayed = 1 group by convert(createtime,date)
select
convert(createtime,date) as recorddate,
count(*) as recordcount,
sum(case when ispayed = 0 then 1 else 0 end) as unpayedcount,
sum(case when ispayed = 0 then 1 else 0 end) as payedcount,
from t_record
where createtime >= '2019-01-01 00:00:00' and createtime < '2019-02-01 00:00:00'
group by convert(createtime,date)
相關報表圖形繪製:統計報表 – higncharts:線形圖餅圖繪製及時間等定製SQL語句 統計
統計某年每個月的資料資訊 如何時間欄位為int型別的秒數,mysql用 例如 select hour from unixtime posttime u as hourtime,year from unixtime posttime u as yeartime,count businessid as ...
關於統計報表常用sql
1 根據條件查總數 select count all sum,if sum if problem state in 1,2 1,0 is null,0,sum if problem state in 1,2 1,0 problem num,if sum if problem state 3,1,0 ...
SQL統計語句技巧
select t.t.rowid from ts orders t select from ts cust 查詢 type 0 1 的訂單數 select count o.oid as 總訂單數 count o1.oid as type1 訂單總數 count o2.oid as type0 訂單總...