2012-06-28 192 views
0

我也有類似的表下面SQL Server 2005中SQL Server查詢內部聯接查詢不起作用

date_column | field1 | field2 
1 June 2012 | xyz | 53 
1 June 2012 | abc | 87 
2 June 2012 | xyz | 81 
3 June 2012 | xyz | 54 
3 June 2012 | abc | 53 
3 June 2012 | abc | 54 
4 June 2012 | mmn | 53 
4 June 2012 | xyz | 54 
4 June 2012 | mmn | 54 
4 June 2012 | mmn | 55 
3 June 2012 | abc | 55 
3 June 2012 | adf | 86 
3 June 2012 | asd | 33 

我想找到具有相應FIELD2值「53」,「54」所有的字段1的值和'55'。同日起這樣的輸出應該如下:

date_column | field1 | field2 
3 June 2012 | abc | 53 
3 June 2012 | abc | 54 
3 June 2012 | abc | 55 
4 June 2012 | mmn | 53 
4 June 2012 | mmn | 54 
4 June 2012 | mmn | 55 

我想下面的SQL代碼內加入,但它不工作

select date_column, field1, field2 from table1 
inner join (select date_column, field1, field2 from table1 where field2 
in ('54', '55')) as table2 
on table1.date_column = table2.date_column and 
table1.field1 = table2.field1 
where field1 in ('53', '54', '55') 
group by date_column, field1, field2 
order by date_column, field1, field2 
+3

它是如何不工作? –

+0

你是否得到一個含糊不清的列錯誤? – HLGEM

回答

1

我相信這應該工作?你可能會過於複雜這還是我誤解了問題

SELECT field1, date_column FROM table1 
WHERE field2 in ('53', '54', '55') 
GROUP BY field1, date_column 
HAVING COUNT(DISTINCT field2) = 3 

Here is the SQLFiddle

你說你只需要找到field1的,所以這應該工作,但是如果你需要的所有列,那麼你可以這樣做:

SELECT Table1.* 
FROM Table1 
JOIN 
(
SELECT field1, date_column FROM table1 
WHERE field2 in ('53', '54', '55') 
GROUP BY field1, date_column 
HAVING COUNT(DISTINCT field2) = 3 
) AS MoreThan1 
ON Table1.field1 = MoreThan1.field1 and Table1.date_column = MoreThan1.date_column 

Here is the SQLFiddle

OR,只需添加一個WHERE如果你只是想爲那些在53-55的所有列:

WHERE Table1.field2 in ('53', '54', '55') 

Another Fiddle

+0

感謝賈斯汀...但我得到「0」行受到影響 – user1449596

+0

@ user1449596隨着小提琴?我知道你的確切預期的結果 –

+0

感謝賈斯汀......它的工作......我終於可以有輸出,我想我想要的方式... 您的代碼以及凱文代碼顯示相同的輸出。 – user1449596

0

我認爲下面你想要做什麼:

select * 
from t 
where field1 in (select field1 
       from t 
       where field2 in ('53', '54', '55') 
       group by field1 
       having max(date) = min(date) 
       ) 

子查詢查找與您的條件匹配的field1 ids。外部查詢只是選擇這些ID的所有數據。

我想我最初誤讀了這個要求。你想要任何日期這是真實的。這個版本應該這樣做:

select * 
from t join 
    (select field1, date 
     from t 
     where field2 in ('53', '54', '55') 
     group by field1 
     having count(distinct field2) = 3 
    ) a 
    on t.field1 = a.field1 and t.date = a.date 
+0

謝謝戈登 此查詢不起作用。請注意我的日期列是文本數據/類型。 – user1449596

+0

你能解釋它是如何不起作用的嗎?日期列的類型無關緊要。 –

+0

嗨戈登,我不知道確切,但您的查詢顯示field2不是53或54或55 ..它只顯示剩餘的field2值的記錄。 – user1449596

0

你只有1個表格(table1),是否正確?如果是這樣,不需要連接:

SELECT * FROM table1 
WHERE field2 in ('53', '54', '55'); 
+0

這是行不通的,因爲它顯示它具有所有FIELD2記錄無論是「53」或「54」或「55」或兩者... 我會盡量賈斯汀和更新很快給出的查詢。 – user1449596

+0

不確定你的意思?你可以GROUP BY日期..?我只是指出,有1個表,不需要連接。 – blearn

0
SELECT DISTINCT t1.date_column, t1.field1, t1.field2 
FROM table1 t1 
WHERE EXISTS 
    (SELECT t2.date_column, t2.field1 
    FROM table1 t2 
    WHERE t2.field2 IN ('53', '54', '55') 
     AND t2.date_column = t1.date_column 
     AND t2.field1 = t1.field1 
    GROUP BY t2.date_column, t2.field1 
    HAVING COUNT(DISTINCT field2) = 3) 
ORDER BY t1.date_column, t1.field1, t1.field2 
+0

感謝凱文 但是這個代碼也不起作用......請注意,值53每個field1值的每個日期會出現幾次,但它只能有一個'53','54'和'55'。 – user1449596

+0

我不確定我瞭解「它只能有'53','54'和'55'」。我已經更新了查詢以允許重複行,所以再試一次。你是這個意思嗎? –

+0

謝謝凱文。你的新代碼終於爲我工作。 即使賈斯汀新代碼工作。非常感謝每一個人。 – user1449596

0
select table1.date_column, table1.field1, table1.field2 
from table1 
inner join (select date_column, field1, field2 
      from table1 
      where field2 in ('54', '55')) as table2 
    on table1.date_column = table2.date_column and table1.field1 = table2.field1 
where table1.field1 in ('53', '54', '55') 
group by table1.date_column, table1.field1, table1.field2 
order by table1.date_column, table1.field1, table1.field2