1. 程式人生 > >MySQL統計當日和最新資料

MySQL統計當日和最新資料

#地圖警情統計sql
#查詢schemaid=GASJ001各個派出所的 所有案件型別的當日之和,統計當日的資料
SELECT columnvalue1,SUM(columnvalue5) FROM z_gasj 
WHERE schemaid='GASJ001' AND TO_DAYS(gathertime) = TO_DAYS(NOW())
GROUP BY columnvalue1

#查詢schemaid=GASJ001各個派出所的 所有案件型別的數量和,統計最新資料
SELECT columnvalue1,SUM(columnvalue5) 當日和 FROM z_gasj AS b 
WHERE NOT EXISTS(SELECT 1 FROM z_gasj 
WHERE Id= b.Id AND b.gathertime<gathertime)
AND schemaid='GASJ001' 
GROUP BY columnvalue1

#警情統計總覽sql
#查詢schemaid=GASJ001各個案件型別六個派出所最新值之和,統計最新
SELECT columnvalue2,SUM(columnvalue5) 當日和 FROM z_gasj AS b 
WHERE NOT EXISTS(SELECT 1 FROM z_gasj 
WHERE Id= b.Id AND b.gathertime<gathertime)
AND schemaid='GASJ001' 
AND columnvalue1 IN (520114510000,520114230000,520114540000,520114550000,520114520000,520114530000)
GROUP BY columnvalue2