Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 split column results into 2 columns

Author  Topic 

vkiss
Starting Member

1 Post

Posted - 2008-08-07 : 15:17:21
I have to create a data set of 2 columns from one:

Specifically, I have a column that has yes/no results (1 or 2) and I want to split it into 2 columns a Yes column and a no column, so I can total the yes's and no's. thx.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-07 : 23:39:01
[code]select
case when column = 1 then 1 else 0 end as [Yes],
case when column = 2 then 1 else 0 end as [No]
from yourtable[/code]



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-08 : 01:59:16
you can directly apply aggregation by grouping on field based on which you want totals. just modify above query as follows
select field,
case when column = 1 then 1 else 0 end as [Yes],
case when column = 2 then 1 else 0 end as [No]
from yourtable
group by field
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-08 : 02:16:54
Don't forget the aggregate function

select field,
sum(case when column = 1 then 1 else 0 end) as [Yes],
sum(case when column = 2 then 1 else 0 end) as [No]
from yourtable
group by field



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -