想要从每个组中选择数据

时间:2014-03-11 10:38:22

标签: sql sql-server sql-server-2008

我想从Involvement列中仅包含Amanda和Dina的每个组(EID)中选择数据。 这是我的表数据

EID Ename           EPrice  Involvement
1   Breakfast           60  Amanda
1   Breakfast           60  Dina
2   Dinner              50  Nick
2   Dinner              50  Dina
2   Dinner              50  Amanda
2   Dinner              50  Jessica

我想要一个结果

EID Ename           EPrice  Involvement
1   Breakfast           60  Amanda
1   Breakfast           60  Dina

我不希望EID = 2的结果,因为它包含NICK和JESSICA

1 个答案:

答案 0 :(得分:3)

select * from your_table
where eid in
(
    select eid 
    from your_table
    group by eid
    having sum(case when involvement not in ('Amanda','Dina') then 1 else 0 end) = 0
)
and involvement in ('Amanda','Dina')