如何在SQL Server中为此逻辑编写CASE语句语法?

时间:2016-11-09 07:07:03

标签: sql sql-server tsql case

我正在使用SQL Server 2014,我的SQL数据库中有下表(称为NonRoomBundle),其摘录如下所示:

  ResaID       StayDate                  BundleDate                   BundleID
  50         NULL                       2016-03-03 00:00:00.000         123
  50         2016-02-28 00:00:00.000    NULL                              3
  50         2016-02-29 00:00:00.000    NULL                              3
  50         2016-03-01 00:00:00.000    NULL                              3
  50         2016-03-02 00:00:00.000    NULL                              3
  50         2016-03-03 00:00:00.000    NULL                              3
  50         2016-03-04 00:00:00.000    NULL                              3
  50         2016-03-05 00:00:00.000    NULL                              3
  50         2016-03-02 00:00:00.000    NULL                              3
  56         NULL                       2016-04-02 00:00:00.000         120
  56         2016-04-01 00:00:00.000    NULL                             25
  56         2016-04-02 00:00:00.000    NULL                             25
  56         2016-04-03 00:00:00.000    NULL                             25

BundleDate列显示BundleId应该为123的有效StayDate。

我需要编写一个查询,它将为我提供以下输出:

ResaID       StayDate                    BundleDate                   BundleID
  50         NULL                       2016-03-03 00:00:00.000         123
  50         2016-02-28 00:00:00.000    NULL                              3
  50         2016-02-29 00:00:00.000    NULL                              3
  50         2016-03-01 00:00:00.000    NULL                              3
  50         2016-03-02 00:00:00.000    NULL                              3
  50         2016-03-03 00:00:00.000    NULL                            123
  50         2016-03-04 00:00:00.000    NULL                            123
  50         2016-03-05 00:00:00.000    NULL                            123
  50         2016-03-02 00:00:00.000    NULL                            123
  56         NULL                       2016-04-02 00:00:00.000         120
  56         2016-04-01 00:00:00.000    NULL                             25
  56         2016-04-02 00:00:00.000    NULL                            120
  56         2016-04-03 00:00:00.000    NULL                            120

如上所示,BundleId从2016-03-02开始变为123'这是BundleDate。 我对SQL的一点知识告诉我一个CASE语句应该可以解决这个问题,但是我很难写它。我到目前为止:

SELECT StayDate
      ,BundleDate
      ,(CASE WHEN BundleDate >= StayDate THEN ....
   FROM NonRoomBundle

3 个答案:

答案 0 :(得分:0)

这就是你想要的吗?

with NonRoomBundle  (StayDate, BundleDate , BundleID)
as
(
select NULL as StayDate, '2016-03-02 00:00:00.000' as BundleDate , 123 as BundleID
union
select '2016-02-28 00:00:00.000' as StayDate, NULL as BundleDate , 3 as BundleID
union
select '2016-02-29 00:00:00.000' as StayDate, NULL as BundleDate , 3 as BundleID
union
select '2016-03-01 00:00:00.000' as StayDate, NULL as BundleDate , 3 as BundleID
union
select '2016-03-02 00:00:00.000' as StayDate, NULL as BundleDate , 3 as BundleID
union
select '2016-03-03 00:00:00.000' as StayDate, NULL as BundleDate , 3 as BundleID
union
select '2016-03-04 00:00:00.000' as StayDate, NULL as BundleDate , 3 as BundleID
union
select '2016-03-05 00:00:00.000' as StayDate, NULL as BundleDate , 3 as BundleID
)   

SELECT StayDate
,BundleDate
,(
 CASE 
WHEN StayDate < (select max(BundleDate ) from NonRoomBundle) THEN 3
else 123
END
)
FROM NonRoomBundle

答案 1 :(得分:0)

你可以试试这个:

declare @tblInput table (ResaID int, StayDate datetime, BundleDate datetime, BundleID int)

insert into @tblInput(ResaID, StayDate, BundleDate, BundleID)
values  (50, NULL, '2016-03-07', 123),
        (50,'2016-02-28', NULL, 3),
        (50,'2016-02-29', NULL, 3),
        (50,'2016-03-01', NULL, 3),
        (50,'2016-03-02', NULL, 3),
        (50,'2016-03-03', NULL, 3),
        (50,'2016-03-04', NULL, 3),
        (50,'2016-03-05', NULL, 3),
        (50,'2016-03-02', NULL, 3),
        (56,NULL, '2016-04-02', 120),
        (56,'2016-04-01', NULL, 25),
        (56,'2016-04-02', NULL, 25),
        (56,'2016-04-03', NULL, 25)

;WITH cte (ResaID, StayDate, BundleDate, BundleID)
as 
(
    select ResaID, StayDate, BundleDate, BundleId 
        from @tblInput 
    where BundleDate is not null
)
SELECT t1.ResaID
      ,t1.StayDate
      ,t1.BundleDate
      ,ISNULL(t2.BundleID, t1.BundleID) as BundleID
   FROM @tblInput t1
   left join cte t2 on t2.BundleDate <= ISNULL(t1.StayDate, GETDATE()) 
                       and t1.ResaID = t2.ResaID;

我所做的是:首先将值与包日期(锚行)分开,然后我使用您在on中提供的case子句与初始表连接条款。您可以根据自己的要求,将GETDATE()函数中的ISNULL替换为更可行的日期。

编辑:我已更新了代码。 EDIT2 :我已更新代码以反映您的需求。

答案 2 :(得分:0)

这可能看似令人费解,并且可能有另一种方式,而不是我提出的方法,但这是给这只猫皮肤的一种方法:

-h