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

时间:2013-03-26 10:52:22

标签: sql sql-server-2008

这对你来说可能很简单(但我是初学者)。

我有一个名为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_datedata_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

感谢任何帮助:)。

由于

3 个答案:

答案 0 :(得分:1)

假设您的日期为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 |

答案 1 :(得分:0)

我猜您正在比较两个字符串,尝试将它们转换为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

答案 2 :(得分:0)

您的第二个查询对您有用

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 

现在查看以上数据