Skip to content
Advertisement

For each distinct PK I have a mixture of TRUE/FALSE

If a PK has one YES value, I want all the other values to be YES. I am thinking going a CTE route that I join back in but was wondering if there is an easier way!

A table below

| CUSTOMER_ID | CONTRACT_ID | ACTIVE |

  1182102192         1          N
  1182102192         2          N
  1182102192         3          N
  1182102192         4          Y
  1182102192         5          N

I am trying to get my table to look like this if only one Y is in the table above.:

| CUSTOMER_ID | CONTRACT_ID | ACTIVE |

  1182102192         1          Y
  1182102192         2          Y
  1182102192         3          Y
  1182102192         4          Y
  1182102192         5          Y

Advertisement

Answer

I strongly recommend window functions if you just want a SELECT:

select ct.*,
       max(active) over (partition by customer_id) as imputed_active
from customertable ct;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement