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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to get count of columns with a specific text?

Author  Topic 

tskmjk
Starting Member

11 Posts

Posted - 2009-09-25 : 09:16:51
Hi,

I am designing a reporting tool where a requirement says to get count of columns where the text is 'yes'. For example:

Data is

Calltable changed_custname changed_custmobno
--------------------------------------------------------------
TEST2 yes no
TEST3 no yes

The result should be by calltable wise and count of the two columns only for 'yes' the result should be like..

Calltable No. of custname changes no. of custmobno changes
--------------------------------------------------------------------

TEST2 1 0
TEST3 0 1

kINDLY help me with the sql query?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-25 : 09:20:47
select Calltable,
sum(case when changed_custname='yes' then 1 else 0 end) as [no of custname changes],
sum(case when changed_custmobno='yes' then 1 else 0 end) as [no of custmobno changes]
from your_table
group by Calltable


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

tskmjk
Starting Member

11 Posts

Posted - 2009-09-25 : 09:24:19
Super!! thanks for your help....
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-25 : 09:26:04
quote:
Originally posted by tskmjk

Super!! thanks for your help....


You are welcome

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -