简体   繁体   中英

SSRS Reports: How do I create a monthly/YTD report like this?

I have a table that looks like this:

-------------------------------------------------------------------
CUSTNUM (INT), ITEMNUM (INT), MONTH (INT), YEAR (INT), AMOUNT (INT)
-------------------------------------------------------------------
100000, 489, 1, 2011, 4000
100000, 489, 2, 2011, 3000
100000, 489, 3, 2011, 5000
100000, 587, 1, 2011, 7500
100000, 587, 2, 2011, 6800
100000, 587, 3, 2011, 9000

What I'm trying to create is a report that shows me each item a customer has purchased in a given month and year. On the same page , I would like a table (either part of the same matrix or whatever is required) that has the Year-to-Date sales grouped by itemnum on each line.

The end result should be a summary-like page (maybe multiple pages) for each customer with the monthly sales at the top and a year-to-date summary of each item purchased at the bottom.

Here's what the desired output would look like when queried against March 2011:

-----------------------
Customer: 100000
-----------------------
Item    Month Quantity
-----------------------
489     5000
587     9000
-----------------------
Total   14000

------------------------
Item    Year-to-Date Qty
------------------------
489     12000
587     23300
------------------------
Total   35300

The next page break would be for a new customer with similar layout.

Specifically, I'm struggling with what the query side of things should look like. I have a working query for the specific month -- and if I omit the MONTH parameter from the where clause, I get all of the rows required for year-to-date -- but then how do I show just the single month up at the top?

Thanks in advance!

If you are using SQL 2008 R2, use Lookup function. This function is not available in earlier editions.

Keep the data set that you already have. No need to worry about YTD yet. Create a second data set that finds the YTD for given customer, group by item. Lookup function can find YTD in 2nd data set based on Item number in 1st data set.

Hope this helps.

Additional answer

Almost forgot about Sub-Report. You can create a second report that takes 1 parameter: CustNum, and shows YTD in a table. Add this as a Sub-Report to your main report. A good place would be in the table footer.

Sql is not designed to make the report, it is designed to extract your data so excel, report builder or some other tool can set up the design. I hope you can use this.

declare @t TABLE (custnum int, itemnum int, month int, year int, amount int)

DECLARE @m INT
DECLARE @y INT

set @m = 3
set @y = 2011

insert @t values(100000, 489, 1, 2011, 4000)
insert @t values(100000, 489, 2, 2011, 3000)
insert @t values(100000, 489, 3, 2011, 5000)
insert @t values(100000, 587, 1, 2011, 7500)
insert @t values(100000, 587, 2, 2011, 6800)
insert @t values(100000, 587, 3, 2011, 9000)

SELECT custnum customer, itemnum item, sum(amount) [Month Quantity]
FROM @t
WHERE MONTH = @m
AND year = @y
GROUP BY custnum, itemnum with rollup 

Result:

customer  item     month quantity
100000    498      5000
100000    587      9000
100000             14000 <---(total for this customer)
                   14000 <---(grand total for all customers)


SELECT itemnum item, sum(amount) [Year-to-Date Qty]
FROM @t
WHERE year = @y
GROUP BY itemnum with rollup

Result:

item      Year-to-Date Qty
489       12000
587       23300
          35300

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM