I am just a beginner in MySql and I am trying to display the worker ID, full name and the maximum number of assignments issued to a particular employee(worker). The two tables worker and assignment are related through Wrk_ID.
My code is not working.
SELECT w.Wrk_ID,
CONCAT_WS(" ", Wrk_FirstName, Wrk_Initial, Wrk_LastName) AS FullName,
COUNT(a.Wrk_ID) AS count
FROM worker w LEFT JOIN assignment a
ON w.Wrk_ID = a.Wrk_ID
GROUP BY w.Wrk_ID
HAVING count = ALL(SELECT MAX(a.Wrk_ID) FROM assignment GROUP BY Wrk_ID);
Really need assistance!...Thanks
Try this
SELECT distinct w.Wrk_ID,
CONCAT_WS(" ", Wrk_FirstName, Wrk_Initial, Wrk_LastName) AS FullName,
COUNT(a.Wrk_ID) AS count
FROM worker w LEFT JOIN assignment a
ON w.Wrk_ID = a.Wrk_ID
GROUP BY w.Wrk_ID
HAVING count >= ALL(SELECT count(Wrk_ID) FROM assignment GROUP BY Wrk_ID);
SQL Fiddle - http://sqlfiddle.com/#!9/b6e2b/10
Related
I am struggling in finding the solution on how to connect 5 different tables, but with different foreign keys. The table looks like that:
I need to write a query that would return all customers visit costs and payment differences (debt or overpay) and the last payment date on selected dates:
SELECT Cusomer ID, Name, Surname, SUM(Visit fee + materials price sum + service sum) - SUM(Payment amount) AS pay_diff, MAX(payment_date) WHEN DATE IS BETWEEN (LAST MONTH) AND (TODAY)
Expected Output choosing between 01-07-2021 and today:
Update: my SQL tables and db response:
customers
customer_visit
visit_services
visit_materials
customer_payments:
Query calculations:
select c.ID, c.Name, c.Surname
sum(cv.visit_fee) + sum(vm.Price_sum) + sum(vs.Service_sum) - sum(cp.Payment_amount) as pay_diff, max(payment_date)
from customer c
left join customer_visit cv
on c.ID = cv.customer_id
left join Visit_materials vm
on cv.ID = vm.Visit_ID
left join Visit_services vs
on cv.ID = vs.Visit_ID
left join Customer_payment cp
as c.ID = cp.Customer_id
where Payment_date >= '01-07-2021'
group by c.ID, c.Name, c.Surname
I noticed that the column names are inconsistent (diagram vs. table samples), you may need to adjust according to actual DB. If you got future date data, you can use between '01-07-2021' and 'xx-xx-2021' inside where clause. I hope it helps. If you get errors, can you post a screenshot of the error message?
for my studies i need to get a code working. I do have two tables:
Training:
UserID
Date
Uebung
Gewicht
Wiederholungen
Mitglied:
UserID
Name
Vorname
and i need to display the max power which you get if you multiply 'Wiederholungen' with 'Gewicht' from the 'Training' table for EACH User with the date and name.
I know there is a "problem" with max() and group by. But i'm kinda new to MySQL and i was only able to find fixes with one table and also every column already existing. I have to join two tables AND create the power column.
I tried a lot and i think this may be my best chance
select name, vorname, x.power from
(SELECT mitglied.UserID,max( Wiederholungen*Gewicht) as Power
FROM training join mitglied
where Uebung = 'Beinstrecker'
and training.UserID = mitglied.UserID
group by training.UserID) as x
inner join (training, mitglied)
on (training.UserID = mitglied.UserID)
and x.Power = Power;
'''
I get way too many results. I know the last statement is wrong (x.power = power) but i have no clue how to solve it.
This is actually a fairly typical question here, but I am bad a searching for previous answers so....
You "start" in a subquery, finding those max values:
SELECT UserID, Uebung, MAX(Gewicht*Wiederholugen) AS Power
FROM training
WHERE Uebung = 'Beinstrecker'
GROUP BY UserID, Uebung
Then, you join that back to the table it came from to find the date(s) those maxes occurred:
( SELECT UserID, Uebung, MAX(Gewicht*Wiederholugen) AS Power
FROM training
WHERE Uebung = 'Beinstrecker'
GROUP BY UserID, Uebung
) AS maxes
INNER JOIN training AS t
ON maxes.UserID = t.UserID
AND maxes.Uebeng = t.Uebeng
AND maxes.Power = (t.Gewicht*t.Wiederholugen)
Finally, you join to mitglied to get information for the user:
SELECT m.name, m.vorname, maxes.Power
FROM ( SELECT UserID, Uebung, MAX(Gewicht*Wiederholugen) AS Power
FROM training
WHERE Uebung = 'Beinstrecker'
GROUP BY UserID, Uebung
) AS maxes
INNER JOIN training AS t
ON maxes.UserID = t.UserID
AND maxes.Uebeng = t.Uebeng
AND maxes.Power = (t.Gewicht*t.Wiederholugen)
INNER JOIN mitglied AS m ON t.UserID = m.UserID
;
Note: t.Uebung = 'Beinstrecker' could be used as a join condition instead, and might be faster; but as a matter of style I try to prevent redundant literals like that unless there is a worthwhile performance difference.
I am performing the following query having some syntax error:
SELECT count (tbl_staff.staff_id as staff_number),SELECT count (tbl_client.client_id as client_number),SELECT count (tbl_appointment.appt_id as appt_number),SELECT count (tbl_subscription.subscription_id as subscription_number)
FROM tbl_subscription
LEFT JOIN tbl_staff
ON (
tbl_staff.merchant_id = tbl_subscription.merchant_id)
LEFT JOIN tbl_appointment
ON (
tbl_appointment.merchant_id = tbl_subscription.merchant_id)
LEFT JOIN tbl_client
ON (
tbl_client.merchant_id = tbl_subscription.merchant_id)
WHERE tbl_subscription.subscription_id=1;
I want get the count of staff_id, client_d, appointment_id on particular Subscription_id.
Your select list is close, but has a few mistakes. Namely, you only need a single SELECT in your query (not one per field) and the "as ..." descriptor belongs outside the parenthesis.
So this part of the query
SELECT count (tbl_staff.staff_id as staff_number),
SELECT count (tbl_client.client_id as client_number),
SELECT count (tbl_appointment.appt_id as appt_number),
SELECT count (tbl_subscription.subscription_id as subscription_number)
FROM tbl_subscription
would become
SELECT count (tbl_staff.staff_id) as staff_number,
count (tbl_client.client_id) as client_number,
count (tbl_appointment.appt_id) as appt_number,
count (tbl_subscription.subscription_id) as subscription_number
FROM tbl_subscription
I have a user table ldap_karen that has all employee data including sup_id (supervisor id) and the employee's job role.
I also I have a supervisor table that has all the unique sup_ids.
Currently I have:
SELECT
ldap_sups.sup_id,
COUNT(ldap_karen.uid) as team_nums,
COUNT(sa_title) as title_nums,
(SELECT sa_title
FROM ldap_karen
WHERE ldap_sups.sup_id=ldap_karen.sup_id
group by sa_title
order by count(ldap_karen.sa_title) desc
limit 1) as sup_role,
ldap_karen.sa_title,
ldap_karen.regionname
FROM
ldap_sups
JOIN ldap_karen ON ldap_sups.sup_id = ldap_karen.sup_id
GROUP BY sup_id
So this gives me the supervisor ID, then number of team members, number of job titles (not all employees have job titles), sup_role - this is what is not working, sa_title (which just lists the job title of the last person in the list), and region.
I would like sup_role to display the "most" job title. So whatever job titles comes up the most for the employee under the supervisor is the sup_role.
Note: Right now my query just runs - due to an issue with the "sup_role" query. Nothing outputs.
This should work, but its not the best way:
SELECT
ldap_sups.sup_id,
COUNT(ldap_karen.uid) as team_nums,
COUNT(ldap_karen.sa_title) as title_nums,
sums_title.sa_title as sup_role,
ldap_karen.sa_title,
ldap_karen.regionname
FROM
ldap_sups
JOIN ldap_karen ON ldap_sups.sup_id = ldap_karen.sup_id
LEFT JOIN (
SELECT sup_id, max(sum), sa_title from
(SELECT sup_id, count(sa_title) as sum, sa_title
FROM ldap_karen
group by sa_title,sup_id) supsup
group by sup_id
) as sums_title on sums_title.sup_id = ldap_sups.sup_id
GROUP BY sup_id
I have a MySQL query that outputs to a php table but I'm having issues in joining two tables that both use a COUNT:
$query = "SELECT mqe.registration,
COUNT(*) AS numberofenqs,
COUNT(DISTINCT ucv.ip) AS unique_views,
SUM(ucv.views) AS total_views
FROM main_quick_enquiries AS mqe
LEFT OUTER JOIN used_car_views AS ucv
ON ucv.numberplate = mqe.registration
WHERE mqe.registration IS NOT NULL
GROUP BY mqe.registration ORDER BY numberofenqs DESC";
The query runs, but the number within the numberofenqs column is always wrong as i know from performing that query on its own that it comes in with the correct result:
SELECT registration, COUNT(*) AS numberofenqs FROM main_quick_enquiries GROUP BY registration ORDER BY numberofenqs DESC
Why is the COUNT(*) not working correctly in top query code and where is it getting the figures from?
it could be because of LEFT OUTER JOIN ...
Try to run this:
SELECT registration
, count(*)
FROM main_quick_enquiries
GROUP BY registration
and compare it with this result
SELECT mqe.registration
, count(*)
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
GROUP BY mqe.registration
There could be a problem :) in duplicity rows... try to find one specific registration number, and compare the details of both query
SELECT *
FROM main_quick_enquiries
WHERE registration = XXXX
+
SELECT *
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
WHERE registration = XXXX
you should see the diffs
Thanks All, but I think I've nailed it with COUNT(DISTINCT mqe.id) instead of COUNT(*).