[英]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
start_date
今天 ( dates
) 且end_date
為明天 ( end_date
)。 所以在上述情況下,結果不應該包括2021-04-22 | A01
2021-04-22 | A01
行但從第 2 行開始: 2021-04-23 | 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-小提琴:
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
Output:
日期 | Delivery_id | 產品 |
---|---|---|
2021-04-23 00:00:00.000 | A01 | 風俗 |
2021-04-23 00:00:00.000 | A03 | 普通的 |
2021-04-22 00:00:00.000 | A03 | 普通的 |
2021-04-24 00:00:00.000 | A03 | 普通的 |
2021-04-24 00:00:00.000 | A04 | 普通的 |
2021-04-22 00:00:00.000 | A04 | 普通的 |
2021-04-23 00:00:00.000 | A04 | 普通的 |
2021-04-22 00:00:00.000 | A05 | 普通的 |
db<小提琴在這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.