Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL to list out duplicate value within different values excluding a combination

I am trying to write a query in Oracle SQL where same order might have multiple modes and I want list out the orders which only has T and I mode including duplicates. If order is duplicate which has mode O and T both then I want to exclude them it should not come it output. For example in the below table, AAWER order has duplicate values with mode O in it, SQL out put should exclude complete AAWER having O and T mode.

Output should be only TTRTW / RRRRE & RRRRE.

Sample data:

Order Mode
AAWER O
AAWER T
TTRTW T
RRRRE T
RRRRE I

Output I want as

ORDER_ID ORDER_MODE
TTRTW T
RRRRE T
RRRRE I
SELECT "Order", "Mode"
FROM your_table
WHERE "Order" IN (
    SELECT "Order"
    FROM your_table
    GROUP BY "Order"
    HAVING 
        COUNT(DISTINCT CASE WHEN "Mode" IN ('T', 'I') THEN "Mode" END) >= 1
        AND COUNT(DISTINCT CASE WHEN "Mode" NOT IN ('T', 'I') THEN "Mode" END) = 0
)
ORDER BY "Order";

Getting error in line 4

like image 466
Jazzzzzzzzz Avatar asked Aug 31 '25 04:08

Jazzzzzzzzz


1 Answers

select "Order","Mode"
  from your_table a
where not exists(select 1
  from your_table b
where b."Mode" not in ('T','I')
and b."Order"=a."Order");

  1. First find all orders which have Mode other than T and I
  2. Check the orders which are not in above list
like image 68
Mahesh Pawar Avatar answered Sep 02 '25 18:09

Mahesh Pawar