SQL 如果满足条件则返回特定值
SQL returning specific value if a condition is met
我有两个表:
CREATE TABLE dates
(
dates DATETIME
);
INSERT INTO dates(dates)
VALUES
('2021-04-22 00:00:00.000'),
('2021-04-23 00:00:00.000'),
('2021-04-24 00:00:00.000')
CREATE TABLE deliveries
(
delivery_id VARCHAR(20),
product VARCHAR(10),
start_date DATETIME,
end_date DATETIME,
);
INSERT INTO deliveries(delivery_id, product, start_date, end_date)
VALUES
('A01', 'CUSTOM', '2021-04-22', '2021-04-23'),
('A02', 'CUSTOM', '2021-04-21', '2021-04-22'),
('A03', 'NORMAL', '2021-04-01', '2021-04-30'),
('A04', 'NORMAL', '2021-04-22', '2021-04-24'),
('A05', 'NORMAL', '2021-04-19', '2021-04-22'),
('A06', 'NORMAL', '2021-04-20', '2021-04-20')
这是我的查询:
declare @TodaysDate datetime = CONVERT(DATETIME, CONVERT(DATE, CURRENT_TIMESTAMP)) + '00:00:00.000'
declare @EndDate datetime = dateadd(day,5,@TodaysDate)
declare @TomorrowsDate datetime = dateadd(day,1,@TodaysDate)
select dt.dates, de.delivery_id, de.product
from dates dt left join deliveries de
on dt.dates between de.start_date and de.end_date
and not(de.end_date = @TodaysDate AND de.product LIKE '%CUSTOM%')
order by delivery_id
结果如下:
dates delivery_id product
2021-04-22T00:00:00Z A01 CUSTOM
2021-04-23T00:00:00Z A01 CUSTOM
2021-04-23T00:00:00Z A03 NORMAL
2021-04-24T00:00:00Z A03 NORMAL
2021-04-22T00:00:00Z A03 NORMAL
2021-04-22T00:00:00Z A04 NORMAL
2021-04-24T00:00:00Z A04 NORMAL
2021-04-23T00:00:00Z A04 NORMAL
2021-04-22T00:00:00Z A05 NORMAL
我需要修改此查询,以便在产品为 CUSTOM
和 start_date
是今天 (2021-04-22),end_date
是明天 (2021-04-23)。所以在上面的例子中,结果不应该包括 2021-04-22 | A01
行,而是从第二行开始: 2021-04-23 | A01
尝试修改您的查询,看看它是否为您提供正确的数据:
select
dt.dates,
de.delivery_id, de.product
from dates dt
left join deliveries de on dt.dates between de.start_date and de.end_date
and not(de.end_date = @TodaysDate AND de.product LIKE '%CUSTOM%')
and not(de.product LIKE '%CUSTOM%' AND dt.dates = @TodaysDate AND de.end_date = @TomorrowsDate)
order by de.delivery_id
只需在 where 子句中添加以下条件:
and not ( start_date = @TodaysDate and end_date =@TomorrowsDate AND de.product LIKE '%CUSTOM%'
and dates=start_date)
DB-Fiddle:
CREATE TABLE dates
(
dates DATETIME
);
INSERT INTO dates(dates)
VALUES
('2021-04-22 00:00:00.000'),
('2021-04-23 00:00:00.000'),
('2021-04-24 00:00:00.000')
CREATE TABLE deliveries
(
delivery_id VARCHAR(20),
product VARCHAR(10),
start_date DATETIME,
end_date DATETIME,
);
INSERT INTO deliveries(delivery_id, product, start_date, end_date)
VALUES
('A01', 'CUSTOM', '2021-04-22', '2021-04-23'),
('A02', 'CUSTOM', '2021-04-21', '2021-04-22'),
('A03', 'NORMAL', '2021-04-01', '2021-04-30'),
('A04', 'NORMAL', '2021-04-22', '2021-04-24'),
('A05', 'NORMAL', '2021-04-19', '2021-04-22'),
('A06', 'NORMAL', '2021-04-20', '2021-04-20')
GO
查询:
declare @TodaysDate datetime = CONVERT(DATETIME, CONVERT(DATE, CURRENT_TIMESTAMP)) + '00:00:00.000'
declare @EndDate datetime = dateadd(day,5,@TodaysDate)
declare @TomorrowsDate datetime = dateadd(day,1,@TodaysDate)
select dt.dates, de.delivery_id, de.product
from dates dt left join deliveries de
on dt.dates between de.start_date and de.end_date
and not(de.end_date = @TodaysDate AND de.product LIKE '%CUSTOM%')
and not ( start_date = @TodaysDate and end_date =@TomorrowsDate AND de.product LIKE '%CUSTOM%'
and dates=start_date)
order by delivery_id
输出:
dates
delivery_id
product
2021-04-23 00:00:00.000
A01
CUSTOM
2021-04-23 00:00:00.000
A03
NORMAL
2021-04-22 00:00:00.000
A03
NORMAL
2021-04-24 00:00:00.000
A03
NORMAL
2021-04-24 00:00:00.000
A04
NORMAL
2021-04-22 00:00:00.000
A04
NORMAL
2021-04-23 00:00:00.000
A04
NORMAL
2021-04-22 00:00:00.000
A05
NORMAL
dbhere
我有两个表:
CREATE TABLE dates
(
dates DATETIME
);
INSERT INTO dates(dates)
VALUES
('2021-04-22 00:00:00.000'),
('2021-04-23 00:00:00.000'),
('2021-04-24 00:00:00.000')
CREATE TABLE deliveries
(
delivery_id VARCHAR(20),
product VARCHAR(10),
start_date DATETIME,
end_date DATETIME,
);
INSERT INTO deliveries(delivery_id, product, start_date, end_date)
VALUES
('A01', 'CUSTOM', '2021-04-22', '2021-04-23'),
('A02', 'CUSTOM', '2021-04-21', '2021-04-22'),
('A03', 'NORMAL', '2021-04-01', '2021-04-30'),
('A04', 'NORMAL', '2021-04-22', '2021-04-24'),
('A05', 'NORMAL', '2021-04-19', '2021-04-22'),
('A06', 'NORMAL', '2021-04-20', '2021-04-20')
这是我的查询:
declare @TodaysDate datetime = CONVERT(DATETIME, CONVERT(DATE, CURRENT_TIMESTAMP)) + '00:00:00.000'
declare @EndDate datetime = dateadd(day,5,@TodaysDate)
declare @TomorrowsDate datetime = dateadd(day,1,@TodaysDate)
select dt.dates, de.delivery_id, de.product
from dates dt left join deliveries de
on dt.dates between de.start_date and de.end_date
and not(de.end_date = @TodaysDate AND de.product LIKE '%CUSTOM%')
order by delivery_id
结果如下:
dates delivery_id product
2021-04-22T00:00:00Z A01 CUSTOM
2021-04-23T00:00:00Z A01 CUSTOM
2021-04-23T00:00:00Z A03 NORMAL
2021-04-24T00:00:00Z A03 NORMAL
2021-04-22T00:00:00Z A03 NORMAL
2021-04-22T00:00:00Z A04 NORMAL
2021-04-24T00:00:00Z A04 NORMAL
2021-04-23T00:00:00Z A04 NORMAL
2021-04-22T00:00:00Z A05 NORMAL
我需要修改此查询,以便在产品为 CUSTOM
和 start_date
是今天 (2021-04-22),end_date
是明天 (2021-04-23)。所以在上面的例子中,结果不应该包括 2021-04-22 | A01
行,而是从第二行开始: 2021-04-23 | A01
尝试修改您的查询,看看它是否为您提供正确的数据:
select
dt.dates,
de.delivery_id, de.product
from dates dt
left join deliveries de on dt.dates between de.start_date and de.end_date
and not(de.end_date = @TodaysDate AND de.product LIKE '%CUSTOM%')
and not(de.product LIKE '%CUSTOM%' AND dt.dates = @TodaysDate AND de.end_date = @TomorrowsDate)
order by de.delivery_id
只需在 where 子句中添加以下条件:
and not ( start_date = @TodaysDate and end_date =@TomorrowsDate AND de.product LIKE '%CUSTOM%'
and dates=start_date)
DB-Fiddle:
CREATE TABLE dates
(
dates DATETIME
);
INSERT INTO dates(dates)
VALUES
('2021-04-22 00:00:00.000'),
('2021-04-23 00:00:00.000'),
('2021-04-24 00:00:00.000')
CREATE TABLE deliveries
(
delivery_id VARCHAR(20),
product VARCHAR(10),
start_date DATETIME,
end_date DATETIME,
);
INSERT INTO deliveries(delivery_id, product, start_date, end_date)
VALUES
('A01', 'CUSTOM', '2021-04-22', '2021-04-23'),
('A02', 'CUSTOM', '2021-04-21', '2021-04-22'),
('A03', 'NORMAL', '2021-04-01', '2021-04-30'),
('A04', 'NORMAL', '2021-04-22', '2021-04-24'),
('A05', 'NORMAL', '2021-04-19', '2021-04-22'),
('A06', 'NORMAL', '2021-04-20', '2021-04-20')
GO
查询:
declare @TodaysDate datetime = CONVERT(DATETIME, CONVERT(DATE, CURRENT_TIMESTAMP)) + '00:00:00.000'
declare @EndDate datetime = dateadd(day,5,@TodaysDate)
declare @TomorrowsDate datetime = dateadd(day,1,@TodaysDate)
select dt.dates, de.delivery_id, de.product
from dates dt left join deliveries de
on dt.dates between de.start_date and de.end_date
and not(de.end_date = @TodaysDate AND de.product LIKE '%CUSTOM%')
and not ( start_date = @TodaysDate and end_date =@TomorrowsDate AND de.product LIKE '%CUSTOM%'
and dates=start_date)
order by delivery_id
输出:
dates | delivery_id | product |
---|---|---|
2021-04-23 00:00:00.000 | A01 | CUSTOM |
2021-04-23 00:00:00.000 | A03 | NORMAL |
2021-04-22 00:00:00.000 | A03 | NORMAL |
2021-04-24 00:00:00.000 | A03 | NORMAL |
2021-04-24 00:00:00.000 | A04 | NORMAL |
2021-04-22 00:00:00.000 | A04 | NORMAL |
2021-04-23 00:00:00.000 | A04 | NORMAL |
2021-04-22 00:00:00.000 | A05 | NORMAL |
db