Remove duplicates from first column in Select (not Distinct) in Oracle

I am new to Oracle.

I have Select as part of a larger query that returns the result below. My problem is that for the first column I only need the first appearance of each value (they are sorted in ascending order). The result should look like what you get when you delete duplicates in Excel (the distinguishing element does not work here, I think, since I still need other rows separately).

Can someone tell me how I can achieve this?

My request (abbreviated):

Select
   p.ID
   , p.SUB_ID
   , p.ITEM_NAME AS ITEM
   , p.ITEM_PRICE AS PRICE
   /* ... */
FROM
   PRICE_LIST p
WHERE
   /* ... */
GROUP BY
   p.ID
   , p.SUB_ID
   , p.ITEM_NAME
   , p.ITEM_PRICE
   /* ... */
ORDER BY
   p.ID, p.SUB_ID

Current result:

enter image description here

Required Result:

enter image description here

+4
source share
2

. , :

WITH cte AS (
    SELECT DISTINCT
        p.ID, p.SUB_ID, p.ITEM_NAME AS ITEM, p.ITEM_PRICE AS PRICE,
        ROW_NUMBER() OVER (PARTITION BY p.ID ORDER BY p.SUB_ID) rn
    FROM PRICE_LIST p
    WHERE
    /* ... */
)
SELECT
    CASE WHEN t.rn = 1 THEN TO_CHAR(t.ID) ELSE '' END AS ID,
    t.SUB_ID, t.ITEM, t.PRICE
FROM cte t
ORDER BY
    t.ID,
    t.SUB_ID

Gordon:

/CTE , ROW_NUMBER() CASE:

SELECT DISTINCT
    CASE WHEN ROW_NUMBER() OVER (PARTITION BY p.ID ORDER BY p.SUB_ID) = 1
         THEN TO_CHAR(p.ID) ELSE '' END AS ID,
    p.SUB_ID, p.ITEM_NAME AS ITEM, p.ITEM_PRICE AS PRICE
FROM PRICE_LIST p
WHERE
    /* ... */
ORDER BY
    p.ID,
    p.SUB_ID
+5
    SELECT 
      DECODE(RN, 1, ID, '') AS ID
    , SUB_ID
    , ITEM
    , PRICE
    , RN 
FROM (
    Select
       p.ID
       , p.SUB_ID
       , p.ITEM_NAME AS ITEM
       , p.ITEM_PRICE AS PRICE
       /* ... */
       , ROW_NUMBER() OVER(PARTITION BY P.ID ORDER BY P.SUB_ID) AS RN
    FROM
       PRICE_LIST p
    WHERE
       /* ... */
    GROUP BY
       p.ID
       , p.SUB_ID
       , p.ITEM_NAME
       , p.ITEM_PRICE
       /* ... */
    ORDER BY
       p.ID, p.SUB_ID
   )
0

Source: https://habr.com/ru/post/1679544/


All Articles