Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Counting in sql

Posted on 2016-09-08
7
Medium Priority
?
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

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 30

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

718 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