Select 日期之间的所有行

Select all rows between dates

我有一个 #tmp table,如下所示。一共三行。

title receipt_date decision_date
"It wasn't as bad as I thought it would be" 2017-06-12 15:07:10.893 2017-06-23 09:37:31.667
"It wasn't as bad as I thought it would be" 2017-07-11 10:35:24.337 2018-06-25 05:54:41.133
"It wasn't as bad as I thought it would be" 2017-09-25 14:06:18.670 2017-11-21 05:13:08.563

代码应该遍历三行中的每一行,并且(基于当前迭代行)select 整个 table 中的所有行,其中 receive_date 介于receive_datedecision_date 在任何其他行。

注意:我专门使用了 greater-than 和 less-than(相对于 greater-than-or-equal),这样该行就不会自我 select.

注意:2017-09-25 14:06:18.670 晚于 2017-07-11 10:35:24.337 但早于 2018-06-25 05:54:41.133,因此该行应该 return 在 select 中。但是我从 select.

中什么也得不到

我是运行SSMS中的代码

代码

SET @Cur2 = CURSOR FOR
  SELECT title,receipt_date,decision_date  
From #tmpTable
GROUP BY title,receipt_date,decision_date 
ORDER BY receipt_date

OPEN @Cur2
FETCH NEXT FROM @Cur2 INTO @TITLE,@RECEIPT_DATE,@DECISION_DATE;
WHILE @@FETCH_STATUS = 0
BEGIN

  select *  
  from #tmpTable
  where receipt_date > @RECEIPT_DATE 
  AND   receipt_date < @DECISION_DATE

  FETCH NEXT FROM @Cur2 INTO @RECEIPT_DATE,@DECISION_DATE;
END;

破译你的问题并不容易。你应该发布一个合适的example!但据我所知,听起来你可以简单地使用 EXISTS 和一个相关的子查询。

SELECT *
       FROM #tmp t1
       WHERE EXISTS (SELECT *
                            FROM #tmp t2
                            WHERE t2.receipt_date < t1.receipt_date
                                  AND t2.decision_date >= t1.receipt_date);

db<>fiddle