SQL語句實現按照分組劃分優先級

SQL語句實現按照分組劃分優先級

-------分組劃分優先級
SELECT B.*
  FROM (SELECT A.color,
               A.P,
               row_number() over(partition by P order by color desc) rn
          FROM (SELECT 'red' AS color, '3' AS P
                  FROM DUAL
                UNION
                SELECT 'red' AS color, '3' AS P
                  FROM DUAL
                UNION
                SELECT 'black' AS color, '3' AS P
                  FROM DUAL
                UNION
                SELECT 'yellow' AS color, '2' AS P
                  FROM DUAL
                UNION
                SELECT 'green' AS color, '2' AS P
                  FROM DUAL
                UNION
                SELECT 'blue' AS color, '1' AS P FROM DUAL) A) B
 WHERE rn < 2

結果截圖:spa

相關文章
相關標籤/搜索