Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / July 2008

Tip: Looking for answers? Try searching our database.

Count ussing a column in the same table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Don Juan - 18 Jul 2008 08:12 GMT
Hello all

Here you will find what I need

---------------------------------------------
Name        Region    No_Customers   No_Names
---------------------------------------------
Albert       US            12          3
Albert       FR            91          3
Albert       EC             2          3
Cecilia      US             3          4
Cecilia      CA             4          4
Cecilia      CH            34          4
Cecilia      FR            12          4
Martha       EC             9          1
Rodrigo      CO             2          1
---------------------------------------------

Where Name is the sales representative
Where Region is the country
Where No_Customers is the number of customers in each country
Where No_Names [must be] the number of times a Name appears in the column Name

Te table [Data] has this structure:
ID, Name,Region.
The goal is to create a column with the number of times that a name is repeated in the Name column.
e.g. Cecilia appears 4 fours because her name is in 4 regions.
Albert apeears 3 times so it shows 3 in No_names
I hope I was clear with my output example.

dj.
vinu - 18 Jul 2008 08:37 GMT
Don

try this

create table #t(id int ,name varchar(10),region char(2),No_cut int)

insert into #t values(1,'Albert' ,      'US' ,           12)  
insert into #t values(2,'Albert'  ,     'FR' ,           91  )
insert into #t values(3,'Albert' ,      'EC' ,            2  )
insert into #t values(4,'Cecilia' ,     'US'  ,           3 )
insert into #t values(5,'Cecilia',      'CA' ,            4  )
insert into #t values(6,'Cecilia' ,     'CH' ,           34 )
insert into #t values(7,'Cecilia' ,     'FR' ,           12 )
insert into #t values(8,'Martha' ,      'EC' ,            9 )
insert into #t values(9,'Rodrigo' ,     'CO' ,            2 )

select t.[name],t.region,t.No_cut,rs.Cnt from #t t inner join

(
select count([name]) Cnt,[name] C_name from #t
group by [name]
)rs on rs.C_name=t.name

vinu
 Hello all

 Here you will find what I need

 ---------------------------------------------
 Name        Region    No_Customers   No_Names
 ---------------------------------------------
 Albert       US            12          3
 Albert       FR            91          3
 Albert       EC             2          3
 Cecilia      US             3          4
 Cecilia      CA             4          4
 Cecilia      CH            34          4
 Cecilia      FR            12          4
 Martha       EC             9          1
 Rodrigo      CO             2          1
 ---------------------------------------------

 Where Name is the sales representative
 Where Region is the country
 Where No_Customers is the number of customers in each country
 Where No_Names [must be] the number of times a Name appears in the column Name

 Te table [Data] has this structure:
 ID, Name,Region.
 The goal is to create a column with the number of times that a name is repeated in the Name column.
 e.g. Cecilia appears 4 fours because her name is in 4 regions.
 Albert apeears 3 times so it shows 3 in No_names
 I hope I was clear with my output example.

 dj.
Erland Sommarskog - 18 Jul 2008 09:47 GMT
> create table #t(id int ,name varchar(10),region char(2),No_cut int)
>
[quoted text clipped - 7 lines]
> insert into #t values(8,'Martha' ,      'EC' ,            9 )
> insert into #t values(9,'Rodrigo' ,     'CO' ,            2 )

Using Vinu's sample, here is a shorter query, which uses SQL 2005
syntax:

select id, name, region, cnt = COUNT(*) OVER (PARTITION BY name)
FROM #t
ORDER BY name

COUNT OVER does not always give good performance, though.

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Don Juan - 18 Jul 2008 10:15 GMT
Thanks both for your help!

>> create table #t(id int ,name varchar(10),region char(2),No_cut int)
>>
[quoted text clipped - 16 lines]
>
> COUNT OVER does not always give good performance, though.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.