Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need Help Grouping by Month & Year

Author  Topic 

Jason2112
Starting Member

17 Posts

Posted - 2009-03-30 : 20:48:03
I read through a lot of old posts but I didn't see anything that covered my particular situation, so please bear with me. I have a view (MS SQL 2005) that contains sales data:
-SalespersonNo
-CustomerNo
-ProductType
-PostingDate
-DollarsSold
-QtySold

I haven't yet decided what to use for a front-end (it's either Access or Crystal Reports), but I want to try to do as much sorting on the server side before the data is imported to the reporting tool. I need to produce a report that shows sales ($ and qty) by ProductType, by Customer, by Salesperson for a given month with comparison data for the same month in the prior year. Something like this:

Salesperson | Customer | ProductType | DollarsSoldJan09 | DollarsSoldJan08 | %change | QtySoldJan09 | QtySoldJan08 | %change

Ideally I'd like the final table/view to show summary data by month for each ProductType for CY (09) and LY (08). I thought I could do this in Access with a Crosstab query, and I was able to make a crosstab query for each year with the data I needed, but when I tried to join them I got some really funky results. So I'd prefer to do as much on the server side as possible. Here's what I've got so far for the 2008 view:

SELECT  TOP (100) PERCENT SalespersonName, CustomerName, ProductType, SUM(DollarsSold) AS DollarsSold08, SUM(QuantitySold) AS QtySold08, MONTH(PostingDate) AS Month
FROM dbo.uvwMoSalesRpt_08
GROUP BY MONTH(PostingDate), SalespersonName, CustomerName, PostingDate, ProductType
ORDER BY SalespersonName, CustomerName, ProductLineDescription


At least with this I can get the month in a 2-digit format, but it's not summarized by month (single entry for each ProductType for each month). I appreciate some help in putting together a proper view(s).

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-03-31 : 03:52:36
1. Remove the GROUP BY postingdate
SELECT		TOP (100) PERCENT
SalespersonName,
CustomerName,
ProductType,
SUM(DollarsSold) AS DollarsSold08,
SUM(QuantitySold) AS QtySold08,
MONTH(PostingDate) AS Month
FROM dbo.uvwMoSalesRpt_08
GROUP BY MONTH(PostingDate),
SalespersonName,
CustomerName,
ProductType
ORDER BY SalespersonName,
CustomerName,
ProductLineDescription



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-03-31 : 04:03:09
Cross post
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=122813



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Jason2112
Starting Member

17 Posts

Posted - 2009-03-31 : 11:36:04
Thanks that worked (oversight on my part).

BTW it's not really a cross post because I'm trying to solve 2 similar problems in two different applications. The other question is still valid.
Go to Top of Page
   

- Advertisement -