【问题标题】:SSRS: Using exists condition in sql where clause while passing values from SSRS ParameterSSRS:在从 SSRS 参数传递值时在 sql where 子句中使用存在条件
【发布时间】:2015-12-05 20:18:05
【问题描述】:
Table : incident
----------------
incident_id   usr_id    item_id   Inc_Date
10059926       191       61006    8-22-2015
10054444       222        3232    6-7-2015

Table: act_reg
--------------
 act_reg_id  act_type_id  incident_id    usr_id  act_type_sc
 454244         1        10059926         191    ASSIGN
 471938        115       10059926         191    TRAVEL TIME
 473379        40        10059926         191    FOLLOW UP
 477652        115       10059926         191    TRAVEL TIME
 489091        504       10059926         191    ADD_ATTCHMNTS
 477653        504       10054444         222    ADD_ATTCHMNTS

参数:@attach (value=1, Label=Yes & Value=0, Label=No)

 Result (While I am selecting 'Yes' in dropdown)
 ----------------------------------------------
 incident_id   usr_id    item_id  
 10059926       191      61006    
 10054444       222       3232

我的查询:

SELECT  incident.incident_id,incident.usr_id,incident.item_id
FROM  incident 
where exists (select * from act_reg 
              where incident.incident_id = act_reg.incident_id
                  and act_reg.act_type_sc (case when @attach=1 and act_reg.act_type_sc='ADD_ATTCHMNTS' then NULL else act_reg.act_type_sc end  )
           )

请帮帮我。

【问题讨论】:

  • 有什么问题?你有错误吗?您是否缺少 act_reg.act_type_sc(case 之间的 = ?
  • @HannoverFist : 改变添加 = 我也没有得到准确的结果。
  • 选择No时的预期结果集?
  • 我认为您的问题与 NULL 有关。您的 WHERE 子句将始终获取所有记录,因为 act_type_sc 将始终等于 NULL。如果您想在 @attach = 0 时排除 ADD_ATTCHMNTS,请尝试使用 AND (reg.act_type_sc <> 'ADD_ATTCHMNTS' OR @attach = 1) 而不是当前的 AND 行。
  • @BhupeshC : 当我选择“否”时,我需要获取没有任何附件信息的结果数据

标签: sql sql-server-2008 reporting-services ssrs-2008 ssrs-2008-r2


【解决方案1】:

您应该将查询更改为以下内容:-

SELECT  incident.incident_id,incident.usr_id,incident.item_id
FROM  incident
left join act_reg 
on incident.incident_id = act_reg.incident_id and act_reg.act_type_sc = 'ADD_ATTCHMNTS'
where 
((@attach = 1 and act_reg.act_reg_id is not null) or 
(@attach = 0 and act_reg.act_reg_id is null))
group by incident.incident_id,incident.usr_id,incident.item_id

SQL Fiddle

【讨论】:

    【解决方案2】:

    这是在 SQL Fiddle 中处理您的数据的查询。我用 0 替换了参数。

    http://www.sqlfiddle.com/#!6/7f8ef/3

    SELECT  incident.incident_id,incident.usr_id,incident.item_id
    FROM  incident 
    WHERE EXISTS (SELECT * from act_reg 
                  WHERE incident.incident_id = act_reg.incident_id
     AND (act_reg.act_type_sc <> 'ADD_ATTCHMNTS' OR @attach = 1)
               )
    

    【讨论】:

    • @attach 值为 0 时。会是什么结果?在这里它显示所有。但它不应该显示 10059926 和 10054444
    • 它只显示一个答案:incident_id usr_id item_id 10059926 191 61006
    • 尝试在我设置的 SQL Fiddle 演示中运行它。 sqlfiddle.com/#!6/7f8ef/3 为 0 时得到一条记录,@attach 为 1 时得到两条记录。
    • 是的,我试过了,当它为0时结果显示10059926,应该是没有值,
    • 我认为当@attach = 1 时它应该是 2 条记录,当它等于 0 时应该是 1 条记录。act_reg 表有 Incident 10059926 和一个 act_type_sc 不是 ADD_ATTCHMNTS。这不是您的查询应该做的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多