2016-08-19 6 views
1

これはおそらく単純なSQLクエリです。私はSQLを書いている間はしばらくしているので、ややこしいと思っています。複数の行に基づくデータのフィルタリングSQL

ID NAME  VALUE 
--- ------  ------- 
1 Country  Brazil 
1 Country  India 
2 Country  US 
2 EmpLevel 1 
3 EmpLevel 3 

疑似クエリ: -

(2 '米国' とEmpLevel '1' として国を持っているIDを持つレコードが同様)

Select * 
from table_name 
where (country = US or country = Brazil) 
    and (Employee_level = 1 or Employee_level = 3) 

このクエリは、

ID NAME  VALUE 
--- ------  ------- 
2  Country  US 
2  EmpLevel  1 

を返す必要があります

私はカップルのSOの投稿にも行った。

Multiple row SQL Where clause

SQL subselect filtering based on multiple sub-rows

Evaluation of multiples 'IN' Expressions in 'WHERE' clauses in mysql

答えて

1

私はあなたがcountryのために期待される結果がUSの代わりBrazilあるべきだと仮定します。ここでconditional aggregationjoinを使用して一つの選択肢です:

select y.* 
from yourtable y join (
    select id 
    from yourtable 
    group by id 
    having max(case when name = 'Country' then value end) in ('US','Brazil') and 
     max(case when name = 'EmpLevel' then value end) in ('1','3') 
) y2 on y.id = y2.id 
+0

少し遅れて、:)はい、コメントごとに質問を更新しました – mlg

関連する問題