简体   繁体   中英

my query works in SQL but not in SSRS

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.

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.

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

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