2013-03-26 86 views
0

這可能很簡單(但我是初學者)。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

回答

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

+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

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 | 
+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

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 

現在上面的數據

相關問題