SQL Server查询内部联接查询不起作用

时间:2012-06-28 18:00:49

标签: sql-server

我在sql server 2005中有一个类似于下面的表

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'和'55'的field1值。在同一天内,输出应如下:

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

5 个答案:

答案 0 :(得分: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

或者,如果您只需要53-55中的所有列,只需添加一个WHERE:

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

Another Fiddle

答案 1 :(得分: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 ID。外部查询只选择那些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

答案 2 :(得分:0)

你只有一张桌子(桌子1),对吗?如果是这样,则不需要加入:

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

答案 3 :(得分: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

答案 4 :(得分: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