Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help getting totals by ZipCode


Need help getting totals by ZipCode

Author
Message
andy 56206
andy 56206
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 35
Hi all,
I am trying to get grand total, totals of each ethnicity and percentage of each ethnicity by zipcode.
I can get the total number by zipcode without adding the ethnicity portion to the query by using:

SELECT Count(ChildPartyID), ResidencyZip
FROM CaseChild
Where programID = x

But I need to be able to display the toal number per zip AND further break down the data by ethnicity.

I am using the following code:

SELECT cc.ResidencyZip as ZIP, e.EthnicityDesc,
    Count(e.EthnicityDesc) as 'Record Count',
    Convert(decimal(18,2),1.0 * COUNT(e.EthnicityDesc)/COUNT(cc.ChildPartyID) * 100) AS 'Percentage'
FROM CaseChild cc JOIN
          Party p on cc.childPartyID = p.partyID JOIN
          Ethnicity e on p.EthnicityID = e.EthnicityID
         
WHERE (cc.programID = 6)
GROUP BY    cc.ResidencyZip,e.ethnicityDesc
ORDER BY    cc.ResidencyZip

I ge the proper totals per ethnicitydesc but all the percentages are 100% and the Count(cc.ChildPartyID) is now Equal to
Count(e.EthnicityDesc)
I am new at this so any help is greatly appreciated.
Thanks
Andy
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14942 Visits: 38939
you can use a technique using SUM(CASE... to get subtotasl;
you'll have to construct the multiple case statements to match your descriptions, i think.

here's a prototype based on the query you posted.

SELECT
cc.ResidencyZip AS ZIP,
COUNT(e.EthnicityDesc) AS 'Record Count',
SUM(CASE WHEN e.EthnicityDesc = 'White' THEN 1 ELSE 0 END) AS BENCOUNT1,
SUM(CASE WHEN e.EthnicityDesc = 'Black' THEN 1 ELSE 0 END) AS BENCOUNT2,
SUM(CASE WHEN e.EthnicityDesc = 'Asian' THEN 1 ELSE 0 END) AS BENCOUNT3,
CONVERT(DECIMAL(18,2),1.0 * COUNT(e.EthnicityDesc)/COUNT(cc.ChildPartyID) * 100) AS 'Percentage'
FROM CaseChild cc
INNER JOIN Party p
ON cc.childPartyID = p.partyID
INNER JOIN Ethnicity e
ON p.EthnicityID = e.EthnicityID
WHERE (cc.programID = 6)
GROUP BY    cc.ResidencyZip
ORDER BY    cc.ResidencyZip



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Chris Harshman
Chris Harshman
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2667 Visits: 3260
andy 56206 (12/5/2012)
..I am using the following code:

SELECT cc.ResidencyZip as ZIP, e.EthnicityDesc,
    Count(e.EthnicityDesc) as 'Record Count',
    Convert(decimal(18,2),1.0 * COUNT(e.EthnicityDesc)/COUNT(cc.ChildPartyID) * 100) AS 'Percentage'
FROM CaseChild cc JOIN
          Party p on cc.childPartyID = p.partyID JOIN
          Ethnicity e on p.EthnicityID = e.EthnicityID
         
WHERE (cc.programID = 6)
GROUP BY    cc.ResidencyZip,e.ethnicityDesc
ORDER BY    cc.ResidencyZip

I ge the proper totals per ethnicitydesc but all the percentages are 100% and the Count(cc.ChildPartyID) is now Equal to
Count(e.EthnicityDesc)


the COUNT function simply counts all the rows per grouping based on your GROUP BY clause, not based on values of specific columns. If you want to calculate percents, you'd probably want to use COUNT with an OVER clause such as:
SELECT cc.ResidencyZip as ZIP, e.EthnicityDesc,
Count(*) as record_count,
Convert(decimal(18,2),100.0 * COUNT(*) / COUNT(*) OVER (PARTITION BY ResidencyZip)) AS ethnicity_percent
FROM CaseChild cc
JOIN Party p on cc.childPartyID = p.partyID
JOIN Ethnicity e on p.EthnicityID = e.EthnicityID
WHERE (cc.programID = 6)
GROUP BY cc.ResidencyZip, e.ethnicityDesc
ORDER BY cc.ResidencyZip



http://msdn.microsoft.com/en-us/library/ms189461(v=sql.100).aspx
andy 56206
andy 56206
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 35
This is why I hesitate to post to this forum. I always seem to run into people like you.
It was a simple question and if you do not have an answer why post!
sanket kokane
sanket kokane
Old Hand
Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)

Group: General Forum Members
Points: 331 Visits: 1019
please don't stop posting your questions..
Just follow some rules ..People are happy to help you here
Don't take anything Personally :-)

-----------------------------------------------------------------------------
संकेत कोकणे
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
sanket kokane (12/10/2012)
please don't stop posting your questions..
Just follow some rules ..People are happy to help you here
Don't take anything Personally :-)


Especially such default rude and non-constructive answers from J.CELKO.
I think he has a template somewhere...

Saying that, you will definitely benefit from posting your question in line with forum etiquette as described in an article - link at the bottom of my signature;-)

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search