2012-02-11 75 views
0

我有這個問題與查詢這兩個表之間沒有任何聯繫,我試圖結合。表1的有效期和截止日必須與PollDate的表2相關聯。 PollDate不得位於有效期和到期日之間。SQL Query從2個不同的表中獲取到期日期和生效日期之間的日期

Table 1 
ClientID EffectiveDate  ExpirationDate 
1  2009-04-01 00:00:00.000 2009-12-18 00:00:00.000 
1  2010-02-12 00:00:00.000 2010-03-05 00:00:00.000 
1  2010-05-18 00:00:00.000 NULL 
1  2009-12-21 00:00:00.000 2010-02-08 00:00:00.000 
1  2010-12-19 00:00:00.000 2009-12-20 00:00:00.000 

Table 2 
ClientID PollDate 
1  2009-12-20 00:00:00.000 
1  2009-12-19 00:00:00.000 
1  2010-02-12 00:00:00.000 
1  2010-02-27 00:00:00.000 
1  2010-05-19 00:00:00.000 
1  2010-05-29 00:00:00.000 
1  2010-05-30 00:00:00.000 
1  2010-05-31 00:00:00.000 
1  2010-06-05 00:00:00.000 
1  2010-06-25 00:00:00.000 
1  2010-06-27 00:00:00.000 
1  2010-07-02 00:00:00.000 
1  2010-08-04 00:00:00.000 
1  2010-08-20 00:00:00.000 

Result 
ClientID inValidDate 
1  2009-12-20 00:00:00.000 
1  2009-12-19 00:00:00.000  
+0

有點困惑「..他們沒有有他們之間的任何聯繫「他們不在同一個數據庫?你在尋找某種RPC選項嗎? – 2012-02-11 00:56:10

+0

不,他們在同一個數據庫中。我的意思是說,這兩個表都沒有任何引用,即:外鍵。兩個表的共同統治是clientID。 – n00bs 2012-02-13 15:14:48

回答

0

的每一行匹配下面是一個輕微的變化@ AJP的結果 - 在ExpirationDate中只佔NULL值:

CREATE TABLE #Table1 
(
    [ClientID] INT, 
    [EffectiveDate] DATETIME, 
    [ExpirationDate] DATETIME 
) 

INSERT INTO #Table1 
(
    [ClientID], 
    [EffectiveDate], 
    [ExpirationDate] 
) 
SELECT 1, '2009-04-01', '2009-12-18' UNION 
SELECT 1, '2010-02-12', '2010-03-05' UNION 
SELECT 1, '2010-05-18', NULL UNION 
SELECT 1, '2009-12-21', '2010-02-08' UNION 
SELECT 1, '2010-12-19', '2009-12-20' 

CREATE TABLE #Table2 
(
    [ClientID] INT, 
    [PollDate] DATETIME 
) 

INSERT INTO #Table2 
(
    [ClientID], 
    [PollDate] 
) 
SELECT 1, '2009-12-20' UNION 
SELECT 1, '2009-12-19' UNION 
SELECT 1, '2010-02-12' UNION 
SELECT 1, '2010-02-27' UNION 
SELECT 1, '2010-05-19' UNION 
SELECT 1, '2010-05-29' UNION 
SELECT 1, '2010-05-30' UNION 
SELECT 1, '2010-05-31' UNION 
SELECT 1, '2010-06-05' UNION 
SELECT 1, '2010-06-25' UNION 
SELECT 1, '2010-06-27' UNION 
SELECT 1, '2010-07-02' UNION 
SELECT 1, '2010-08-04' UNION 
SELECT 1, '2010-08-20' 

SELECT 
    t2.[ClientID], 
    t2.[PollDate] AS 'inValidDate' 
FROM 
    #Table1 AS t1 
JOIN 
    #Table2 AS t2 
ON 
    (t2.[PollDate] < t1.[EffectiveDate] 
OR t2.[PollDate] > ISNULL(t1.[ExpirationDate], '9999-12-31')) 
AND t1.ClientID = t2.ClientID -- Not clear from your question if this is necessary 
+0

如果您想在'ExpirationDate'中包含所有'NULL'值,可以使用't2。[PollDate]> ISNULL(t1。[ExpirationDate],'1753- 01-01'))'而不是 – diaho 2012-02-11 00:45:21

+0

感謝diaho,這是需要的。您的查詢會在有效日期和到期日期內生成所有匹配PollDate的列表。我需要添加的是他們都是空的地方,因此排除不在兩個日期之間的PollDates。 – n00bs 2012-02-13 16:51:29

0

不確定關於syntext,但你想做這樣的事情。

select clientID, polDate as 'inValidDate' 
FROM Table1 t1 
INNER JOIN Table2 t2 
ON t2.PolDate not in between t1.EffectiveDate and t1.ExpirationDate 

編輯: 假設,如果到期日爲空意味着政策將永不過期。

select clientID, polDate as 'inValidDate' 
FROM Table1 t1 
INNER JOIN Table2 t2 
ON t2.PolDate not in between t1.EffectiveDate and ISNULL(t1.ExpirationDate, '2999-01-01') 
0

u能嘗試:

select t2.ClientID, t2.PoolDate 
from Table1 t1, Table2 t2 
where t2.PollDate between t1.EffectiveDate and t1.ExpirationDate 

,因爲ü沒有把在連接條件,表1中的每一行將與表2

+0

我以爲他說「PollDate不能在有效期和到期日之間」。是不是你的查詢會導致之間? – AJP 2012-02-11 00:38:59

+0

,但問題的示例結果顯示「無效日期」 – vicker313 2012-02-11 00:44:48

相關問題