Select Row for Max Sum Value In Multiple Tables MYSQL - mysql

I need to query for the users with highest amount of sales by all projects, where the users are in users table, sales in units table, projects in projects table.
Projects Top Agent Total Sales for Project
Project A User A 100000
Project B User B 20000
Project C User A 1000
Project D - 0
The Projects column is list all the projects regardless it has sales or not.
The Top Agent column is list the user with the highest sales in the project.
The Total Sales for Project is the total sales for a projects.
The agent column i got is incorrect because there is someone else has the highest sales, the query seems to return the first row of the result
SELECT projects, pid, CASE WHEN agent is null THEN '-' ELSE agent END as agent,
CASE WHEN FORMAT(topagent,0) > 0 THEN FORMAT(topagent,0) ELSE 0 END as salesvolume
FROM (
SELECT projects.name as projects, projects.id as pid,
concat(users.f_name, ' ', users.l_name) as agent,
SUM(units.price) AS topagent
FROM users inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE units.status = 'Sold'
GROUP BY pid
union
select projects.name as projects, projects.id as pid,
concat(users.f_name, ' ', users.l_name) as agent,
SUM(units.price) AS topagent
from projects left outer join types on projects.id = types.project_id
left outer join units on types.id = units.types_id and units.status = 'Sold'
left outer join bookings on units.id = bookings.unit and units.status = 'Sold'
left outer join users on bookings.agent_id = users.id and units.status = 'Sold'
group by pid
) a
GROUP BY pid
order by topagent desc

Try it if helps you-
SELECT a.prjname, IFNULL(usr.name,'-') AS Top_Agent, SUM(a.sale) AS Total_Sales_for_Project
FROM
(
SELECT prj.id AS prjid,prj.name AS prjname,usr.id,usr.name AS usrname,IFNULL(SUM(unit.price),0) AS sale
FROM projects AS prj
LEFT JOIN `types` AS typ ON typ.project_id=prj.id
LEFT JOIN units AS unt ON unt.type_id=typ.id AND unt.status='sold'
LEFT JOIN bookings bkg ON bkg.unit=unt.id
LEFT JOIN users usr ON usr.id=bkg.agent_it
GROUP BY prj.id,usr.id
ORDER BY prj.id,usr.id,sale DESC
) a
GROUP BY a.prjid

Your column aliases are confusing to read. In English, it seems what you mean by topagent is "sum of sales by a human". But in SQL, your GROUP BY pid means that the SUM(units.price) really means "sum of sales in a project".
Then the UNION adds a list of projects to a list of users. The agent names are basically random at this point.
If I decipher the requirements as "a list of projects ranked by the sales values of each project's top sales agent", then you'd have SQL as below:
SELECT
pid,
projects.name as project_name,
IFNULL(a.top_agent_name,'-') as top_agent_name,
CASE WHEN FORMAT(top_agent_sales,0) > 0 THEN FORMAT(top_agent_sales,0) ELSE 0 END as top_agent_salesvolume
FROM
projects
JOIN
SELECT
a.pid,
a.agent_name as top_agent_name,
a.agent_sales as top_agent_sales
FROM
(SELECT
projects.id as pid,
concat(users.f_name, ' ', users.l_name) as agent_name,
SUM(units.price) AS agent_sales
FROM users
inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE units.status = 'Sold'
GROUP BY pid, users.id
) a # get all agents for all projects
JOIN
(SELECT
MAX(agent_sales) as max_project_agent_sales
FROM
(SELECT
projects.id as pid,
SUM(units.price) AS agent_sales
FROM users
inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE units.status = 'Sold'
GROUP BY pid, users.id
)
GROUP BY pid) b ON a.pid = b.pid
WHERE
a.agent_sales = b.max_project_agent_sales
ORDER BY a.agent_sales desc
Old answer below:
There are 2 topagents for each pid in the inner query since it's a union of 2 group bys. There isn't a reducing function in the outer group by pid so the topagent returned in the select is the first one that came up in the inner query.

Related

Showing maximum

I want to show maximum of guarantee which specific user has. For example user has bought 3 items which have 1,2,5 years guarantee. So I want to show 5 years guarantee and name of this product.
I did subquery in case few products have this same guarantee.
SELECT t.`id-user`, name, guarantee FROM transactions t
JOIN user u ON `t`.`id-user` = `u`.`id-user`
JOIN products p ON `p`.`id-product = `t`.`id-product`
WHERE guarantee = (SELECT MAX(p2.guarantee)
FROM products p2
WHERE `p2`.`id-product` = `p`.`id-product`)
This query shows all products and their guarantees.
I think the simplest method is the substring_index()/group_concat() method for getting values associated with a maximum/minimum:
SELECT t.iduser, u.name,
MAX(p.guarantee) as guarantee,
SUBSTRING_INDEX(GROUP_CONCAT(p.name ORDER BY p.guarantee DESC), ',', 1)
FROM transactions t JOIN
user u
ON t.iduser = u.iduser JOIN
products p
ON p.idproduct = t.idproduct
GROUP BY t.iduser, u.name;
You can use your method too, but the correlated subquery is tricky:
SELECT t.iduser, u.name, p.guarantee, p.name
FROM transactions t JOIN
user u
ON t.iduser = u.iduser JOIN
products p
ON p.idproduct = t.idproduct
WHERE p.guarantee = (SELECT MAX(p2.guarantee)
FROM transactions t2 JOIN
products p2
ON p2.idproduct = t2.idproduct
WHERE t2.iduser = u.iduser
);
I think it work.
select [User].Name as [UserName],
Product.MaxGuarantee,
Product.Name as Product_Name
from [Users] [User]
left join Transactions [Transaction]
on [Transaction].[User] = [User].ID
cross apply(
select max(guarantee) MaxGuarantee, Name
from Products
where ID = [Transaction].Product
) Product
where [User].ID = ''

How to solve difference in number of records from a union query

I have a list of technicians and their number of patients, when I click on a technician I get the list of patients and their details.
To do this, I have query that Returns the number of patients per technician and another returns the records of the patients.
SELECT *, SUM(Rcount) as Number_of_patients
FROM
(
SELECT users.users_id, users.name, patients.patients_id, count(*) as Rcount
FROM pecs
INNER JOIN users ON pecs.techniciens_id = users.users_id
INNER JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
GROUP BY users_id
UNION ALL
SELECT users.users_id, users.name, patients.patients_id, count(*) as Rcount
FROM followup
INNER JOIN users ON followup.technician_id = users.users_id
INNER JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN pecs ON pecs.pecs_id = followup.pecs_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
GROUP BY users_id
)x
GROUP BY users_id ORDER BY last_name ASC
the result is:
users_id | name | Number_of_patients
40 | ABABAB | 223
that is 223 patients for technician_ID = 40
Now to view a list of the patients for this technician I have the following query:
SELECT *
FROM
(
SELECT patients.patients_id, patients.name
FROM pecs
LEFT JOIN users ON pecs.techniciens_id = users.users_id
LEFT JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
WHERE pecs.techniciens_id = 40
#GROUP BY patients_id
UNION ALL
SELECT patients.patients_id, patients.name
FROM followup
LEFT JOIN users ON followup.technician_id = users.users_id
LEFT JOIN titles ON users.titles_id = titles.titles_id
LEFT JOIN pecs ON pecs.pecs_id = followup.pecs_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
WHERE followup.technician_id = 40
#GROUP BY patients_id
)x
GROUP BY patients_id ORDER BY last_name ASC
Now, I get the same number of records (223) but there are duplicates rows of patients ...I need help on how to get the correct number of patients for each technician without duplicates.
Can anyone please help?
I am not sure I understood exactly your question. Anyway, I would try to keep things as simple as possibile. Regarding your "count" query, you could start from something like this, where I removed filed I think should not be useful for counting. This query should give you count avoiding multiple id (I use DISTINCT in the two select and UNION to remove possible duplicate between the two select). May be some join could be eliminated (but I don't know the whole structure). Please follow Tim B. suggestion about formatting and care in posting a question.
SELECT USERS_ID, COUNT(*) AS PATIENTS_COUNT
FROM (
SELECT DISTINCT USERS.USERS_ID, PATIENTS.PATIENTS_ID
FROM PECS
INNER JOIN USERS ON PECS.TECHNICIENS_ID = USERS.USERS_ID
INNER JOIN TITLES ON USERS.TITLES_ID = TITLES.TITLES_ID
INNER JOIN PATIENTS ON PATIENTS.PATIENTS_ID = PECS.PATIENTS_ID
UNION
SELECT DISTINCT USERS.USERS_ID, PATIENTS.PATIENTS_ID
FROM FOLLOWUP
INNER JOIN USERS ON FOLLOWUP.TECHNICIAN_ID = USERS.USERS_ID
INNER JOIN TITLES ON USERS.TITLES_ID = TITLES.TITLES_ID
INNER JOIN PECS ON PECS.PECS_ID = FOLLOWUP.PECS_ID
INNER JOIN PATIENTS ON PATIENTS.PATIENTS_ID = PECS.PATIENTS_ID
) A

SUM multiple Count and Group by

I need a count of total actions id (calls, meetings and tasks) group by Username and the name Accounts
I try this, but the total isn't correct
SELECT count(calls.id) + count(meetings.id) + count(tasks.id) AS 'total', users.user_name AS 'name', GROUP_CONCAT(accounts.name) AS 'accounts'
FROM accounts, calls, users, meetings, tasks
WHERE accounts.id = calls.parent_id
AND calls.assigned_user_id = users.id
AND accounts.id = meetings.parent_id
AND meetings.assigned_user_id = users.id
AND accounts.id = tasks.parent_id
AND tasks.assigned_user_id = users.id
GROUP BY name
Without the benefit of having representative data to test with, my guess would be that the joining 5 tables has multiplied the rows and hence the totals are incorrect due to that. Using DISTINCT inside the COUNT() might help, e.g.
SELECT
COUNT(DISTINCT calls.id)
+ COUNT(DISTINCT meetings.id)
+ COUNT(DISTINCT tasks.id) AS 'total'
, users.user_name AS 'name'
, GROUP_CONCAT(DISTINCT accounts.name) AS 'accounts'
FROM accounts
INNER JOIN calls ON accounts.id = calls.parent_id
INNER JOIN users ON calls.assigned_user_id = users.id
INNER JOIN meetings ON accounts.id = meetings.parent_id
AND meetings.assigned_user_id = users.id
INNER JOIN tasks ON accounts.id = tasks.parent_id
AND tasks.assigned_user_id = users.id
GROUP BY
users.user_name
;
Note I have swapped the old way of joining through the where clause for a more modern approach, you really should join in.
Another possibility is that your counts are incorrect because you are using INNER JOINS which require the existence of data in both tables for rows to be returned. So maybe you need some LEFT OUTER JOINs instead.
SELECT
COUNT(DISTINCT calls.id)
+ COUNT(DISTINCT meetings.id)
+ COUNT(DISTINCT tasks.id) AS 'total'
, users.user_name AS 'name'
, GROUP_CONCAT(DISTINCT accounts.name) AS 'accounts'
FROM accounts
LEFT OUTER JOIN calls ON accounts.id = calls.parent_id
LEFT OUTER JOIN users ON calls.assigned_user_id = users.id
LEFT OUTER JOIN meetings ON accounts.id = meetings.parent_id
AND meetings.assigned_user_id = users.id
LEFT OUTER JOIN tasks ON accounts.id = tasks.parent_id
AND tasks.assigned_user_id = users.id
GROUP BY
users.user_name
;
The final query might be a mixture of joins, some INNER and some LEFT.
`SELECT COUNT(calls.id) + COUNT(meetings.id) + COUNT(tasks.id) AS total, GROUP_CONCAT(users.user_name) AS name, GROUP_CONCAT(accounts.name) AS accounts
FROM accounts JOIN calls ON (accounts.id = calls.parent_id)
JOIN users ON (calls.assigned_user_id = users.id)
JOIN meetings ON (meetings.assigned_user_id = users.id)
JOIN tasks ON (accounts.id = tasks.parent_id and tasks.assigned_user_id = users.id)
GROUP BY users.user_name, accounts.name`

Output Name Field is DIfferent With the Output without Name Field MYSQL

SELECT users.id as uid, projects.id as pid
FROM users
inner join usergroup on usergroup.id = users.user_group
inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE bookings.status = 'Accepted' AND units.status = 'Sold'
GROUP BY pid
with the query above, i get the correct and logic output:
pid | uid
1 1
2 1
9 12
10 14
then i want to show the user's name, so i added 1 field in query as shown below:
SELECT users.id as uid, users.f_name, projects.id as pid
FROM users
inner join usergroup on usergroup.id = users.user_group
inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE bookings.status = 'Accepted' AND units.status = 'Sold'
GROUP BY pid
but i got a different output for uid:
pid | uid
1 1
2 1
9 9
10 11
This is not logic and anyone know why? Let's assume both are correct but why will output different result?
Your 2nd query you are filtering data with join condition before where condition while in 1st query you are filtering data just in where clause...keep you 2nd query same as 1st to get same results....
try below query-
SELECT projects.id as pid, users.id as uid, users.f_name
FROM users
inner join usergroup on usergroup.id = users.user_group
inner join bookings on bookings.agent_id = users.id
inner join units on bookings.unit = units.id
inner join types on types.id = units.types_id
inner join projects on projects.id = types.project_id
WHERE bookings.status = 'Accepted' AND units.status = 'Sold'
GROUP BY pid
One project ID can be associated to many units and each unit with many bookings and hence with many users. You group by project ID, but you don't specify which of all associated users you want to see for a project ID. You would do this with an aggregate function such as MIN(users.id), MAX(users.id), etc. But you don't use such function, thus telling MySQL: "just give me randomly one of the matching users".
As long as you keep your query as is, it seems that MySQL always gives you the same users, maybe the first it finds. This is by no way guaranteed; you could just as well get different users with the same query.
Now that you changed your query, MySQL goes another route and picks different matching users.

mySQL Sub Select needed

I have three tables, libraryitems, copies and loans.
A libraryitem hasMany copies, and a copy hasMany loans.
I'm trying to get the latest loan entry for a copy only; The query below returns all loans for a given copy.
SELECT
libraryitems.title,
copies.id,
copies.qruuid,
loans.id AS loanid,
loans.status,
loans.byname,
loans.byemail,
loans.createdAt
FROM copies
INNER JOIN libraryitems ON copies.libraryitemid = libraryitems.id AND libraryitems.deletedAt IS NULL
LEFT OUTER JOIN loans ON copies.id = loans.copyid
WHERE copies.libraryitemid = 1
ORDER BY copies.id ASC, loans.createdAt DESC
I know there needs to be a sub select of some description in here, but struggling to get the correct syntax. How do I only return the latest, i.e MAX(loans.createdAt) row for each distinct copy? Just using group by copies.id returns the earliest, rather than latest entry.
Image example below:
in the subquery , getting maximum created time for a loan i.e. latest entry and joining back with loans to get other details.
SELECT
T.title,
T.id,
T.qruuid,
loans.id AS loanid,
loans.status,
loans.byname,
loans.byemail,
loans.createdAt
FROM
(
SELECT C.id, C.qruuid, L.title, MAX(LN.createdAt) as maxCreatedTime
FROM Copies C
INNER JOIN libraryitems L ON C.libraryitemid = L.id
AND L.deletedAt IS NULL
LEFT OUTER JOIN loans LN ON C.id = LN.copyid
GROUP BY C.id, C.qruuid, L.title) T
JOIN loans ON T.id = loans.copyid
AND T.maxCreatedTime = loans.createdAt
A self left join on loans table will give you latest loan of a copy, you may join the query to the other tables to fetch the desired output.
select * from loans A
left outer join loans B
on A.copyid = B.copyid and A.createdAt < B.createdAt
where B.createdAt is null;
This is your query with one simple modification -- table aliases to make it clearer.
SELECT li.title, c.id, c.qruuid,
l.id AS loanid, l.status, l.byname, l.byemail, l.createdAt
FROM copies c INNER JOIN
libraryitems li
ON c.libraryitemid = li.id AND
li.deletedAt IS NULL LEFT JOIN
loans l
ON c.id = l.copyid
WHERE c.libraryitemid = 1
ORDER BY c.id ASC, l.createdAt DESC ;
With this as a beginning let's think about what you need. You want the load with the latest createdAt date for each c.id. You can get this information with a subquery:
select l.copyid, max(createdAt)
from loans
group by l.copyId
Now, you just need to join this information back in:
SELECT li.title, c.id, c.qruuid,
l.id AS loanid, l.status, l.byname, l.byemail, l.createdAt
FROM copies c INNER JOIN
libraryitems li
ON c.libraryitemid = li.id AND
li.deletedAt IS NULL LEFT JOIN
loans l
ON c.id = l.copyid LEFT JOIN
(SELECT l.copyid, max(l.createdAt) as maxca
FROM loans
GROUP BY l.copyid
) lmax
ON l.copyId = lmax.copyId and l.createdAt = lmax.maxca
WHERE c.libraryitemid = 1
ORDER BY c.id ASC, l.createdAt DESC ;
This should give you the most recent record. And, the use of left join should keep all copies, even those that have never been leant.