SQL problem: Adapting code fragment

5 pts.
Tags:
Access 2007
SQL
SQL commands
SQL Server
How do I adapt the fragment of code below to show:
a) Two columns - the Filipino Population and the Total Population 
b) The Filipino Population as a percentage of the total population 
SELECT DISTINCTROW POPDATA.CITYNUM, 
COUNT(*)FROM POPDATA 
WHERE POPDATA.ANCNUMBER=720 
GROUP BY POPDATA.CITYNUM 
ORDER BY COUNT(*) DESC;


Software/Hardware used:
Microsoft Office Access 207
ASKED: August 22, 2010  10:56 AM
UPDATED: August 23, 2010  6:58 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

Not sure why you want to try to use that fragment as a starting point.
Assuming that “ancnumber” is “ancestry number” and 720 is the code for filipino, the following will give the answer:

select
count(*) as TotalPopulation,
tmp.Filipinos,
cast ((100.0 * tmp.Filipinos / count(*)) as decimal(5.2)) as PercentFilipinos
from
popdata,
( select count(*) as Filipinos from popdata where ancnumber = 720) tmp
group by tmp.Filipinos

While there are ways to mix aggregate functions across entire tables against aggregate functions within subsets of rows, unless there is some special reason, doing aggregates on subsets is often easier as a subquery and then join the subquery with the main data set for the result.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following