Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Count over group

 
   Database Forums (Home) -> Programming RSS
Next:  Transparent Data Encryption (TDE)  
Author Message
DavidC

External


Since: Jul 30, 2010
Posts: 2



(Msg. 1) Posted: Fri Jul 30, 2010 2:51 pm
Post subject: Count over group
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have a view that I want to count the occurrance of a FedIDNo only once in a
group of linked SSNs (using ROW_NUMBER() OVER ?). For instance, if I have
the following I want to count only 1:

FedIDNo SSN

123456789 987654321
123456789 998877654

Below is my SQL.

SELECT dbo.ClientInfo.FedIDNo, dbo.People.SSN
FROM dbo.Timesheets INNER JOIN
dbo.ClientInfo ON dbo.Timesheets.ClientLinkID =
dbo.ClientInfo.PeopleLinkID INNER JOIN
dbo.People INNER JOIN
dbo.PeopleLink ON dbo.People.PersonID =
dbo.PeopleLink.PersonID ON dbo.Timesheets.WorkerLinkID =
dbo.PeopleLink.PeopleLinkID
GROUP BY dbo.ClientInfo.FedIDNo, dbo.People.SSN


Thanks.

--
David

 >> Stay informed about: Count over group 
Back to top
Login to vote
Eric Isaacs

External


Since: May 12, 2008
Posts: 109



(Msg. 2) Posted: Fri Jul 30, 2010 4:33 pm
Post subject: Re: Count over group [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Assuming you don't want it as part of your result set (which wouldn't
make sense because you have the SSN in there) you just need to use
COUNT(DISTINCT fieldname)...

SELECT COUNT(DISTINCT dbo.ClientInfo.FedIDNo)
FROM dbo.Timesheets INNER JOIN
dbo.ClientInfo ON dbo.Timesheets.ClientLinkID =
dbo.ClientInfo.PeopleLinkID INNER JOIN
dbo.People INNER JOIN
dbo.PeopleLink ON dbo.People.PersonID =
dbo.PeopleLink.PersonID ON dbo.Timesheets.WorkerLinkID =
dbo.PeopleLink.PeopleLinkID


-Eric Isaacs

 >> Stay informed about: Count over group 
Back to top
Login to vote
DavidC

External


Since: Jul 30, 2010
Posts: 2



(Msg. 3) Posted: Sat Jul 31, 2010 6:52 am
Post subject: Re: Count over group [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Actually, I do want it in the result set because the SSN is for the employEE
and the FedIDNo is for the EmployER. I need to count the Employer of a set of
Employees only once, no matter how many employees they have. If I have it in
the result set then the last record will have the count of unique FedIDNo.
Thanks.
--
David


"Eric Isaacs" wrote:

> Assuming you don't want it as part of your result set (which wouldn't
> make sense because you have the SSN in there) you just need to use
> COUNT(DISTINCT fieldname)...
>
> SELECT COUNT(DISTINCT dbo.ClientInfo.FedIDNo)
> FROM dbo.Timesheets INNER JOIN
> dbo.ClientInfo ON dbo.Timesheets.ClientLinkID =
> dbo.ClientInfo.PeopleLinkID INNER JOIN
> dbo.People INNER JOIN
> dbo.PeopleLink ON dbo.People.PersonID =
> dbo.PeopleLink.PersonID ON dbo.Timesheets.WorkerLinkID =
> dbo.PeopleLink.PeopleLinkID
>
>
> -Eric Isaacs
> .
>
 >> Stay informed about: Count over group 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 1649



(Msg. 4) Posted: Sat Jul 31, 2010 5:25 pm
Post subject: Re: Count over group [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

DavidC (dlchase@lifetimeinc.com) writes:
> Actually, I do want it in the result set because the SSN is for the
> employEE and the FedIDNo is for the EmployER. I need to count the
> Employer of a set of Employees only once, no matter how many employees
> they have. If I have it in the result set then the last record will
> have the count of unique FedIDNo.

It's not entirely clear what output you want, but it seems that
if you add

dense_rank() OVER (ORDER BY dbo.ClientInfo.FedIDNo)

to the result set, each employer will get a unique number.


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

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: Count over group 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
count(*) vs count(1) - I have always used * for queries like this where columns don't really matter because they arent being returned:: SELECT COUNT(*) FROM ... or WHERE EXISTS (SELECT * FROM ... Recently I've seen other SQL developers replacing the * with a 1 in these ..

COUNT and UNION? - Hi, I have the following very simple union query. I am union joining two select distinct lists to get a unique list of values . Can anyone tell me how to amend the query below so it just returns a COUNT of the rows returned. i.e m...

COUNT (DISTINCT (myfield)) PROBLEM - it works well SELECT DISTINCT ([cinsiyet]) FROM URUNLER WHERE cinsiyet<>'Bileklik' AND cinsiyet<>'Yuzuk' AND cinsiyet<>'Set' AND cinsiyet<> 'Kupe' AND cinsiyet<>'Bilezik' AND cinsiyet<> 'Kolye'; but i need to ...

Question anout DISTINCT and or COUNT - Hello group ! I have a table with 3 fields: table ID (Primary key) is : Word||Num Num = internal DB number to connect this record to another table (NOT RELEVANT for my question) Word = english word ID Num Word..

Optimizing an index for a select count(distinct) - Hi, I have a table logging our website activity with about 60 M of rows. One user has for itself more than 1 M rows. When I do a count(distinct), it takes two seconds. I was wondering if there is a way to speed that up? Here the table CREATE TABLE..
   Database Forums (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]