如果满足条件,SQL 将返回特定值

时间:2021-04-22 14:54:06

标签: sql sql-server-2008-r2

我有两张桌子:

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

我需要修改此查询以添加另一个条件,以便仅在产品为 end_date 和 {start_date 列中的 dates {1}} 是今天 (2021-04-22) 而 CUSTOM 是明天 (2021-04-23)。所以在上面的例子中,结果不应该包括 start_date 行而是从第二行开始:end_date

2 个答案:

答案 0 :(得分:0)

尝试将您的查询修改为此,看看它是否为您提供正确的数据:

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

答案 1 :(得分:0)

只需在 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

输出:

<头>
日期 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 正常

dbhere