Jacob Wood
Jacob Wood

Reputation: 467

get data for the year within from and to dates

I have the following query which gets year,Sum data between from and to date.
It works perfectly.

SELECT strftime('%Y',Date), SUM(Amount) 
from table where  (Date between '2008-08-01' and '2012-07-31') 
GROUP BY strftime('%Y',Date)

I am displaying the sum on a bar chart.
Now If I click on the respective year it should show all the transactions till the "To Date" for that year or if the value is smaller than "To date" then the entire year's transaction.

For Eg: In the example above it displays Sum From Date : 2008-08-01 and To Date: 2012-07-31 .

If the user taps on the year 2012 bar column it should show transactions till the date 2012-07-31 and not for the entire year but if the user taps on 2011 it should transaction for the entire year as the TO Date is greater than 2011 year.

Same for the year 2008, it should display data from "2008-08-01" not the entire year and if I tap the bar column of that year it should show the transactions from that particular date till the end of the year and not for the entire year.

I have the query below which returns wrong value .

SELECT strftime('%Y',Date) ,* 
from table where (Date between '2011-08-01' and '2012-07-31') 
                 and strftime('%Y',Date)='2012' 
GROUP BY strftime('%Y',Date)

This returns only one row for the date 2012-01-01 not all the values for the year 2012 till 2012-07-31.

Basically I would like to change only year in the following query and rest should be same. strftime('%Y',Date)='2012'

I don't know how to fix it or How to go about it w.r.t the above requirement.
Please help me fix it or Any Alternative. Thanks in Advance.

Upvotes: 1

Views: 183

Answers (1)

valex
valex

Reputation: 24144

You don't need GROUP BY

SELECT strftime('%Y',Date) ,* 
from table where (Date between '2011-08-01' and '2012-07-31') 
                 and strftime('%Y',Date)='2012' 

Upvotes: 1

Related Questions