Given the following diagram:
Right now I have queries to find out how much each member has donated and also listing those donations in a list for each member. Now I want to query the donations table to get the results divided up by each organization.
Something like:
Can someone please help me with this SQL?
Assuming that you're using MySQL:
SELECT
MemberId,
OrganizationId,
SUM(Amount) AS `Amount Donated to Organization`,
COUNT(Amount) AS `Number of Donations`
FROM
Donations
GROUP BY
MemberId,
OrganizationId
;
Related
I need to complete the following task in SQL:
Show the employment status in individual offices of the company in the following layout: office code, country, number of employees.
Table employees:
Table offices:
My idea was:
SELECT officeCode, (SELECT country FROM offices), COUNT(*)
FROM employees
GROUP BY officeCode
But it seems it doesn't work... Can you help?
Your main data set would be the office. Join to the employee's table by the office code. Then from your main table, get the data you need and then do a count( employee number)
Remember to add all the fields that you are displaying to the group by.
I've made a table with the columns for a customer name and each activity they participate in. How can I can I count the activities for each name and display it?
I've done;
SELECT Activity_Name, COUNT(*) AS 'Number_of_activities'
FROM tablename
GROUP BY Activity_Name;
which gives me each a table of each activity and how many participants in each activity but not each customer and their number of activities
Apologies for anything I've done wrong, only a couple months into coding and first time posting on stack...
Considering I don't know how your schema looks exactly, this query should be a nice representation of the idea how to do it:
SELECT customer_name, COUNT(*) AS 'Number_of_activities_per_customer'
FROM tablename
GROUP BY customer_name;
I have 3 mysql tables:
members_table(registrationDate memberId ageBand scoreBand)
clicks_table(clickDate memberId isApplication isApproval revenue ageBand scoreBand)
activeUsers_table(activityMonth memberId ageBand scoreBand platform)
I'm trying to find the way to get the :
percentage of active users every month for each monthly registration cohort broken out by age.
I can't fine the right manner for find results, can you help please?
Thanks!
I fear your question is difficult to understand, so I'll just offer a hint about how you might proceed.
If you want to write aggregate queries to summarize by month, you can use the LAST_DAY() function. Here's an example:
SELECT COUNT(*) members, LAST_DAY(registrationDate) registrationMonth
FROM members_table
GROUP BY LAST_DAY(registrationDate)
You could write several queries like this summarizing different metrics by month. Then, treating the queries like tables, you could join them together.
I would appreciate your help with this task.
I have to use data from two tables:
tourist_country (tourist_id, country_id), and
tourist_age_category (tourist_id, age_category_id).
I know how to get number of tourists for each country id, and number of tourists for each age category. But what I need is the number of tourists for each country_id but with a specific age category.
I believe I'm close to my answer when joining those tables:
SELECT *
FROM tourist_age_category
JOIN tourist_country ON tourist_country.tourist_id = tourist_age_category.tourist_id
But it hasn't gotten me anywhere so I ask for help, thank you!
Not quite sure I understand what you mean, but perhaps this is what you want:
SELECT country_id, age_category_id, count(*)
FROM tourist_age_category
JOIN tourist_country ON tourist_country.tourist_id = tourist_age_category.tourist_id
GROUP BY country_id, age_category_id
I am studying SQL at my university, and I was practicing this exercise and got stuck! I have a database that stores all the exams passed by a student for a specific teach.
These are the tables in the database:
Student(**number**, name, birthday)
Exam(**student_number**, **teaching_code**, vote) *(store the exams passed by the students with vote)*
Teaching(**code**, name)
Where number is the primary key for the Student table, and code is for Teaching, "student_number" references "number" in Student, and "teaching_code" references "code" in Teaching.
The exercise asks to select the students’ numbers with the average highest score.
I know how to write a query which gives me a table containing the average for each students but I don't know how to select the highest from it or how to show the corresponding student number!
The solution with the limit command doesn't work if exists some students have the same highest average...
The query to show the average score per student is:
select avg(e.vote) as Average from STUDENT s, EXAM e
where s.number = e.student_number
group by s.number
EDIT:
I tried the MAX function in SQL, I have tried this:
select MAX( avg(e.vote) ) as Average from STUDENT s, EXAM e
where s.number = e.student_number
group by s.number
but it say "Error Code: 1111. Invalid use of group function"
Probably the solution is with a nested query but I can't realize it.
SELECT MAX(Expression)
FROM tables
WHERE Condition
You can check the documentation on how to use MAX and find highest value.
If you want to select the TOP 5 or 10 or 20... highest, use the TOP clause.
select student_code, MAX(avg_vote)
FROM (
SELECT student_code, AVG( vote) as avg_vote
FROM Exam
GROUP BY student_Code ) t
GROUP BY student_code
I didn't checked that query.
May query to select that max average is be like that
What a pity MySQL doesn't seem to support CTE's, then this would have been so simple.
select t.student_code, t.avg_vote
FROM (SELECT student_code, AVG(vote) as avg_vote
FROM Exam
GROUP BY student_Code) t
WHERE t.avg_vote = (select max(avg_vote)
FROM (SELECT student_code, AVG(vote) as avg_vote
FROM Exam
GROUP BY student_Code))
Continue studying hard, being presented with a likely answer is far less useful than actually reaching the conclusion yourself. Actually, if you're lucky, my proposal will not work (I haven't tested) so that you will have to come up with the right modification yourself!