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
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With