Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write this query to get total records per months within given date.
the query is:-

SQL
SELECT count(*) AS CountOfTotal, (year(invDate) & '/' & month(invDate)) AS Months
FROM SalesDetails
WHERE invDate between #07/25/15# and #04/25/16#
GROUP BY (year(invDate) & '/' & month(invDate))
ORDER BY (year(invDate) & '/' & month(invDate));

results are:
262 --- 2015/10
268 --- 2015/11
330 --- 2015/12
289 --- 2015/7
406 --- 2015/8
154 --- 2015/9

this will return results without sorting, because the value is converted to text.

so how I get the month wise Total-Count and sorting on year and month wise??

What I have tried:

First I tried:
SQL
SELECT count(total) AS CountOfTotal, month(invDate) AS Months
FROM SalesDetails
WHERE invDate between #07/25/15# and #04/25/16#
GROUP BY  month(invDate)
ORDER BY  month(invDate);

In this query I couldn't figure out which month in which year...

after that I tried this in SQLite it worked fine:
SQL
select sum(total) as CountOfTotal,strftime('%m',invDate) as Months from SalesDetails where invDate between '2015/05/25' and '2016/04/25' group by strftime('%m',invDate) order by invDate

this not working in MS Access because of aggregate function(Group by), I know the Sqlite query is not correct(but I don't know how its working)..
Posted
Updated 3-May-16 6:04am
v2

If the column invDate is of type Date you could try to change your query like this:
SQL
SELECT count(total) AS CountOfTotal, month(invDate) AS Months
FROM SalesDetails
WHERE invDate between #07/25/15# and #04/25/16#
GROUP BY  month(invDate)
ORDER BY  invDate;


You want to sort by both year and month, right?
 
Share this answer
 
Comments
JayantaChatterjee 30-Apr-16 22:47pm    
You got me right, but I already tried this. This doesn't work because invDate is not in Group by clause, so its give me an error...

thanks for the help..
George Jonsson 1-May-16 0:02am    
Ah OK. I missed your explanation with the SQLite code.
What happens if you do 'GROUP BY invDate' as well?
JayantaChatterjee 2-May-16 5:21am    
if I use invDate in GROUP BY then it group records with single day wise not for month wise..
George Jonsson 2-May-16 8:17am    
Sorry. I'm out of my league here.
Don't have the energy right now to set up a proper Access database either.
JayantaChatterjee 3-May-16 4:31am    
Okay. thank you..
I solved My problem with this Query:-
SQL
SELECT (MonthName(Month(invDate),true) &'-'&  year(invDate)) AS SalesDate, count(*) AS Total
FROM salesDetails
WHERE invDate between #07/01/2015# and #05/03/2016#
GROUP BY year(invDate), Month(invDate)
ORDER BY year(invDate), Month(invDate);

I also add MonthName function for getting the month name and concatenate the Year and Month, this all for my requirement...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900