How can I use a subquery on my query results and then ORDER BY a calculated result for each row?

Eilidh

Context

I'm having some trouble putting together the logic of subqueries(?) together in my head.

   *---------*---------*---------*------------*------------*---------*
   |GUEST_ID | Country | County  | Attending  | Donation   | Party   |
   *---------*---------*---------*------------*------------*---------*

I have a database containing records about attendees of an charity ball.

GUEST_ID: Table key.
Country: Country the guest is from.
County: County the guest is from (i.e. a region within that country).
Attending: Whether or not the guest is attending (i.e. true or false).
Donation: Amount the guest is donating to the cause.
Party: Which Party the guest is attending.


Goal

I wish to display a table broken down by Country and County, showing the number of attendees from each Country + County, and the average donation of those who are attending from that Country + Country. I'd then like to order the rows from highest average donation to lowest. I understand the constituent parts of this query, however I'm not sure how to 'glue' it together as a whole.

I can GROUP BY Country, County.

I can SUM(Donation).

I can COUNT(*) WHERE ATTENDING = 'Yes'

And I know I can SET @variables to store results in the interim.

I also know I can ORDER BY DESC.

So far

My issue is with understanding how to combine these elements into a functioning query. I'm guessing I need to use subqueries however it's getting the order right I'm having trouble with. This is what I have so far -

SELECT SUM(`Donation`) AS `TotalDonations`, `Country`, `County`  
FROM `GuestList` 
WHERE `Party` = `2014CharityBall`
GROUP BY `Country`, `County`

I'm not sure how to add the subquery to find the COUNT of only those guests who are definitely attending, or how to calculate the TotalDonations / DefinitelyAttending and then ORDER BY this.


Results Required

*------------*------------*---------------*---------------*---------------*
| Country    | County     | # of Attendees|Total Donations|Avg. Donation  |
*------------*------------*---------------*---------------*---------------*

Country: Country the guests are from.
County: County the guests are from (i.e. a region within that country).
# of Attendees: Number of attendees (Attending = 'true') within that country and county.
Total Donations: Total donations of all those attending (Attending = 'true') within that country and county (e.g. SUM(Donation)).
Avg. Donation: Average donation of all those attending (Attending = 'true') within that country and county (e.g. AVG(Donation) - that is, ofc, Total Donations / # of Attendees).


Extra Credit

Just an expression ;)

If I want to calculate the Total Donations among all donators invited to the party, and, separately, the Total Donations solely among those donators attending the party, how would I do that?

JBrooks
    SELECT `Country`, 
    `County`,
    SUM(`Donation`) AS `TotalDonations`,
    FORMAT(AVG(`Donation`), 0) AS `AVGDonations`,
    COUNT(1) Attending
    FROM `GuestList` 
    WHERE `Party` = `2014CharityBall`
           AND Attending = `Yes`
    GROUP BY `Country`, `County`
    Order by 4 desc

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How i can use result of subquery in this query?

How can I reference a calculated field in a subquery?

How can I add a row to my SQL results between each "category" change in the data?

How can i order my query result from Hive external Druid table?

How can I pass the results of a query/subquery to another query, and UNION the results together?

How can I include my query in my query results?

How to Order By a Subquery result

How to fix single-row subquery returns more than one row issue in select query in order to achieve the result in a desired manner?

Order by result of a query / Mysql Subquery

How can I update my database table with the result of my query

How can I tell the Django ORM to reverse the order of query results?

How can I order my results by a secondary index or column in Cassandra?

How can I use 'AS' in my count query?

How can I cast a query result to string and insert it into a table row

How can I use the result of a SQL query in another SQL query

How can I use a query result as a column for a separate query?

How can I use a SQL query result as a column in another query?

How to use postgres subquery result to filter overall results

How can I find records in a subquery result where an item has a result which is false and has no results which are true?

How to use the result of a subquery

How can I query mulltiple tables and order by date in my view

How can I fix this issue with my simple query with ORDER BY and LIMIT?

How can I add a new calculated column using a window function to my SQL query?

How can I make this SQL query work to prevent the "subquery returned more than one row" error?

How can I update calculated data in my excel sheet in the next column each time I run my code in MATLAB?

Insert into table as many results row returns from subquery along with outer query result

Can I use purrr to execute a dplyr query and save the result of each query output

How to re-use the result of a subquery in the main query, with Postgres

How to use insert query where one of the value is a result of a subquery