sqlite query to find sum of a year by the given dates

大城市里の小女人 提交于 2019-12-11 09:45:01

问题


I have written a query : SELECT YEAR(Date),SUM(replace(price, ',', '')) FROM example GROUP BY year(date); .I am trying to get sum of all price for that year but I am getting an error - no such function : year. Please guide me .I am a noob in db.Thanks again.

12/10/11        377,245.67
12/19/11        5,104,245.00
12/28/11        23,332.98
12/24/10        126,875.21
12/24/10        5,112,225.14
12/23/10        4,552.54
11/24/10        402.82
10/15/09        132,875.32
9/19/09     126,334,123.32
9/23/09     345,887.21
9/29/09     16,520.11
9/28/09     388,902.02
11/12/08        24,622.43
10/25/08        278,916.52
10/12/08        42.22
9/22/08     17,234.01
9/16/08     377,245.67
12/15/07        5,104,245.00
12/18/07        23,332.98
12/19/07        126,875.21
12/22/07        23,332.98
12/28/07        126,875.21

Format I am looking for:

Year(Date)               price
8           438483479.3
9           8774343479
10          5654389.34
11          5654354646
12          23434483479

回答1:


try this

SELECT strftime('%Y', Date),SUM(replace(price, ',', '')) FROM example GROUP BY strftime('%Y', Date);



回答2:


I suggest this sintax:

SELECT strftime('%Y',Date), SUM(replace(price, ',', '')) FROM example GROUP BY strftime('%Y',Date);

Hope it helps




回答3:


There is no function year() in sqlite. You must use function strftime() instead.

For example, to get year value of the field Date, you can use strftime("%Y", Date).

Therefore, you must replace year(Date) in your query by strftime("%Y", Date).



来源:https://stackoverflow.com/questions/14019624/sqlite-query-to-find-sum-of-a-year-by-the-given-dates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!