SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get the count of each value ina column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

praniktha
Starting Member

3 Posts

Posted - 09/20/2012 :  06:06:56  Show Profile  Reply with Quote
Hi,
I have a column in my Table which contains
AS,Bs
AS,CS
CS,ES
DE,AS
IR,CS,DE.
I need to find the Count of each value and output shuld come like this
AS 5
BS 3
CS 4 .can anyone help me to write a Procedure

senthil_nagore
Aged Yak Warrior

India
997 Posts

Posted - 09/20/2012 :  06:10:04  Show Profile  Visit senthil_nagore's Homepage  Send senthil_nagore a Yahoo! Message  Reply with Quote
What about other values ES, IR , DE ? or else you want the count of only AS,BS and CS ?

Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

praniktha
Starting Member

3 Posts

Posted - 09/20/2012 :  06:17:12  Show Profile  Reply with Quote
yes i want count of other values also ,the values may be of different combination & diff values
Go to Top of Page

senthil_nagore
Aged Yak Warrior

India
997 Posts

Posted - 09/20/2012 :  07:03:04  Show Profile  Visit senthil_nagore's Homepage  Send senthil_nagore a Yahoo! Message  Reply with Quote
Assign all the records into a varaible as below and try this

declare @my_var varchar(max)
Declare @srt varchar(10)

set @my_var = 'AS,Bs,AS,CS,CS,ES,DE,AS,IR,CS,DE,'



While len(@my_var)>0
Begin
set @srt= substring(@my_var,0,charindex(',',@my_var)+1)

select @srt +'-->'+ cast( (LEN(@my_var) - LEN(REPLACE(@my_var, @srt, ''))) /LEN(@srt) as varchar(20))

set @my_var = REPLACE(@my_var, @srt, '')
End


Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

praniktha
Starting Member

3 Posts

Posted - 09/21/2012 :  00:05:11  Show Profile  Reply with Quote
Thanx foryour reply.When i assign all the records to varchar(max) it exceeds the limit.huge amount of data in DB with various combinations.can u tell any other way.i want to get the precentage of each value

Edited by - praniktha on 09/21/2012 00:06:16
Go to Top of Page

senthil_nagore
Aged Yak Warrior

India
997 Posts

Posted - 09/21/2012 :  00:26:30  Show Profile  Visit senthil_nagore's Homepage  Send senthil_nagore a Yahoo! Message  Reply with Quote
I know its not an optimized way. But your table is not normalized, so you can try with temp table/table varaible, or you can go by row by row.

Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008

Edited by - senthil_nagore on 09/21/2012 00:44:46
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000