Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Counting in sql

Posted on 2016-09-08
7
Medium Priority
?
63 Views
Last Modified: 2016-09-08
Hi All

I'm trying to do something which might come down to logic, but I think mostly comes down to not knowing how to write it in an sql query.  I've attached a sheet from Excel so that I can explain what I mean.

I need to figure out a way to count the number of different personal_id's per SSN e.g. for THES06001750Y there will be 2.  In my whole data, some may only have one and some could have many, many more.  I need this number so that I can divide into another value later on as it's to do with duplicates.  I can't just do something clever with duplicates because I probably don't know how but mainly because I won't be able to access the other data, summarize both sets of data and track differences in the way I need.

I've had a couple of goes and the best (?!!!!) one so far seems to be to create a table based on distinct personal_id's, get the SSNs and then, in the main table, which has all the first set of data in it, add a column and try to join the tables with some kind of count.  In the vain hope that today might be my lucky day, I tried the following:

UPDATE A SET PI_Counts = B.COUNT (personal_id)
FROM LFRD A LEFT OUTER JOIN LFRD_PI_Count B ON A.SSN = B.PI_SSNs;

I didn't really think it would work and, of course it didn't!

Please can somebody help me figure out a way to do this?

Thanks
Sarah
EE_Count_Example.xlsx
0
Comment
Question by:ScuzzyJo
  • 3
  • 2
  • 2
7 Comments
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41789704
Here's one way to do it:

SELECT  COUNT(personal_id) AS cnt ,
        ssn
FROM    ( SELECT    personal_id ,
                    ssn
          FROM      #tmp_test
          GROUP BY  personal_id ,
                    ssn
        ) c
GROUP BY ssn;

Open in new window


I first get the distinct records from the table (using grouping), then do the count
0
 
LVL 44

Accepted Solution

by:
zephyr_hex (Megan) earned 1600 total points
ID: 41789722
And I believe your update will look like:

UPDATE A SET A.PI_Counts = COUNT(c.personal_id)
FROM LFRD A
INNER JOIN (
	SELECT personal_id, ssn
    FROM      #tmp_test
    GROUP BY  personal_id, ssn
) c ON c.ssn = A.ssn
GROUP BY c.ssn

Open in new window

0
 

Author Comment

by:ScuzzyJo
ID: 41789744
Hi Zeyphyr_hex

Thanks, am trying it now, but how do I get the results into a table?  I know I'm going to put "into" somewhere but I'm not sure where.

Thanks
Sarah
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 400 total points
ID: 41789764
Below will insert the result set in to a temp table


SELECT  COUNT(personal_id) AS cnt ,
        ssn
INTO #Temp
FROM    ( SELECT    personal_id ,
                    ssn
          FROM      #tmp_test
          GROUP BY  personal_id ,
                    ssn
        ) c
GROUP BY ssn;

Open in new window

0
 

Author Comment

by:ScuzzyJo
ID: 41789782
Hi

Stupid me!  I knew that - where to put the into bit.

Your solution works perfectly.  Thanks for your help.

Thanks
Sarah
0
 

Author Closing Comment

by:ScuzzyJo
ID: 41789785
Really quick and good answers.  Thanks to both of you.
0
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41789790
Welcome :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question