Solved

Counting in sql

Posted on 2016-09-08
7
48 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 42

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 42

Accepted Solution

by:
zephyr_hex (Megan) earned 400 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 100 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 28

Expert Comment

by:Pawan Kumar
ID: 41789790
Welcome :)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

831 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