0

我在 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

我想找到具有相应字段 2 值“53”、“54”和“55”的所有字段 1 值。在同一日期内,因此输出应如下所示:

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
4

5 回答 5

1

我相信这应该工作?你可能过于复杂了,或者我误解了这个问题

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

这是 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

这是 SQLFiddle

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

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

另一个小提琴

于 2012-06-28T18:07:24.080 回答
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 
于 2012-06-28T19:39:18.917 回答
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
于 2012-06-28T18:04:13.047 回答
0

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

SELECT * FROM table1
WHERE field2 in ('53', '54', '55');
于 2012-06-28T18:04:37.750 回答
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
于 2012-06-28T18:18:22.423 回答