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
 Count Question

Author  Topic 

fengfeng
Yak Posting Veteran

64 Posts

Posted - 2011-03-04 : 15:50:13
I want to count the number of phones. There is a phone field in the database.

But it appears like this:

Motorola Samsung Nokia
Motorola Nokia
Samsung Nokia
Nokia Motorola


So if there's more than 1 phone it appears on the same line separated by a space.

I know to count the number of phones is

sum((1+len(NULLIF(LTRIM(phone),''))-len(replace(NULLIF(LTRIM(phone),''),' ',''))))

How do I count everything except "Apple" and "LG"
So if it is motorola nokia samsung apple lg

It only counts the motorola nokia and samsung. How do I incorporate that into my query?

My current query so far is

Select Phone= sum((1+len(NULLIF(LTRIM(phone),''))-len(replace(NULLIF(LTRIM(phone),''),' ',''))))
from database
where date = '20110101'
and phone > ' '


But is there a way to group the count of phones by the individual phone so it can look something like this:

Motorola 3
Nokia 3
Samsung 2

There are tons of different phones names so I dont want to have to type out to count every different phone name.

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-03-04 : 16:06:38
perhaps you will need to parse the data into multiple columns, e.g.

Col1 Col2 Col3
Motor Sams Nok

Then append col1, col2 and col3 data through three different select using UNION to make one set of data and at last use the count function with Group by clause on the resultant data set.



Cheers
MIK
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-05 : 09:35:31
I am thinking about this in a different way than MIK, mainly because fengfeng says there can be a large number of phone companies. I want to parse each row and put into a table, and then count. The code below looks long and convoluted, but a lot of it is setting up the data and tables required for testing
----------------------------------------------------------------
--- Create a table of numbers.
if object_id('tempdb.dbo.#Numbers') is not null drop table dbo.#Numbers;
create table dbo.#Numbers(n int not null primary key clustered);

--- insert 10000 numbers into it. (must be greater than the sum total
--- of characters in every row of the phone names)
declare @max int; set @max = 10000;

with a as (select top (cast(sqrt(@max)+1 as int)) number as N from master..spt_values)
insert into #Numbers
select top (@max) row_number() over (order by a.N) from A cross join A b

GO
----------------------------------------------------------------
-- create test data with phone names

if object_id('tempdb.dbo.#phones') is not null drop table #phones;
create table #phones (phoneNames varchar(255));
insert into #phones values ('Motorola Samsung Nokia LG');
insert into #phones values ( 'Apple Nokia'); -- check: if extra spaces work correctly
insert into #phones values ( 'Airtel LG Apple BSNL Motorola');
insert into #phones values ( 'ABC ABC Motorola'); -- check: two ABCs should count as 2

GO

----------------------------------------------------------------
-- Need a separator other than space because xml mistreats space.
declare @sep varchar(1); set @sep = '|';

-- concatenate rows into @var with each name separated by the separator.
declare @var varchar(max);
select @var =
(
select
@sep as [text()],
replace(phoneNames,' ',@sep) as [text()]
from #phones
for xml path ('')
)+ @sep ;

-- split the concatenated var into a table
with CTE(name) as
(
select
nullif(subString(@sep + @var + @sep , n , charindex(@sep ,@sep + @var + @sep , n) - n) , '')
from
#Numbers
where
n <= len(@sep + @var + @sep)
and subString(@sep + @var + @sep , n - 1, 1) = @sep
and charIndex(@sep , @sep + @var + @sep , n) - n > 0
)
-- and count records in the table.
select name,count(*) as phones from CTE group by name order by name

----------------------------------------------------------------

if object_id('tempdb.dbo.#Numbers') is not null drop table dbo.#Numbers;
if object_id('tempdb.dbo.#phones') is not null drop table #phones;
----------------------------------------------------------------
And, fengfeng, thanks for posting this problem, I had fun trying to figure it out on an otherwise lazy Saturday morning :--)
Go to Top of Page
   

- Advertisement -