|
|
#1 | |
|
Math in SQL? (HW)
I haven't even had 3 hours of hands on time with SQL yet, so I'm sorry if this comes off sounding like a very noobish question, but I can't figure out what I need to do to take the number returned by this query:
Code:
SELECT description, deaths1 FROM who.deaths INNER JOIN who.icd9 ON who.deaths.cause = who.icd9.cause where sex = 2 and country in (SELECT country FROM who.country where country_name like '%united kingdom%') and ryear = 1980 and deaths1 > 0 group by who.icd9.cause order by deaths1 desc limit 1; Specifically, the question from my homework assignment is Quote:
I can't make what I've already written a subquery, because MySQL complains that it doesn't support limits inside of subqueries. Suggestions? Edit: Alright, I can separately find the sum of all UK female deaths from 1980 with this query: Code:
SELECT SUM(deaths1) deaths1 FROM who.deaths where sex = 2 and country in (SELECT country FROM who.country where country_name like '%united kingdom%') and ryear = 1980;
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed. Like it on Facebook! Last edited by ArtOfWarfare; Feb 22, 2013 at 09:53 PM. |
||
|
|
0
|
|
|
#2 |
|
A schema and sample data would make this a lot easier to help
![]() Do you have a query giving you the female population of the UK? From what I understand of the question you need the leading death cause figure divided by the total female population multiplied by 100. So something like: Code:
SELECT description, deaths, (deaths/pop.population)*100 AS 'Percentage' FROM deaths, (SELECT population FROM countries WHERE name = 'United Kingdom') AS pop ORDER BY deaths DESC LIMIT 1 |
|
|
|
0
|
|
|
#3 |
|
Thanks!
I was able to figure out how to do this using the code you shared. My exact implementation doesn't look as nice (it's a lot more lines) but it gets the job done: Code:
SELECT description, deaths1, pop1, (deaths1*100/pop1) AS 'Percent' FROM who.deaths INNER JOIN who.icd9 ON who.deaths.cause = who.icd9.cause INNER JOIN who.population ON who.deaths.country = who.population.country AND who.deaths.sex = who.population.sex AND who.deaths.ryear = who.population.ryear where who.deaths.sex = 2 and who.deaths.country in (SELECT country FROM who.country where country_name like '%united kingdom%') and who.deaths.ryear = 1980 and who.deaths.deaths1 > 0 group by who.icd9.cause order by deaths1 desc limit 1;
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed. Like it on Facebook! |
|
|
|
0
|
|
|
#4 | |
|
Quote:
. I expected you'd need more lines to get it to work properly with your database.Calculated columns are very useful and can be used to work out data when querying rather than having to store it in the database. I'm not a MySQL guy by trade so had to look it up in the documentation and you're correct, the AS keyword can be omitted when using aliases. I myself prefer leaving it in so I can easily see which fields I've given aliases, but each to their own I guess. |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 10:36 PM.








Linear Mode
