如何select根据涉及多个日期的不同条件得出SQL中的结果?
How to select the results based on different conditions involving multiple dates in SQL?
我有一个 table 的项目类似于以下内容,其中包含项目名称、SoldDate(如果已售出)和 DisplayInWebsiteDate(如果该项目未售出,则应在网站上显示该日期)
Id ItemName IsSold SoldDate DisplayInWebsiteDate
-------------------------------------------------------------------------
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 项目基于以下条件:
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))
SQL提琴手是here
通过以下语句这应该非常简单:
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)
但是,您可以将日期等参数化...但基本上我认为就是这样。
详见SQLFiddle:http://sqlfiddle.com/#!18/36538/4/1
对这两种情况都使用 OR
应该可以。
SELECT *
FROM YourTable t
WHERE (
(SoldDate IS NULL AND DisplayInWebsiteDate > CAST(GetDate() AS DATE))
OR
(SoldDate >= DATEADD(day,-30, CAST(GetDate() AS DATE)))
)
我有一个 table 的项目类似于以下内容,其中包含项目名称、SoldDate(如果已售出)和 DisplayInWebsiteDate(如果该项目未售出,则应在网站上显示该日期)
Id ItemName IsSold SoldDate DisplayInWebsiteDate
-------------------------------------------------------------------------
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 项目基于以下条件:
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))
SQL提琴手是here
通过以下语句这应该非常简单:
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)
但是,您可以将日期等参数化...但基本上我认为就是这样。
详见SQLFiddle:http://sqlfiddle.com/#!18/36538/4/1
对这两种情况都使用 OR
应该可以。
SELECT *
FROM YourTable t
WHERE (
(SoldDate IS NULL AND DisplayInWebsiteDate > CAST(GetDate() AS DATE))
OR
(SoldDate >= DATEADD(day,-30, CAST(GetDate() AS DATE)))
)