Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CASE WHEN – Multiple Conditions – Over Multiple Rows of same reference

I am trying to create a flag by using CASE WHEN to determine something but it keeps returning "single_state" – I guess because its not applying the CASE WHEN to all rows with o/n 1234 which is the bit I need help with.

My expected output is to have "multi_state" for the first 3 lines as they are all the same o/n and "single_state" for the last line as its neither grouped or got more than 1 state.

Example below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

DATA Table

o/n      sku     order_type    state   state_check
1234    ABC987    Grouped     express
1234    BCD654    Grouped     arrived
1234    CDF321    Grouped     shipped
5679    GBT104   Not Grouped  express

Query

SELECT o/n, SKU, order_type, state, 
CASE WHEN order_type is "Grouped" AND (state = "express" AND state = "arrived") THEN "multi_state"
CASE WHEN order_type is "Grouped" AND (state = "express" AND state = "shipped") THEN "multi_state"
ELSE "single_state"
END AS state_check
FROM data.table

>Solution :

You need a window function (an aggregation function with an OVER clause) to look at multiple rows at once.

SELECT 
  "o/n", sku, order_type, state,
  CASE
    WHEN order_type <> 'Grouped' THEN
      'single_state'
    WHEN COUNT(DISTINCT state) OVER (PARTITION BY "o/n", order_type) = 1 THEN
      'single_state'
    ELSE
      'multi_state'
  END
FROM data.table
ORDER BY "o/n", sku;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading