Select records based on column priority

前端 未结 4 1191
Happy的楠姐
Happy的楠姐 2021-01-23 06:37

First of all, the title of this question is horrible, but I didn\'t find a better way to describe my issue.

There\'s probably a very easy way to do this, but I couldn\'t

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-23 07:29

    I just want to offer that you can do this with a group by:

    select (case when sum(case when colour = 'Green' then 1 else 0 end) > 0
                 then max(case when colour = 'Green' then id end)
                 else max(case when colour = 'Red' then id end)
            end) as id,
           product_id
           (case when sum(case when colour = 'Green' then 1 else 0 end) > 0 then 'Green'
                 else 'Red'
            end) as colour
    from t
    group by product_id
    

提交回复
热议问题