Select records based on column priority

前端 未结 4 1152
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:11

    You can have it like this

    WITH PriorityTable
    AS
    (
        SELECT T.*,
            ROW_NUMBER() OVER (PARTITION BY T.ID
                                ORDER BY PT.ColorPriority )  PriorityColumn
        FROM XX AS T
        INNER JOIN (
            SELECT 'RED' AS f_COLOUR , 1 AS ColorPriority
            UNION
            SELECT 'GREEN' AS f_COLOUR , 2 AS ColorPriority
        ) AS PT
            ON T.f_COLOUR  = PT.f_COLOUR 
    )
    
    SELECT * FROM PriorityTable
    WHERE PriorityColumn = 1
    

提交回复
热议问题