SQL: GROUP BY for number ranges? -
i'm first time poster , new sql, colleagues.
have column of numbers (let's call "numbers") , values in range 1-9999.
need create grouping column (let's call "numbers level") , assign values of aa numbers 1-999, bb 1000-6999 , cc 7000-9999.
so, final output below (output not need ordered):
numbers | numbers level ------------------------------------- 136 | aa 2935 | bb 4288 | bb 8987 | cc 436 | aa 3737 | bb
is there easy way script this, i.e.: using group clause?
or grouping column have exist , need populate command?
please let me know if question wreaks of cluelessness , i'll clarify best can.
this easiest case statement (assuming you're using sql-server; if not, please add tags correct version).
select [numbers] , case when [numbers] between 1 , 999 'aa' when [numbers] between 1000 , 6999 'bb' when [numbers] between 7000 , 9999 'cc' end [numbers level] mytable
if of numbers don't fall ranges, return null - use else
if want different result when happens.
Comments
Post a Comment