简体   繁体   English

在where子句中使用case表达式

[英]Using a case expression in a where clause

Although I know this isn't very efficient, I need to get this working fast so I try to follow the easiest path, or at least I thought so. 虽然我知道这不是很有效,但我需要让它快速工作,所以我尝试遵循最简单的路径,或者至少我是这么认为的。 I'm trying to get the max data out of an entity and take into consideration only the rows where USER is not null, and in case no rows have USER not set to null, then I will take the null ones into consideration. 我试图从实体中获取最大数据并仅考虑USER不为空的行,并且如果没有行将USER设置为null,那么我将考虑空值。

The entity's structure is as follows: 该实体的结构如下:

  • Id int, Id int,
  • ParentId int, ParentId int,
  • Guid varchar, Guid varchar,
  • EventOn DateTime, EventOn DateTime,
  • User int (null) 用户int(null)

So, the query is the following one: 所以,查询是以下一个:

select RS.[$Id] RangeSheet,
       GRST.EventOn,
       RSIS.Id [Status],
       RSIS.Name StatusRefex    
  from RangeSheet RS
left outer join (select RST.ParentId RangeSheet,
                        MAX(RST.EventOn) EventOn
                   from RangeSheetTime RST
                  where RST.[User] is (case RST.[User] when null then null else not null)
               group by RST.ParentId) GRST on RS.[$Id]=GRST.RangeSheet
left outer join RangeSheetTime RST on GRST.EventOn=RST.EventOn 
                                  and GRST.RangeSheet=RST.ParentId
left outer join RangeSheetItemState RSIS on (case when RST.StartOrEnd = 1 then 1 when RST.StartOrEnd = 0 then 4 else null end) = RSIS.Id
 where RS.[$IsDeleted]=0

The case I'm having trouble with is the one inside the view GRST. 我遇到问题的情况是GRST视图中的一个。 What could I do to accomplish my requirements? 我能做些什么来完成我的要求?

I'm a bit baffled about what you're doing. 我对你正在做的事感到有点困惑。 Going off your text description though would something like the following help? 关闭你的文字描述虽然会有以下帮助吗?

;WITH t AS
     ( SELECT  Id     ,
              ParentId,
              Guid    ,
              EventOn ,
              USER    ,
              RANK() OVER (PARTITION BY ParentId ORDER BY
              CASE
                       WHEN USER IS NULL
                       THEN 0
                       ELSE 1
              END DESC, EventOn DESC) Rnk
     )
SELECT *
FROM   t
WHERE  Rnk=1

I think you are just going to have to left join in two different Max subqueries. 我想你只需要加入两个不同的Max子查询。

    left join (
    select RST.ParentId RangeSheet,
           MAX(RST.EventOn) EventOn
    from RangeSheetTime RST
    where Not RST.[User] is NULL
    group by RST.ParentId) max1

This subquery will not return a row if all of the ParentID rows have nulls in the User column. 如果所有ParentID行在“用户”列中都为空,则此子查询不会返回一行。

and

left join (
select RST.ParentId RangeSheet,
       MAX(RST.EventOn) EventOn
from RangeSheetTime RST
group by RST.ParentId) max2

Now just select the value you want. 现在只需选择您想要的值。

coalesce(max1.EventOn, max2.EventOn) as EventOn

Here's my re-write of your query: 这是我重新编写您的查询:

   SELECT rs.[$Id] RangeSheet,
          grst.eventon,
          rsis.id AS [Status],
          rsis.name AS StatusRefex    
     FROM RANGESHEET rs
LEFT JOIN (SELECT rst.ParentId,
                  rst.EventOn,
                  RANK() OVER (PARTITION BY rst.parentid
                                   ORDER BY CASE
                                              WHEN rst.user IS NULL THEN 0
                                              ELSE 1
                                            END DESC, rst.eventon DESC) Rnk
            FROM RANGESHEETTIME rst) grst ON grst.parentid = rs.[$Id] 
                                         AND grst.rnk = 1
LEFT JOIN (SELECT rst.eventon,
                  rst.parentid,
                  CASE rst.startorend
                    WHEN 0 THEN 4
                    WHEN 1 THEN 1
                    ELSE NULL
                  END AS item_state_id
             FROM RANGESHEETTIME rst
            WHERE rst.startorend IN (0, 1)) x ON x.eventon = GRST.EventOn
                                             AND x.parentid = GRST.RangeSheet
LEFT JOIN RANGESHEETITEMSTATE rsis ON rsis.id = x.item_state_id

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

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