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