Sql将记录与两列中的其他记录进行比较

Sql将记录与两列中的其他记录进行比较

问题描述:

这可能很简单(但我是初学者)。Sql将记录与两列中的其他记录进行比较

我有一个有以下的列名为Table_1表:

[id] 
[command] 
[verify_date] 
[data_interval_start] 
[data_interval_end] 

数据表里面看起来是这样的:

id command  verify_date data_interval_start data_interval_end 
-------------------------------------------------------------------- 
1 123456  2013-02-01 2013-05-01   2013-05-06 
2 234513  2012-05-02 2013-01-01   2013-03-01 
3 221342  2013-05-04 2011-01-01   2011-01-02 

我要检查,如果verify_date是每个data_interval_start之间和data_interval_end; 因此,请取第一个verify_date值,并在data_interval_startdata_interval_end之间的所有记录之间进行检查。

我试过这个查询,但没有结果:

SELECT command from Table_1 
WHERE verify_date IN (data_interval_start, data_interval_end) 


SELECT command from Table_1 
WHERE verify_date between data_interval_start AND data_interval_end 


SELECT command from Table_1 
WHERE verify_date = data_interval_start OR verify_date = data_interval_end 

任何帮助表示赞赏:)。

感谢

+0

所以是找到某一特定verify_date是全体之间的客观检查对(data_interval_start,data_interval_end) – 2013-03-26 10:57:36

+0

'BETWEEN'(第二个例子)应该适合你。在您的样本数据中,您没有任何满足条件的记录! – Prasanna 2013-03-26 11:00:07

+0

Jit B .....是.....例如:
验证如果2013年2月1日是所有之间,的(data_interval_start,data_interval_end)的对
接着,验证是否是2012-05-02所有之间时,双(data_interval_start,data_interval_end)等on .... – user2143407 2013-03-26 11:14:16

我猜你是比较两个字符串尝试将它们转换为DATE

 select command from Table_1 where convert(CHAR(10),verify_date,111) 
    between convert(CHAR(10),data_interval_start,111) AND convert(CHAR(10),data_interval_end,111) 

here how to convert dates

+0

echo_me之间,您的查询返回0行也.... – user2143407 2013-03-26 11:13:09

+0

你检查我更新的答案? – 2013-03-26 11:15:34

+0

选择转换(CHAR(10),data_interval_start,111)与转换(CHAR(10),data_interval_end,111)之间的转换(CHAR(10),verify_date,111) 的命令**返回0行** – user2143407 2013-03-26 11:21:42

假设你的日期是DATE type fields,您可以使用CASE如下;

select id, command, case when verify_date between 
           data_interval_start and data_interval_end 
         then 'Yes' 
         else 'No' 
        end isBetween 
from table1 

如果您的日期,然后字符串类型的字段让他们进入ISO formatyyyymmdd)并转换为DATE像下面的比较;

select id, command, case when convert(date,replace(verify_date,'-','')) between 
           convert(date,replace(data_interval_start,'-','')) 
           and convert(date,replace(data_interval_end,'-','')) 
         then 'Yes' 
         else 'No' 
        end isBetween 
from table1 

编辑:如果你需要比较一个给定的日期,你可以如下做到这一点。 (请注意,是包容之间。所以,如果你需要排除data_interval_startdata_interval_end,然后用<>运营商)

--say this is the date you need to compare your records with 
declare @verify_date date = '20130201' 

select id, command 
from table1 
where @verify_date between convert(date, data_interval_start) 
           and convert(date, data_interval_end) 

要为所有varify_date做,你可以使用一个cross join像下面。

Sql-Fiddle-Demo

select t1.id, t1.command, t2.verify_date 
from table1 t1 cross join table1 t2 
where t2.verify_date between convert(date, t1.data_interval_start) 
           and convert(date, t1.data_interval_end) 

| ID | COMMAND | VERIFY_DATE | 
------------------------------ 
| 2 | 234513 | 2013-02-01 | 
| 1 | 123456 | 2013-05-04 | 
+0

卡夫,但它是错的,verify_date 2013-02-01介于第二对记录之间:2013-01-01 2013-03-01 – user2143407 2013-03-26 11:19:21

+0

不......还是不适用于第二种解决方案。 – user2143407 2013-03-26 11:23:26

+0

您是否在寻找任何开始和结束日期之间的'verify_date',而不考虑它们的数据记录关系? – Kaf 2013-03-26 11:25:52

你的第二个查询会为你工作

SELECT command from Table_1 
WHERE verify_date between data_interval_start AND data_interval_end 

id command  verify_date data_interval_start data_interval_end 
-------------------------------------------------------------------- 
1 123456  2013-02-01 2013-05-01   2013-05-06 
2 234513  2012-05-02 2012-01-01   2013-03-01 
3 221342  2013-05-04 2011-01-01   2011-01-02 

现在上面的数据