简体   繁体   中英

SSRS Report with date range with group by

I have a table FinalQuote in SQL Server with these columns:

Q_hub_from : Perth, Burnie, Sydney etc.
Q_hub_to : Perth, Burnie, Sydney etc.
FinalMargin : 400, 500, 650 etc.
processed_at : Date Time
BookedYN : Yes/No.

I want to create a SSRS report which will show the average value of FinalMargin for all the rows from a specific Q_hub_from to specific Q_hub_to . Also wanna show average margin in two columns: one contain average of rows where BookedYN is 'YES' and other contain average of rows where BookedYN is 'NO'. It is working fine and display required result when I specify a date range(for processed_at ) in query.

My query so far is:

SELECT 
    Q_hub_from, Q_hub_to, 
    ROUND(AVG(FinalMargin), 0) AS Margin,
    COUNT(*) AS NoOfQuotesDone, 
    COUNT(CASE BookedYN WHEN 'Yes' THEN 1 ELSE NULL END) AS NoOfQuotesBooked,
    ROUND(AVG(CASE BookedYN WHEN 'No' THEN FinalMargin ELSE NULL END), 0) AS MarginWhenNotBooked, 
    ROUND(AVG(CASE BookedYN WHEN 'Yes' THEN FinalMargin ELSE NULL END), 0) AS MarginWhenBooked
FROM 
    FinalQuote
WHERE 
    Q_hub_from <> '' 
    AND Q_hub_from IS NOT NULL
    --AND CAST(processed_at as date) BETWEEN '2018-08-01' AND '2018-10-10'
    AND CAST(processed_at AS DATE) BETWEEN DATEADD(day, -10, GETDATE()) AND GETDATE()
GROUP BY 
    Q_hub_from, Q_hub_to
ORDER BY 
    Q_hub_from ASC

This query produces this output:

在此处输入图片说明

But I want date range to be selected by user, so I added StartDate and EndDate parameters. My processed_at column only mentioned in the WHERE clause, not in the select statement, so I get this error:

[rsMissingFieldInDataSet] and [rsErrorReadingDataSetField]

When I add processed_at field in DataSet and in GROUP BY clause then it is working (although still displays previous error) but now result is not expected. It shows 2 rows for same Q_hub_from and Q_hub_to .

Query:

SELECT Q_hub_from, Q_hub_to, processed_at, ROUND(AVG(FinalMargin),0) AS Margin,
COUNT(*) AS NoOfQuotesDone, COUNT(case BookedYN when 'Yes' then 1 else null end) AS NoOfQuotesBooked,
ROUND(AVG(case BookedYN when 'No' then FinalMargin else null end),0) AS MarginWhenNotBooked, ROUND(AVG(case BookedYN when 'Yes' then FinalMargin else null end),0) AS MarginWhenBooked
FROM FinalQuote
WHERE Q_hub_from <> '' AND Q_hub_from IS NOT NULL 
AND processed_at between @StartDate AND @EndDate
GROUP BY Q_hub_from, Q_hub_to, processed_at
ORDER BY Q_hub_from ASC

Output:

在此处输入图片说明

If anyone can help me in achieving the required result that only show 1 row for same Q_hub_from to Q-hub_to within specific date range(ie processed_at between StartDate and EndDate ) that will be of great help. I don't wanna display processed_at column. I know I am doing something wrong as Its my first time with SSRS.

Feel free to ask if you want to know more. Any help will be appreciated! Thanks.

If you have not set the values for parametes then try to assign some values to parameters and make sure parameter type is Date.
You can do it like this, create a dataset and put this query
select DATEADD(day, -10, GETDATE()) AS StartDate, GETDATE() AS EndDate

Use this dataset for your both the parameters only in default values section. Don't add it in available values section.

Suggestion 1: AND cast( processed_at as date) between @StartDate AND @EndDate . As you need to make sure how this column is stored in the tables. If it is DateTime then do an implicit cast to date. Suggestion 2: on the SSRS make sure you can see your 2 params are params. IF you do then DO NOT SET any values for them. See if you can pick any random range and report renders. Suggestion 3: if you have to put default values make sure they are either query or best case stored proc generated (sometime like usp_report_params_defaultDateRange). You will give this for default date range. Also type will be date and not int, varchar, etc. Suggestion 4: If above fail trying building a query and just hardcoding the values so something like Select '2000-01-01' as Q_hub_from, '2001-01-01' as Q_hub_to from table name where cast( processed_at as date) between @StartDate AND @EndDate and see if it works. Add 1 column at a time and anchor to report and see if it gives you error. (NOTE YOU HAVE TO DELETE .DATA file from your local Bin folder after each local successful run)

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