我有一個表,如:我使用的是SELECT name, SUM(trees_planted) FROM year2016 GROUP BY name
SQL(SQLite的)比較另一個重複的行分組的行的總和
|------------------------|
|day name trees_planted|
|------------------------|
|1 | alice | 3 |
|2 | alice | 4 |
|1 | bob | 2 |
|2 | bob | 4 |
|------------------------|
獲得:
name | trees_planted
alice | 7
bob | 6
但後來我有另一個表從2015年開始,我想比較一下上一年的結果,例如,如果愛麗絲2016年種植的樹數比2015年多,我會得到如下結果:
name | tree_difference
alice | -2 (if previous year she planted 5 trees, 5 -7 = -2)
bob | 0 (planted the same number of trees last year)
嘿,它的工作,我不明白爲什麼,但我會玩你的答案,看看我是否得到它,我也必須查找'使用'關鍵字,我第一次看到它。非常感謝你。 – userian
很高興有用@userian。也看看trincot的答案。它有不同的方法,但它也可能適用於你。 –