1. 一張表a,欄位有date(日期),city(城市),area(區域),cl(期初人口數量),ml(期末人口數量),zl(增加的人口數量)
2. 只有當天zl有變動的時候才會有資料,如果不變動,則當天不記錄
3. 表內容示例:
4. date city area cl ml zl
5. 2018-05-01 cc1 a11 100 99 -1
6. 2018-05-02 cc1 a12 200 205 5
7. 2018-05-02 cc2 a21 50 100 50
8. 2018-05-03 cc1 a11 99 140 51
9. 要查每一個區域的一定時間范圍內的期初數量和期末數量,例:查2018-05-01至2018-05-03,cc1-a11的期初是100.期末是140,cc1-a12的期初是200,期末是205,cc2-a21的期初是50,期末是100
10. 找到兩種方式查期初和期末:一是取最小日期和最大日期所在的日期,據日期取找值;二是自關聯。目前因為資料量到2千萬,查詢速度很慢,尋找優化方案
11. 在線等,著急,謝謝!
uj5u.com熱心網友回復:
SELECT city,area,(select a1.cl from a as a1 where a1.date =min(a.date) and a1.city=a.city and a1.area=a.area ) as cl,
(select a2.ml from a as a2 where a2.date =max(a.date) and a2.city=a.city and a2.area=a.area ) as ml
FROM a where date between '2018-05-01' AND '2018-05-03' group by city,area
不知執行效率如何?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/72063.html
標籤:MySQL
上一篇:列印機 續打方面的問題
