Using MySQL, how do I get the total items and total revenue for each manager's team? Suppose I have these 3 different tables (parent-child-grandchild):
Employee1 is under Supervisor1, and they are both under Manager1, and so on, but real data are in random arrangement. Color-coded the numbers to visualize which gets added.
I want my query to output the total items and total revenue of each manager's team like:
To easily create the table:
DROP TABLE IF EXISTS manager;
CREATE TABLE manager (id int, name varchar(55), no_of_items int, revenue int);
INSERT INTO manager (id, name, no_of_items, revenue)
VALUES
(1 , 'Manager1' , 10 , 100),
(2 , 'Manager2' , 20 , 200),
(3 , 'Manager3' , 30 , 300);
DROP TABLE IF EXISTS supervisor;
CREATE TABLE supervisor (id int, name varchar(55), manager_id int, no_of_items int, revenue int);
INSERT INTO supervisor (id, name, manager_id, no_of_items, revenue)
VALUES
(4 , 'Sup1' , 1, 100 , 1000),
(5 , 'Sup2' , 2, 200 , 2000),
(6 , 'Sup3' , 3, 300 , 3000);
DROP TABLE IF EXISTS employee;
CREATE TABLE employee (id int, name varchar(55), supervisor_id int, no_of_items int, revenue int);
INSERT INTO employee (id, name, supervisor_id, no_of_items, revenue)
VALUES
(7 , 'Emp1' , 4, 400 , 4000),
(8 , 'Emp2' , 5, 500 , 5000),
(9 , 'Emp3' , 4, 600 , 6000);
SQL Fiddle
Utilizing a combination of Nested Subqueries and UNION ALL, you can use the following:
SELECT inner_nest.manager_id,
inner_nest.name,
SUM(inner_nest.total_items) AS total_items,
SUM(inner_nest.total_revenue) AS total_revenue
FROM (
SELECT id as manager_id,
name,
SUM(no_of_items) AS total_items,
SUM(revenue) AS total_revenue
FROM manager
GROUP BY id
UNION ALL
SELECT m.id as manager_id,
m.name,
SUM(s.no_of_items) AS total_items,
SUM(s.revenue) AS total_revenue
FROM manager m
INNER JOIN supervisor s ON s.manager_id = m.id
GROUP BY m.id
UNION ALL
SELECT m.id as manager_id,
m.name,
SUM(e.no_of_items) AS total_items,
SUM(e.revenue) AS total_revenue
FROM manager m
INNER JOIN supervisor s ON s.manager_id = m.id
INNER JOIN employee e ON e.supervisor_id = s.id
GROUP BY m.id
) AS inner_nest
GROUP BY inner_nest.manager_id
Try this: http://sqlfiddle.com/#!9/7a0aef/20
select id,name,COALESCE(sum(distinct m),0)+COALESCE(sum(distinct s),0)+COALESCE(sum(e),0)
as total_item,COALESCE(sum(distinct mv),0)+COALESCE(sum(distinct sv),0)+COALESCE(sum(ev),0)
as total_revenue
from
(
select m.id,m.name,m.no_of_items as m,s.no_of_items as s,e.no_of_items as e,
m.revenue as mv,s.revenue as sv,e.revenue as ev
from manager m left join supervisor s
on m.id=s.manager_id
left join employee e on s.id=e.supervisor_id)a
group by id,name
A single query with the appropriate joins does the trick... In pseudo code (sorry, I'm typing on my phone)
Select a.manager_id, (a.total_items+b.total_items+c.total_items) as totalitems, (a.total_revenue+b.total_revenue+c.total_revenue) as totalrevenue
From parent_table a
Join child_table b on a.manager_id=b.manager.id
Join grandchild_table c on b.sup_id=c.sup_id
Group by a.manager_id
Some adjustments may be needed, but this should definitely point you in your way
Related
SELECT StudentID, Fname, LName, S_LessonNumber, LessonName, Date, Cost
FROM STUDENT_2
JOIN LESSON ON S_LessonNumber = LessonNumber
NATURAL JOIN STUDENT_1
WHERE StudentID = '1001'
The resulting table I get with this query is as follows,
When attempting to display the total amount paid, and the total number of lessons taken, using the following query, I was only able return one row.
SELECT StudentID, Fname, LName, S_LessonNumber, LessonName, Date,
Cost,COUNT( DISTINCT S_LessonNumber ) , SUM( Cost )
FROM STUDENT_2
JOIN LESSON ON S_LessonNumber = LessonNumber
NATURAL JOIN STUDENT_1
WHERE StudentID = '1001'
Is there a way that I can return all 4 rows with the values for COUNT(DISTINCT S_LessonNumber) and SUM(Cost) repeated.
The desired output is as follows:
StudentID FName LName S_LessonNumber LessonName Date Cost COUNT SUM
1001 Hannibal Lecter 7 C--- --- 15 4 60
1001 Hannibal Lecter 6 Wa-- --- 15 4 60
1001 Hannibal Lecter 5 Tri-- --- 15 4 60
1001 Hannibal Lecter 1 Cha- --- 15 4 60
Aggregate functions will always return 1 row. If using subqueries is not a problem, you can do:
SELECT StudentID, Fname, LName, S_LessonNumber, LessonName, Date, Cost,
(SELECT COUNT( DISTINCT S_LessonNumber ) FROM STUDENT_2 JOIN LESSON ON S_LessonNumber = LessonNumber NATURAL JOIN STUDENT_1 WHERE StudentID = '1001') AS COUNT,
(SELECT SUM( Cost ) FROM STUDENT_2 JOIN LESSON ON S_LessonNumber = LessonNumber NATURAL JOIN STUDENT_1 WHERE StudentID = '1001') AS SUM
FROM STUDENT_2
JOIN LESSON ON S_LessonNumber = LessonNumber
NATURAL JOIN STUDENT_1
WHERE StudentID = '1001'
Check it here: http://rextester.com/SBUQ82088
create table if not exists fstudents (id int, fname text, lname text);
create table if not exists fstudents2 (student_id int, lesson_number int, lesson_date date, cost int);
create table if not exists flessons (number int, name text);
insert into fstudents values (1001, 'Anibal', 'Lecter');
insert into flessons values (1, 'Cha Cha');
insert into flessons values (2, 'Waltz');
insert into flessons values (3, 'Country');
insert into flessons values (4, 'Triple2');
insert into fstudents2 values (1001, 1, '2016-10-06', 15);
insert into fstudents2 values (1001, 2, '2016-10-07', 15);
insert into fstudents2 values (1001, 3, '2016-10-08', 15);
insert into fstudents2 values (1001, 4, '2016-10-09', 15);
Use a subquery to count an sum, and then join it to main query.
select st2.student_id, st.fname, st.lname, ls.name, st2.lesson_date, st2.cost, agg.courses, agg.total_cost
from fstudents2 st2
join fstudents st on st2.student_id = st.id
join flessons ls on st2.lesson_number = ls.number
join (select st3.student_id, count(st3.lesson_number) courses, sum(st3.cost) total_cost
from fstudents2 st3
group by st3.student_id) agg on agg.student_id = st2.student_id
where
st2.student_id = 1001;
table a
no name
2001 jon
2002 jonny
2003 mik
2004 mike
2005 mikey
2006 tom
2007 tomo
2008 tommy
table b
code name credits courseCode
A2 JAVA 25 wer
A3 php 25 wer
A4 oracle 25 wer
B2 p.e 50 oth
B3 sport 50 oth
C2 r.e 25 rst
C3 science 25 rst
C4 networks 25 rst
table c
studentNumber grade coursecode
2003 68 A2
2003 72 A3
2003 53 A4
2005 48 A2
2005 52 A3
2002 20 A2
2002 30 A3
2002 50 A4
2008 90 B2
2007 73 B2
2007 63 B3
SELECT a.num, a.Fname,
b.courseName, b.cMAXscore, b.cCode, c.stuGrade
FROM a
INNER JOIN c
ON a.no = c.no
INNER JOIN b
ON c.moduleCode = b.cCode
INNER JOIN b
ON SUM(b.cMAXscore) / (c.stuGrade)
AND b.cMAXscore = c.stug=Grade
GROUP BY a.Fname, b.cMAXscore, b.cCode, b.courseName,c.stuGrade
"calculate and display every student name(a.Fname) and their ID number(a.num) along with their grade (c.grade) versus the coursse name(b.courseName) and the courses max score(b.cMAXscoure). "
I cant figure out how to divide the MAX by the grade, can someone help?
From the specification, it doesn't look like an aggregate function or a GROUP BY would be necessary. But the specification is ambiguous. There's no table definitions (beyond the unfortunate names and some column references).
Definitions of the tables, along with example data and an example of the desired resultset would go a long ways to removing the ambiguity.
Based on the join predicates in the OP query, I'd suggest something like this query, as a starting point:
SELECT a.Fname
, a.num
, c.grade
, b.courseName
, b.cMAXsource
FROM a
JOIN c
ON c.no = a.no
JOIN b
ON b.cCode = c.moduleCode
ORDER
BY a.Fname
, a.num
, c.grade
, b.courseName
, b.cMAXsource
It seems like that would return the specified result (based on my interpretation of the vague specification.) If that's insufficient i.e. if that doesn't return the desired resultset, then in what way does the desired result differ from the result from this query?
(For more help with your question, I suggest you setup a sqlfiddle example with tables and example data. That will make it easier for someone to help you.)
FOLLOWUP
Based on the additional information provided in the question (table definitions and example data...
To get the maximum (highest) grade for a given course, you could use a query like this:
SELECT MAX(c.grade)
FROM c
WHERE c.coursecode = 'A2'
To get the highest grade for all courses:
SELECT c.coursecode
, MAX(c.grade) AS max_grade
FROM c
GROUP BY c.coursecode
ORDER BY c.coursecode
To match the highest grade for each course to each student grade, use that previous query as an inline view in another query. Something like this:
SELECT g.studentNumber
, g.grade
, g.coursecode
, h.coursecode
, h.highest_grade
FROM c g
JOIN ( SELECT c.coursecode
, MAX(c.grade) AS highest_grade
FROM c
GROUP BY c.coursecode
) h
ON h.coursecode = g.coursecode
To perform a calculation, you can use an expression in the SELECT list of the outer query.
For example, to divide the value of one column by another, you can use the division operator:
SELECT g.studentNumber AS student_number
, g.grade AS student_grade
, g.coursecode AS student_coursecode
, h.coursecode
, h.highest_grade
, g.grade / h.highest_grade AS `student_grade_divided_by_highest_grade`
FROM c g
JOIN ( SELECT c.coursecode
, MAX(c.grade) AS highest_grade
FROM c
GROUP BY c.coursecode
) h
ON h.coursecode = g.coursecode
If you want to also return the name of the student, you can perform a join operation to (the unfortunately named) table a. Assuming that studentnumber is UNIQUE in a :
LEFT
JOIN a
ON a.studentnumber = c.studentnumber
And include a.Fname AS student_first_name in the SELECT list.
If you also need columns from table b, then join that table as well. Assuming that coursecode is UNIQUE in b:
LEFT
JOIN b
ON b.coursecode = g.courscode
Then b.credits can be referenced in an expression in the SELECT list.
Beyond that, you need to be a little more explicit about what result should be returned by the query.
If you are after a "total overall grade" for a student, you'd need to specify how that result should be obtained.
Without knowing table definations it is very hard to provide solution to your problem.
Here is my version of what you are trying to look for:
DECLARE #Student TABLE
(StudentID INT IDENTITY,
FirstName VARCHAR(255),
LastName VARCHAR(255)
);
DECLARE #Course TABLE
(CourseID INT IDENTITY,
CourseCode VARCHAR(25),
CourseName VARCHAR(255),
MaxScore INT
);
DECLARE #Grade TABLE
(ID INT IDENTITY,
CourseID INT,
StudentID INT,
Score INT
);
--Student
insert into #Student(FirstName, LastName)
values ('Test', 'B')
insert into #Student(FirstName, LastName)
values ('Test123', 'K')
--Course
insert into #Course(CourseCode, CourseName, MaxScore)
values ('MAT101', 'MATH',100.00)
insert into #Course(CourseCode, CourseName, MaxScore)
values ('ENG101', 'ENGLISH',100.00)
--Grade
insert into #Grade(CourseID, StudentID, Score)
values (1, 1,93)
insert into #Grade(CourseID, StudentID, Score)
values (1, 1,65)
insert into #Grade(CourseID, StudentID, Score)
values (1, 1,100)
insert into #Grade(CourseID, StudentID, Score)
values (2, 1,100)
insert into #Grade(CourseID, StudentID, Score)
values (2, 1,69)
insert into #Grade(CourseID, StudentID, Score)
values (2, 1,95)
insert into #Grade(CourseID, StudentID, Score)
values (1, 2,100)
insert into #Grade(CourseID, StudentID, Score)
values (1, 2,65)
insert into #Grade(CourseID, StudentID, Score)
values (1, 2,100)
insert into #Grade(CourseID, StudentID, Score)
values (2, 2,100)
insert into #Grade(CourseID, StudentID, Score)
values (2, 2,88)
insert into #Grade(CourseID, StudentID, Score)
values (2, 2,96)
SELECT a.StudentID,
a.FirstName,
a.LastName,
c.CourseCode,
SUM(b.Score) AS 'StudentScore',
SUM(c.MaxScore) AS 'MaxCourseScore',
SUM(CAST(b.Score AS DECIMAL(5, 2))) / SUM(CAST(c.MaxScore AS DECIMAL(5, 2))) AS 'Grade'
FROM #Student a
INNER JOIN #Grade b ON a.StudentID = b.StudentID
INNER JOIN #Course c ON c.CourseID = b.CourseID
GROUP BY a.StudentID,
a.FirstName,
a.LastName,
c.CourseCode;
The problem statement doesn't say anything about dividing by the max, I think you're misunderstanding it.
You need to write a subquery that gets the maximum score for each class, using MAX and GROUP BY. You can then join this with the other tables.
SELECT s.name AS student_name, c.name AS course_name, g.grade, m.max_grade
FROM student AS s
JOIN grade AS g ON s.no = g.studentNumber
JOIN course AS c ON c.code = g.courseCode
JOIN (SELECT courseCode, MAX(grade) AS max_grade
FROM grade
GROUP BY courseCode) AS m
ON m.courseCode = c.courseCode
If you did need to divide the grade by the maximum, you can use g.grade/m.max_grade.
I have a table with a cost_maintence column that has cost for the entire year(52) weeks. I also have a table of renters, and a table of renter_units where there is a week_owned column that has the week number the renter rented. I am trying to figure out how I could calculate the cost for each renter. The equation I came up with is:
what each person owes = (cost_maintence/52) * #weeks each renter
rented
Is there any way I could get the value from a query?
create table renters(
id,
lname,
fname,
phone_num);
create table unit(
id,
unit_name,
unit_num,
cost_maintence);
create table renters_unit(
renters_id,
unit_id,
week_owned);
This is the query I came up with but I have no way of testing it out
select r.lname, r.fname, count(ru.week_owned),
sum(u.cost_maintence/52*count(ru.week_owned))
from renters r, renters_units ru, units u
where r.id = ru.renter_id
and ru.unit_id = u.id
and u.unit_name =unitname
and u.unit_num = unitnum
group by lname
order by lname,fname asc;
Here's an example. The inner query will get you amount owed per item, and the outer query sums that to find the total owed per person.
SELECT fname, SUM(owes) AS total_due
FROM (
SELECT r.fname,
r.id,
u.unit_name,
u.cost_maintence/52*COUNT(ru.week_owned) AS owes
FROM renters AS r
INNER JOIN renters_unit AS ru ON r.id = ru.renters_id
INNER JOIN unit AS u ON u.id = ru.unit_id
GROUP BY r.id, u.id
) AS t
GROUP BY id
Try it out with a SQLFiddle demo
Example Schema:
create table renters(
id int,
lname varchar(20),
fname varchar(20),
phone_num varchar(20));
create table unit(
id int,
unit_name varchar(30),
unit_num int,
cost_maintence int);
create table renters_unit(
renters_id int,
unit_id int,
week_owned int);
INSERT INTO renters VALUES (1, 'Peterson', 'Chaz', '8675309');
INSERT INTO unit VALUES (1, 'Skateboard', 1337, 52);
INSERT INTO unit VALUES (2, 'Flamethrower', 5432, 104);
INSERT INTO renters_unit VALUES (1, 1, 1);
INSERT INTO renters_unit VALUES (1, 1, 2);
INSERT INTO renters_unit VALUES (1, 1, 4);
INSERT INTO renters_unit VALUES (1, 2, 4);
INSERT INTO renters_unit VALUES (1, 2, 5);
By this, we can see that Chaz should owe $7 for the year (had a skateboard for 3 weeks at $1 per week, and a flamethrower for 2 weeks at $2 per week).
The inner query gives the following:
FNAME UNIT_NAME OWES
Chaz Skateboard 3
Chaz Flamethrower 4
And the outer:
FNAME TOTAL_DUE
Chaz 7
SELECT t.renters_id, SUM(u.cost_maintence)/52
FROM unit u JOIN renters_unit t ON t.unit_id = u.id
GROUP BY t.renters_id
I have a temp table similar to this
create table #temp
(
log_id int,
...some other fields,
one_user_id int,
two_user_id int,
three_user_id int,
four_user_id int,
one_username varchar(50),
two_username varchar(50),
three_username varchar(50),
four_username varchar(50)
)
I start out knowing all the user ids, but then I need to look up their names in a user lookup table and update the name fields in the temp table.
create table #user_lookup
(
user_id int,
username varchar(50)
)
I know I could join to the user lookup table once for every id using a different alias to get them all, but I was looking for a slick way to do it just once.
Any ideas ?
EDIT:
Ok, more info on the purpose for multiple users per row. The #temp table row (not all fields displayed) signifies a log entry that represents a collation of multiple actions by potentially multiple users, but all tying to that one log row.
I could have duplicate log rows, one for each user who played a role, but it's easier to consume on the client side as single rows.
This is why there are multiple users per row.
I think this should work:
UPDATE temp
SET one_username = u1.username
, two_username = u2.username
, three_username = u3.username
, four_username = u4.username
FROM #temp as temp
join #user_lookup as u1 on u1.user_id = temp.one_user_id
join #user_lookup as u2 on u2.user_id = temp.two_user_id
join #user_lookup as u3 on u3.user_id = temp.three_user_id
join #user_lookup as u4 on u4.user_id = temp.four_user_id
But I don't know why you have four users in one table... ;)
The only other real alternative solution is to pull in the related records with an IN clause and make use of CASE statements to tie the usernames with the correct user_id's. However this is way more complicated than simply using a JOIN statement and doesn't really offer any advantage except that there aren't multiple JOIN's involved. Here is a complete working sample of how to pull data using this structure:
create table #temp
(
one_user_id int,
two_user_id int,
three_user_id int,
four_user_id int,
one_username varchar(50),
two_username varchar(50),
three_username varchar(50),
four_username varchar(50)
)
insert #temp (one_user_id, two_user_id, three_user_id, four_user_id) values (1, 3, 6, 7)
insert #temp (one_user_id, two_user_id, three_user_id, four_user_id) values (2, 5, 8, 1)
;with User_Lookup as (
select 1 as user_id, 'abc' as username union
select 2, 'def' union
select 3, 'ghi' union
select 4, 'jkl' union
select 5, 'mno' union
select 6, 'pqr' union
select 7, 'stu' union
select 8, 'vwx' union
select 9, 'jon' union
select 10, 'bob'
), Result as (
select
one_user_id,
two_user_id,
three_user_id,
four_user_id,
max(case when U.user_id = one_user_id then U.username end) as one_username,
max(case when U.user_id = two_user_id then U.username end) as two_username,
max(case when U.user_id = three_user_id then U.username end) as three_username,
max(case when U.user_id = four_user_id then U.username end) as four_username
from
#Temp T,
User_Lookup U
where
U.user_id in (T.one_user_id, T.two_user_id, T.three_user_id, T.four_user_id)
group by
T.one_user_id, T.two_user_id, T.three_user_id, T.four_user_id
)
update
#temp
set
one_username = R.one_username,
two_username = R.two_username,
three_username = R.three_username,
four_username = R.four_username
from
Result R
inner join
#temp T on R.one_user_id=T.one_user_id and R.two_user_id=T.two_user_id
and R.three_user_id=T.three_user_id and R.four_user_id=T.four_user_id
select * from #temp
drop table #temp
Output:
one_user_id two_user_id three_user_id four_user_id one_username two_username three_username four_username
1 3 6 7 abc ghi pqr stu
2 5 8 1 def mno vwx abc
Ok, I have a query over two tables. I need to get two sums. I do a group by so the sum() works correctly.
SELECT sum(a.x), sum(b.y) FROM a,b GROUP BY a.n where a.n=b.m
So far this works well, but the problem is i need to group them differently for the second sum (sum(b.y)), than for the first sum (sum(a.x)).
The real query is somewhat more complex but this is my main problem.
This is what i actually try to select sum(stock.amount) - if( sold.amount IS NULL , 0, sum( sold.amount ) )
How can I solve that in one query?
since you are not writing down the tables I am gonna make a wild guess and assume the tables are like :
stock : id, item_id, amount
sold : id, item_id, amount
then again I assume that you need the stock_in_total, sold_total, left_total counts
SELECT
stock_sums.item_id,
stock_sums.st_sum as stock_in_total,
COALESCE(sold_sums.so_sum,0) as sold_total,
(stock_sums.st_sum - COALESCE(sold_sums.so_sum,0)) as left_total
FROM (
SELECT stock.item_id as item_id, SUM(stock.amount) as st_sum
FROM stock
GROUP BY item_id
) as stock_sums
LEFT JOIN (
SELECT sold.item_id as item_id, SUM(sold.amount) as so_sum
FROM sold
GROUP by item_id
) as sold_sums ON stock_sums.item_id = sold_sums.item_id
I hope this would help.
Here is how I would do it. I assume that Stock is the main table, with an ID and an amount, and that Sold maps to Stock via an ID value, and has zero to many records for each Stock item.
SELECT Q1.id, Q1.Total1, Q2.Total2
, Q1.Total1 - COALESCE(Q2.Total2,0) as Outstanding
FROM (
SELECT id, SUM(amount) as Total1
FROM Stock GROUP BY id
) as Q1
LEFT OUTER JOIN (
SELECT id, SUM(Amount) as Total2
FROM Sold GROUP BY id
) as Q2
ON Q2.id = Q1.id
Note that simply formatting your SQL into a clean way forces you to break it into logical parts and will often reveal exactly what is wrong with the query.
The example above also handles correctly the cases where there is not match in the Sold table.
Cheers,
Daniel
(Code Assumptions)
DROP TABLE Stock
CREATE TABLE Stock (
id integer
, amount decimal(10,2)
)
INSERT INTO Stock (id, amount ) VALUES ( 1, 10.1);
INSERT INTO Stock (id, amount ) VALUES ( 2, 20.2);
INSERT INTO Stock (id, amount ) VALUES ( 3, 30.3);
SELECT * FROM STOCK
DROP TABLE Sold
CREATE TABLE Sold (
id integer
, amount decimal(10,2)
)
INSERT INTO Sold (id, amount ) VALUES ( 1, 1.1);
INSERT INTO Sold (id, amount ) VALUES ( 1, 2.2);
INSERT INTO Sold (id, amount ) VALUES ( 1, 3.3);
INSERT INTO Sold (id, amount ) VALUES ( 2, 2.22);
SELECT * FROM Sold
SELECT Q1.id, Q1.Total1, Q2.Total2
, Q1.Total1 - COALESCE(Q2.Total2,0) as Outstanding
FROM (
SELECT id, SUM(amount) as Total1
FROM Stock GROUP BY id
) as Q1
LEFT OUTER JOIN (
SELECT id, SUM(Amount) as Total2
FROM Sold GROUP BY id
) as Q2
ON Q2.id = Q1.id
Results:
id Total1 Total2 Outstanding
1 10.10 6.60 3.50
2 20.20 2.22 17.98
3 30.30 30.30
REVISION
It sounds like you want the total amount of stock you have as one count for each different stock. Then you want how much stock you have left for each stock based on what has been sold. Correct?
If so check this out:
select stock, sum(a.x) as sharesBeforeSale, (sum(a.x) - sum(b.y)) as sharesAfterSale
FROM db.table1 a, db.table2 b
WHERE a.UNIQUEID = b.UNIQUEID AND b.y IS NOT NULL
GROUP BY a.UNIQUEID;
Does that accomplish what you are looking to do?
stock sharesBeforeSale sharesAfterSale
duk 100 25
orc 101 101
yrc 54 41
Enjoy!
Sample tables
db.table1 (stock owned):
UNIQUEID x stock
1 100 duk
2 101 orc
3 54 yrc
db.table2 (stock sold):
UNIQUEID y
1 75
2 0
3 13