在where子句中使用case表达式

时间:2010-09-01 15:24:23

标签: sql case where-clause

虽然我知道这不是很有效,但我需要让它快速运行,所以我尝试遵循最简单的路径,或者至少我是这么认为的。我试图从实体中获取最大数据并仅考虑USER不为空的行,并且如果没有行将USER设置为null,那么我将考虑空值。

实体的结构如下:

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

因此,查询如下:

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

我遇到问题的情况是GRST视图中的那个。 我该怎么做才能达到我的要求?

3 个答案:

答案 0 :(得分:2)

我对你正在做的事感到有点困惑。关闭你的文字描述会有类似下面的帮助吗?

;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

答案 1 :(得分:1)

我认为你只需要加入两个不同的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

如果所有ParentID行在“用户”列中都为空,则此子查询不会返回一行。

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

现在只需选择您想要的值。

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

答案 2 :(得分:1)

以下是我对您的查询的重写:

   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