Home > OS >  1 or 0 Per Group
1 or 0 Per Group

Time:06-08

Re-edited to make it clearer:

I would like my results to appear as they do in 'Column b' based on 'Column a' groupings? so a 1 or 0 per group based on column a. Column b does not exist currently, I am trying to code this in. I was trying to use row_number or rank but this not appear to work for me. So how do I write my SQL so I can get my SQL results to mirror Column b? Any help is appreciated

Thank - you

column a column b
aaa 1
aaa 0
ddd 1
ddd 0
ddd 0
yyy 1
yyy 0
yyy 0

CodePudding user response:

You just need to wrap your row_number() in a case, something like this:

select
    column_a
    case row_number() over (partition by column_a)
        when 1 then 1
        else 0
    end as column_b
from 
    table
/
  •  Tags:  
  • sql
  • Related