我試圖創建一個(sqlite)查詢,它將執行GROUP BY,但不會將任何值與「unknown」組合在一起。例如,我有表:除了某些值以外的組
id | name | parent_id | school_id |
1 | john | 1 | 1 |
2 | john | 1 | 1 |
3 | john | 1 | 1 |
4 | nick | 2 | 2 |
5 | nick | 2 | 2 |
6 | nick | 3 | 3 |
7 | bob | 4 | 4 |
8 | unknown | 5 | 5 |
9 | unknown | 5 | 5 |
10| unknown | 5 | 5 |
與正確的查詢「GROUP BY姓名,PARENT_ID,學校ID」我需要以下行返回:
id | name | parent_id | school_id |
1 | john | 1 | 1 |
3 | nick | 2 | 2 |
4 | nick | 3 | 3 |
5 | bob | 4 | 4 |
6 | unknown | 5 | 5 |
7 | unknown | 5 | 5 |
8 | unknown | 5 | 5 |
任何幫助將不勝感激。謝謝!
答案添加避免GROUP BY和解析表兩次的成本。 – MatBailie 2011-12-22 23:59:04