I have a schema with sex(m/f),state_name(all states of a country), student_name,and education (phd,ms).
I use following query to return total males or females in each category of education for every state.
select sex,education,count(*) from mytable group by education,sex;
How can I write a query that returns the arithmetic ratio of (male divided by female) for each education in every state?
I am trying to get to query that gives me gender ratio across education tiers for each state (I got count above, but I could not figure out how to do arithmetic division)
You can do conditional aggregation:
select education, sum(sex = 'male') / nullif(sum(sex = 'female'), 0) ratio
from mytable
group by education
Related
I have the following query:
SELECT i.[Race / Ethnicity], NZ(Count(i.[Race / Ethnicity]),0) AS [Count Ethnicity]
FROM Information AS i
LEFT JOIN Visits AS v
ON i.[Customer ID] = v.[Customer ID]
WHERE (Month(v.[Visit Date]) = [Forms]![Report Generator]![month_box_rpt]
AND Year(v.[Visit Date]) = [Forms]![Report Generator]!
[year_box_rpt]) and (Month(i.[Signup Date]) = [Forms]![Report Generator]![month_box_rpt] AND Year(i.[Signup Date]) = [Forms]![Report Generator]![year_box_rpt])
GROUP BY (i.[Race / Ethnicity]);
It takes month ([month_box_rpt]) and year ([year_box_rpt]) values from a form and compares to month and year values for a value in one of the tables ([Visit Date]) and returns a count of people of different ethnicities (GROUP BY (i.[Race / Ethnicity])).
The query works. However if there are zero records that meet the specified criteria I get a blank return when the query is run.
How can I get a result that has each of my universe of ethnicities and a zero value if there are no people of that ethnicity that meet the criteria?
Example:
Race / Ethnicity Count
Asian 0
Black 0
White 0
or (if some groups have values):
Race / Ethnicity Count
Asian 0
Black 3
White 2
Right now the above query returns:
Race / Ethnicity Count
or:
Race / Ethnicity Count
Black 3
White 2
I believe it involves a sub-query and have tried several but haven't been able to get it to work.
Thanks in advance for guidance.
Left Join the results of your aggregate query with another table or query that contains a single row for each of the Ethnicity values and nothing else.. join on Ethnicity = Ethnicity
I'm sure this is probably something simple that I'm missing, but I'm trying to do a MySQL query that counts the occurrence of each item in a row, and the returns a result that has the entire row, ordered by count.
For example, I have a row with these entries:
Agriculture
Energy
Energy
Environment
Agriculture
Mining
Energy
I want my query to return to me:
Energy
Energy
Energy
Agriculture
Agriculture
Environment
Mining
I'm trying this:
SELECT `companyname`, `id`, `location`,`industry`, COUNT(`industry`) AS `industry_occurrence`
FROM atl3_atl_testimonials
WHERE state ='1'
ORDER BY industry_occurrence
But it is only returning the first "Energy" result, rather than all the results, ordered by occurrence. What should I change?
I hope you are looking for one such here.
SELECT `companyname`, `id`, `location`,
a.industry, cnt AS `industry_occurrence`
FROM atl3_atl_testimonials a,
(SELECT COUNT(`industry`) cnt, `industry`
FROM atl3_atl_testimonials
Group by industry) f
WHERE state ='1'
AND f.industry = a.industry
ORDER BY cnt desc
I'm working on a sports database, and I want to write a query that will return the name and the statistical value for certain categories. For example, goal leader, assist leader, points leader, +/- leader, penalty minutes leader, etc. I am using a table called NJDSkaters which contains player names and stats from a specific team. Here is the query code:
SELECT CONCAT(PlayerName,' - ',Goals) AS GoalLeader, CONCAT(PlayerName,' - ',Assists)
CONCAT(PlayerName,' - ',Points) AS PointsLeader
FROM NJDSkaters
WHERE Goals = (SELECT DISTINCT MAX(Goals) FROM NJDSkaters)
OR Assists = (SELECT DISTINCT MAX(Assists) FROM NJDSkaters)
OR Points = (SELECT DISTINCT MAX(Points) FROM NJDSkaters);
Here is a snippet from my skater register table which will show the players who should be returned by this query:
As you can see, my desired return query should have 'Ilya Kovalchuk - 37' returned as GoalLeader, 'Patrik Elias - 52' as AssistLeader, and 'Ilya Kovalchuk - 83' as PointsLeader. Running the query does provide these results, but there is extra information included that I do not want, as you can see here:
My question is, how do i get rid of the excess information? I only want the leaders in each category, and I don't want to see the #2 player listed, even if that player is #1 in some other category. Essentially, what I'm saying, is I want only 1 row in this table. Before, I had code that would return all players with the leaders at the top, so this code is a step closer to my desired result, but now I'm stuck. Searching for an answer to this problem has been challenging, as finding a way to ask it generally is difficult.
You need to PIVOT your data, I would use something like this:
SELECT
MAX(CASE WHEN NJDSkaters.Goals=mx.goals
THEN CONCAT(PlayerName,' - ', NJDSkaters.Goals) END) GoalLeader,
MAX(CASE WHEN NJDSkaters.Assists=mx.assists
THEN CONCAT(PlayerName,' - ', NJDSkaters.Assists) END) AssistsLeader,
MAX(CASE WHEN NJDSkaters.Points=mx.points
THEN CONCAT(PlayerName,' - ', NJDSkaters.Points) END) PointsLeader
FROM
NJDSkaters INNER JOIN (
SELECT MAX(Goals) goals, MAX(Assists) assists, MAX(Points) points
FROM NJDSkaters) mx
ON NJDSkaters.Goals=mx.goals
OR NJDSkaters.Assists=mx.assists
OR NJDSkaters.Points=mx.points
Please see fiddle here.
You might also want to use GROUP_CONCAT instead of MAX in case that more than one player shares the same maximum value:
SELECT
CONCAT(GROUP_CONCAT(CASE WHEN NJDSkaters.Goals=mx.goals
THEN PlayerName END), ' - ', mx.goals) GoalLeader,
CONCAT(GROUP_CONCAT(CASE WHEN NJDSkaters.Assists=mx.assists
THEN PlayerName END), ' - ', mx.assists) AssistsLeader,
CONCAT(GROUP_CONCAT(CASE WHEN NJDSkaters.Points=mx.points
THEN PlayerName END), ' - ', mx.points) PointsLeader
FROM
NJDSkaters INNER JOIN (
SELECT MAX(Goals) goals, MAX(Assists) assists, MAX(Points) points
FROM NJDSkaters) mx
ON NJDSkaters.Goals=mx.goals
OR NJDSkaters.Assists=mx.assists
OR NJDSkaters.Points=mx.points
A little explanation:
The subquery mx will return the maximum number of goals, the maximum number of assists, and the maximum points
I'm joining the table NJDSkaters with this subquery to return all of the rows that have the maximum number of goals OR the maximum number of assists OR the maximum points
CASE WHEN NJDSkaters.Goals=mx.goals THEN PlayerName END will return the PlayerName if that player has the maximum number of goals, otherwise it will return NULL. The same goes for assists and points.
using GROUP_CONCAT I'm concatenating all of the players names returned by the CASE WHEN. GROUP_CONCAT will skip NULL values and will only concatenate players that have the maximum value for their category
using CONCAT I'm concatenating the string returned by the GROUP_CONCAT above with the maximum value for each category.
why not to limit the result by using LIMIT 1?
I have a table with 3 columns
Column 0: auto inc index
Column 1: Person's Gener
Column 2: The STATE the person lives in (US States and Puerto Rico)
I want to run a query that tells me how many MEN are in each state
so the output would list all 50 states (in 1 column) and the second would be a number to determine the number of MEN in that state
Alaska 1000
New York 85000
(the figures above aren't accurate but i'm illustrating what I am looking for)
Thanks
You need to use a GROUP BY clause and the COUNT aggregate function.
SELECT State, COUNT(*) AS NumberOfMen
FROM your_table
WHERE Gender = 'M'
GROUP BY State
try this
select STATE, count(*) as num from table_name where gender ='MALE' GROUP BY STATE;
I have a Users table with these values (stripped down for the example):
Name
City
County
StateProvince
Country
Continent
Strength
Endurance
Intelligence
I basically want a query that returns the highest stat by region for yourself. If you have the highest Strength (67) in the country, highest Endurance (59) in your City, and not the highest Intelligence anywhere, I'd like a 2 row result of:
('Strength', 67, 'Country', 'United States')
('Endurance', 59, 'City', 'Redmond')
Note that if you have the highest Strength in the country, you will also have the highest strength in the state/province, county, and city (but I'd like those not to be returned in the result). I can do this all using multiple SELECT queries, however, I'd like to know how to do this in one single query for efficiency (or if that's even a good idea?). I'd imagine the logic would go something like (pseudo code):
(SELECT FROM Users ORDERBY Strength WHERE Country = 'MyCountry' LIMIT 1) IF Name != 'Me' (SELECT FROM Users ORDERBY Strength WHERE StateProvince = 'MyState' LIMIT 1) ... and so on
Furthermore, the above would also need to work with Endurance and Intelligence. Thanks!
Check this link MySQL control-flow-functions
SELECT CASE 1 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'more' END;