简体   繁体   English

我的查询在SQL中有效,但在SSRS中无效

[英]my query works in SQL but not in SSRS

Can someone please advise what I did wrong with the last WHERE clause? 有人可以告诉我我上一个WHERE子句做错了什么吗?

I have the following query that works fine in SQL: 我有以下在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: 当我将它们插入SSRS>数据集属性>查询时,唯一的区别是我在最后一个分组之前添加了另一行:

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 我已经制作了6个与字段来源相同的字段名称:ID,U,R,T_0,T_End,State2

My query parameters are @StartDate and @EndDate with values of [@StartDate] and [@EndDate] 我的查询参数是@StartDate和@EndDate,其值分别为[@StartDate]和[@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. 例如,状态列似乎只接收第一个“最大情况时”,第二和第三个“最大情况时”数据未返回,而我会在其中很多地方得到空白的T_0和T_End值,其中在SQL中,它们都将返回。

I tried to run the query in SSRS without the last WHERE clause and it works just fine. 我试图在没有最后一个WHERE子句的情况下在SSRS中运行查询,并且它工作得很好。

Thanks in advance. 提前致谢。

Make sure that @StartDate and @EndDate have the same case as the SSRS variables have. 确保@StartDate和@EndDate具有与SSRS变量相同的大小写。 In TSQL case is ignored (@startdate is the same as @StartDate) but not in SSRS. 在TSQL中,将忽略(@startdate与@StartDate相同),但在SSRS中则不会。 The variables case have to match exactly. 变量大小写必须完全匹配。

Found the issue, the WHERE clause needs to be moved inside the CTE section. 发现问题后,需要在CTE部分中移动WHERE子句。 Thank you for your help @MatBailie 谢谢您的帮助@MatBailie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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