Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据SQL中涉及多个日期的不同条件选择结果?_Sql_Sql Server_Sql Server 2017 - Fatal编程技术网

如何根据SQL中涉及多个日期的不同条件选择结果?

如何根据SQL中涉及多个日期的不同条件选择结果?,sql,sql-server,sql-server-2017,Sql,Sql Server,Sql Server 2017,我有一个类似于下面的表,其中包含项目名称、SoldDate(如果售出)和DisplayInWebsiteDate(如果未售出,则该项目应在网站中显示的日期) 我想根据以下条件选择项目: 1. If item is not Sold i.e SoldDate is NULL then check if its DisplayInWebsiteDate is greater than current date and that would be valid 2. If item is So

我有一个类似于下面的表,其中包含项目名称、SoldDate(如果售出)和DisplayInWebsiteDate(如果未售出,则该项目应在网站中显示的日期)

我想根据以下条件选择项目:

1. If item is not Sold i.e SoldDate is NULL then check if its DisplayInWebsiteDate is 
   greater than current date and that would be valid

2. If item is Sold i.e SoldDate has some date then ignore DisplayInWebsiteDate and check if that item was 
   sold within last 30 days, and display. If it was sold more than 30 days earlier, then don't get that record
今天的日期是2020-01-22,因此,结果如下

1. Shirt is VALID because it is not sold, and DisplayInWebsiteDate is greater than today's date
2. Pant is INVALID because it is sold, so we ignore its DisplayInWebsiteDate. 
   And its sold date has passed more than 30 days form today's date
3. Jacket is VALID because it was sold just 17 days ago i.e it falls within range of 30 days sold date
4. Trouser is INVALID because it is not sold and its DisplayInWebsiteDate has already passed on January 10, 2020
预期结果:

 Id      ItemName      IsSold       SoldDate        DisplayInWebsiteDate
 -------------------------------------------------------------------------
 1         Shirt         0            NULL              2020-03-28
 3         Jacket        1          2020-01-05          2020-01-20

这对你有用吗:

SELECT [Id], [ItemName],  [IsSold], [SoldDate], [DisplayInWebsiteDate]
FROM tbl
WHERE ([ISSOLD] <> 1 AND (CAST([DisplayInWebsiteDate] as date) >= CAST(GETDATE() as DATE))
       OR
       ([ISSOLD] = 1 AND DATEDIFF(day, [SoldDate], CAST(GETDATE() as DATE)) <= 30))
选择[Id]、[ItemName]、[IsSold]、[SoldDate]、[DisplayInWebsiteDate]
来自tbl
其中([IsSald]1和(强制转换([DisplayInWebsiteDate]作为日期)>=强制转换(GETDATE()作为日期))
或

([ISSOLD]=1和DATEDIFF(day[SoldDate],CAST(GETDATE()as DATE))通过以下语句,这应该非常简单:

CREATE TABLE t(
  ID int,
  ItemName nvarchar(100),
  IsSold int,
  SoldDate datetime,
  DisplayInWebsiteDate datetime
)

INSERT INTO t VALUES
(1, 'Shirt', 0, NULL, '2020-03-28')
,(2, 'Pant', 1, '2019-10-20', '2020-04-25')
,(3, 'Jacket', 1, '2020-01-05', '2020-01-20')
,(4, 'Trouser', 0, NULL, '2020-01-10');


SELECT *
  FROM t
  WHERE (SoldDate IS NULL AND DisplayInWebsiteDate > GETDATE())
    OR (SoldDate IS NOT NULL AND DateDiff(d, SoldDate, GETDATE()) <= 30)
创建表t(
ID int,
项目名称nvarchar(100),
IsSold int,
SoldDate日期时间,
DisplayInWebsiteDate日期时间
)
插入到t值中
(1,“衬衫”,0,空,“2020-03-28”)
,(2,'Pant',1,'2019-10-20','2020-04-25')
,(3,‘导管架’,1,‘2020-01-05’,‘2020-01-20’)
,(4,'裤子',0,空,'2020-01-10');
挑选*
从t
其中(SoldDate为NULL,并显示在WebSiteDate>GETDATE()

或者(SoldDate不为NULL,DateDiff(d,SoldDate,GETDATE())在这两种情况下都使用

SELECT *
FROM YourTable t
WHERE (
    (SoldDate IS NULL AND DisplayInWebsiteDate > CAST(GetDate() AS DATE))
    OR 
    (SoldDate >= DATEADD(day,-30, CAST(GetDate() AS DATE)))
)

还要指定预期结果。@jarlh我已更新了预期结果否,它不起作用,因为它正在检查所有项目的DisplayInWebsiteDate和今天的日期,因此不满足我的条件2mentioned@Vim请检查。更新了我的答案并添加了fiddler。@Vim不客气。:)顺便说一句,如果它是一个大表,并且在SoldDate上有一个索引,那么这个替代方案应该被证明是最快的。如果在SoldDate上没有索引,那么它可能并不重要。
SELECT *
FROM YourTable t
WHERE (
    (SoldDate IS NULL AND DisplayInWebsiteDate > CAST(GetDate() AS DATE))
    OR 
    (SoldDate >= DATEADD(day,-30, CAST(GetDate() AS DATE)))
)