c# razor group data by month from datetime

Hi everyone,

I'm writing a new report for a sales system. I have a table that records sales, and I want to total the sales for each month.

The database table is called CART
The Date field is called DATESUBMITTED
The sales figure I want to total for each month is called TOTALVAT

Is it possible to write a query that will select all sales between 2 dates (populated by dropdown calenders) and then group them by month using the month/year parts of the datetime field?

I can generate a grouped Daily report using the following:

 sales = db.Query("SELECT SUM(TotalVAT) AS Total, DateSubmitted FROM Cart WHERE DateSubmitted IS NOT NULL AND DateSubmitted BETWEEN @0 AND @1 GROUP BY DateSubmitted",startdate,enddate);

Open in new window


All I need is the ability to define the Group By as the month, not the whole date.

Any ideas would be greatly appreciated!
RossHuddsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RossHuddsAuthor Commented:
It's always the way isn't it? You post a question and then solve it yourself 5 minutes later...

The trick is to convert the DateTime field like this:

        sales = db.Query("SELECT SUM(TotalVAT) AS Total, CONVERT(CHAR(4), DateSubmitted, 100) AS Datey FROM Cart WHERE DateSubmitted IS NOT NULL AND DateSubmitted BETWEEN @0 AND @1 GROUP BY CONVERT(CHAR(4), DateSubmitted, 100)",startdate,enddate);

Open in new window


Hope that helps someone.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.