Group By Except For Certain Value

后端 未结 4 1019
清歌不尽
清歌不尽 2021-01-17 16:18

I\'m trying to create a (sqlite) query that will perform a GROUP BY but will not group anything with the value \'unknown\'. For example, I have the table:

i         


        
4条回答
  •  长发绾君心
    2021-01-17 17:00

    You can't easily do this with one statement but you can UNION the results of two statements

    • GROUP the list of all but unknown
    • Add (UNION) the list of all unknown

    SQL Statement

    SELECT MIN(id), name, parent_id, school_id
    FROM   YourTable
    WHERE  name <> 'unknown'
    GROUP BY
           name, parent_id, school_id
    UNION ALL
    SELECT id, name, parent_id, school_id
    FROM   YourTable
    WHERE  name = 'unknown'
    

    Note that I assume you have posted wrong unknown id's in your result

提交回复
热议问题