Solved

Counting in sql

Posted on 2016-09-08
7
38 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
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 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 18

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal 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 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41789790
Welcome :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now