2

Can someone please advise what I did wrong with the last WHERE clause?

I have the following query that works fine in SQL:

with
t1
as
(select ID, state, T, U, R, 
 row_number() over (partition by ID, U order by T asc) as asc_T, 
 row_number() over (partition by ID, U order by T desc) as desc_T
 from rawdata
 where R like ‘%123%’)

 select ID, 
 max(case when desc_T = 1 then state else null end) as state2, 
 max(case when asc_T = 1 then T else null end) as T_0, 
 max(case when desc_T = 1 then T else null end) as T_End, 
 U, R 
 from t1

 group by ID, U, R
 order by ID, U

When I plug them in SSRS > Dataset Properties > Query the only difference is that I added an additional line before the last group by:

with
t1
as
(select ID, state, T, U, R, 
 row_number() over (partition by ID, U order by T asc) as asc_T, 
 row_number() over (partition by ID, U order by T desc) as desc_T
 from rawdata
 where R like ‘%123%’)

 select ID, 
 max(case when desc_T = 1 then state else null end) as state2, 
 max(case when asc_T = 1 then T else null end) as T_0, 
 max(case when desc_T = 1 then T else null end) as T_End, 
 U, R 
 from t1

 where T > @StartDate and T < @EndDate
 group by ID, U, R
 order by ID, U

Query type is text. I've verified my data source.

I have made 6 field names that are identical to my field sources: ID, U, R, T_0, T_End, State2

My query parameters are @StartDate and @EndDate with values of [@StartDate] and [@EndDate]

But when I run it it's missing a bunch of data and the table looks off.
For instance, it seems that the state column only picks up and first 'max case when', second and third 'max case when' data did not get returned, and i'd get blank T_0 and T_End values for a lot of them where in SQL they all get returned.

I tried to run the query in SSRS without the last WHERE clause and it works just fine.

Thanks in advance.

13
  • 4
    Maybe it's just a typo in the question, but the end parenthesis for the definition of t1 is missing. Commented Jul 24, 2015 at 13:15
  • 1
    Can you give an example of a row of data that you would expect to be returned by the query, but isn't? We really can't help diagnose a problem described as "missing a bunch of data". Commented Jul 24, 2015 at 13:17
  • 1
    Troubleshoot your filtering issues by writing queries. SSRS complicates the issue. Commented Jul 24, 2015 at 13:26
  • 2
    You can't have added that WHERE clause to the end; it must have gone before the GROUP BY, or inside of t1's CTE. Please could you paste in the exact query that exists in SSRS, just to be really specific and eliminate any possible ambiguity or doubt? My best guess is that you've added it to the outer query, which can mean that the CTE assigns a row the value 1 for asc_T, but then your outer query filters that row out. Commented Jul 24, 2015 at 13:41
  • 2
    To test if it's SSRS (unlikely) or your query, try this; run it through SSRS and find an ID where things look wrong. Run your CTE (the t1) for just that ID, then the whole query for just that ID, then the whole query plus the WHERE clause for just that ID. Commented Jul 24, 2015 at 13:48

2 Answers 2

2

Make sure that @StartDate and @EndDate have the same case as the SSRS variables have. In TSQL case is ignored (@startdate is the same as @StartDate) but not in SSRS. The variables case have to match exactly.

Sign up to request clarification or add additional context in comments.

1 Comment

Their cases do match, I double checked.
1

Found the issue, the WHERE clause needs to be moved inside the CTE section. Thank you for your help @MatBailie

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.