Rosscoasks
Rosscoasks

Reputation: 107

How to pivot a field into two seperate fields based on condition

I'm looking for some assistance with the following issue. I am looking for the the most elegant way to do this, as my current method really is not nice, at all. Any help would be greatly appreciated :

I have this:

cnumber     cid         prim
n100        123123      y         
n100        465488      n         
n123        798         y         
n768        544         y         
n455        123         y         
n455        098         n       

I am looking to get this:

cnumber     y           n         
n100        123123      465488
n123        798         NULL
n768        544         NULL
n455        123         98

Upvotes: 1

Views: 17

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270713

Is this what you want?

select cnumber,
       max(case when prim = 'y' then cid end) as y,
       max(case when prim = 'n' then cid end) as n
from t
group by cnumber;

Upvotes: 1

Related Questions