I have this table below as a result
SELECT
doctors.`name`,
COUNT(`doctor-barge-naghs`.`code-naghs`) AS 'countEachDoctor'
FROM
doctors
INNER JOIN `doctor-barge-naghs` ON `doctor-barge-naghs`.`code-doctor` = doctors.id
GROUP BY doctors.`name`
and I want to calculate the SUM 'countEachDoctor' field and show
it beside of each row.
I did this
SELECT t1.*,(SELECT SUM(t1.countEachDoctor))
FROM(
SELECT
doctors.`name`,
COUNT(`doctor-barge-naghs`.`code-naghs`) AS 'countEachDoctor'
FROM
doctors
INNER JOIN `doctor-barge-naghs` ON `doctor-barge-naghs`.`code-doctor` = doctors.id
GROUP BY doctors.`name`) AS t1
it is what I wanted but unfortunately ,it just show one records,I need all records.
IF you are not using mysql 8, you can achieve it like this:
SELECT
doctors.`name`,
COUNT(`doctor-barge-naghs`.`code-naghs`) AS 'countEachDoctor',
(SELECT SUM(t.countEachDoctor)
FROM (
SELECT
COUNT(`doctor-barge-naghs`.`code-naghs`) AS 'countEachDoctor'
FROM doctors
INNER JOIN `doctor-barge-naghs` ON `doctor-barge-naghs`.`code-doctor` = doctors.id
GROUP BY doctors.`name`) t) AS sumCount
FROM
doctors
INNER JOIN `doctor-barge-naghs` ON `doctor-barge-naghs`.`code-doctor` = doctors.id
GROUP BY doctors.`name`
Related
How to show multiple table list in mysql
I have a query that has two tables
one for patients and the second one for patients who has tested for covid-19
I want to show all the patients either they tested or not
If tested then show the result with his/her name
It only showing the patients who has tested for the covid not every one
how to solve that please ?
here's my Query
SELECT patient.*
,covidtest.covidTestResult ,subareas.areaname
FROM
( patient LEFT OUTER JOIN subareas
ON patient.town_id = subareas.town_id ) LEFT OUTER join covidtest ON
patient.Idnumber = covidtest.Idnumber where
patient.Idnumber=covidtest.Idnumber and
covidtest.CovidTestDate=(select max(covidtest.CovidTestDate)from
covidtest where patient.Idnumber=covidtest.Idnumber) group by covidtest.Idnumber
UNION SELECT patient.* ,covidtest.covidTestResult,subareas.areaname FROM
( patient LEFT OUTER JOIN subareas ON
patient.town_id = subareas.town_id ) LEFT OUTER join covidtest ON
patient.Idnumber = covidtest.Idnumber where patient.Idnumber=covidtest.Idnumber
and covidtest.CovidTestDate=(select max(covidtest.CovidTestDate) from covidtest where
patient.Idnumber=covidtest.Idnumber) group by covidtest.Idnumber;
here is what you need to do:
SELECT
patient.*,
covidtest.covidTestResult,
subareas.areaname
FROM patient
left join subareas on patient.town_id = subareas.town_id
Left join lateral
( select covidTestResult from covidtest
where patient.Idnumber = covidtest.Idnumber
order by CovidTestDate desc
limit 1
) covidtest on 1=1
i have two table galang_dana and donasi, and then i'm try to count all data inside the galang_dana table, and donasi table. The data in galang_dana table is there 6 data, when i'm try this query it's show the data correctly there 6 data
select count(galang_dana.id_galang_dana) as jumlah_galang_dana
from galang_dana
but when i'm try to inner join that both table using this query to count and sum the other data, the data of query select count(galang_dana.id_galang_dana) as jumlah_galang_dana its showing 8 data.
select count(galang_dana.id_galang_dana) as jumlah_galang_dana,
count( distinct donasi.id_user) as jumlah_donatur,
SUM(donasi.jumlah_dana) as total_dana_terkumpul
from `galang_dana` left join
`donasi`
on `galang_dana`.`id_galang_dana` = `donasi`.`id_galang_dana`
I think you want to aggregate before joining:
select gd.jumlah_galang_dana,
d.jumlah_donatur, d.total_dana_terkumpul
from (select count(*) as jumlah_galang_dana
from `galang_dana` gd
) gd cross join
(select count(distinct d.id_user) as jumlah_donatur,
sum(d.jumlah_dana) as total_dana_terkumpul
from donasi d
) d;
DB::table('galang_dana')
->selectRaw('
COUNT(*) jumlah_galang_dana,
(SELECT COUNT(*) FROM donasi WHERE donasi.id_galang_dana = galang_dana.id_galang_dana) jumlah_donatur,
(SELECT SUM(`jumlah_dana`) FROM donasi WHERE donasi.id_galang_dana = galang_dana.id_galang_dana) total_dana_terkumpul
')
->groupBy('id')
->get();
I am running a query on a PHP page that will pull all records from one table, INNER JOIN with two other tables and then list all of the results. However on the second table I only want the most recent record.
Here is my query
SELECT * FROM wn_trailer
INNER JOIN (
SELECT id, trailer_id, trailer_status, trailer_assigned, MAX(last_update), trailer_lat, trailer_long
FROM wn_trailer_history
) AS th ON wn_trailer.id = th.trailer_id
INNER JOIN wn_trailer_status ON wn_trailer_status.id = th.trailer_status
INNER JOIN wn_users ON wn_users.id = th.trailer_assigned
ORDER BY trailer_number ASC
The query runs but returns only the first record.
You want an additional JOIN to bring in the data on the last update date. Also, your subquery needs a GROUP BY:
SELECT *
FROM wn_trailer t INNER JOIN
(SELECT trailer_id, MAX(last_update) as max_last_update
FROM wn_trailer_history
GROUP BY trailer_id
) tht
ON t.id = tht.trailer_id INNER JOIN
wn_trailer_history th
ON th.trailer_id = tht.trailer_id AND
th.last_update = tht.max_last_update INNER JOIN
wn_trailer_status ts
ON ts.id = th.trailer_status INNER JOIN
wn_users u
ON u.id = th.trailer_assigned
ORDER BY trailer_number ASC;
I also added table aliases so the query is easier to write and to read.
This is two query that i need to combine:
first query:
SELECT emp.name, count(rej.employee_ic) as 'rejected leave'
FROM employee as emp
LEFT OUTER JOIN rejectedleave as rej
ON emp.ic_no = rej.employee_ic
GROUP BY emp.`ic_no`
Second query:
SELECT emp.name, count(app.employee_ic) as 'approved leave'
FROM employee as emp
LEFT OUTER JOIN approvedleave as app
ON emp.ic_no = app.employee_ic
GROUP BY emp.`ic_no`
The output:
first query:
first query output image
second query:
second query output image
i want to combine this two table into one table. please help me to solve this problem, appreciate your help.
Is this you need? It find the count of approved and rejected leaves for each available employee in separate subqueries and then joins it with the employee table to show the count of rejected and approved leaves for each employee in one row.
Use coalesce(col,0) to return 0 if the count if null.
Coalesce returns first non null value from the given list of arguments.
select
t1.*,
coalesce(t2.approvedleave,0) approvedleave
coalesce(t3.rejectedleave,0) rejectedleave
from employee t1
left join (
select employee_ic, count(*) approvedleave
from approvedleave
group by employee_ic
) t2 on t1.ic_no = t2.employee_ic
left join (
select employee_ic, count(*) rejectedleave
from rejectedleave
group by employee_ic
) t3 on t1.ic_no = t3.employee_ic;
Read about coalesce here:
http://www.w3resource.com/mysql/comparision-functions-and-operators/coalesce-function.php
I suspect you want this:
SELECT e.name, COALESCE(r.rejected_leave, 0) as rejected_leave,
COALESCE(a.approved_leave, 0) as approved_leave
FROM employee e LEFT OUTER JOIN
(SELECT rl.employee_ic, COUNT(*) as rejected_leave
FROM rejectedleave rl
GROUP BY rl.employee_ic
) r
ON e.ic_no = r.employee_ic LEFT OUTER JOIN
(SELECT al.employee_ic, COUNT(*) as approved_leave
FROM approvedleave al
GROUP BY al.employee_ic
) a
ON e.ic_no = a.employee_ic;
That is, do the aggregation before doing the joins. This assumes that e.ic_no and e.name are both unique in the employees table.
I'm trying to left join the second table useri_ban based on the users' ids, with the extra condition: useri_ban.start_ban = max_start.
In order for me to calculate max_start, I have to run the following subquery:
(SELECT MAX(ub.start_ban) AS max_start, user_id FROM useri_ban ub WHERE ub.user_id = useri.id)
Furthermore, in order to add max_start to every row, I need to inner join this subquery's result into the main result. However, it seems that once I apply that join, the subquery is no longer able to access useri.id.
What am I doing wrong?
SELECT
useri.id as id,
useri.email as email,
useri_ban.warning_type_id as warning_type_id,
useri_ban.type as type,
useri.created_at AS created_at
FROM `useri`
inner join
(SELECT MAX(ub.start_ban) AS max_start, user_id FROM useri_ban ub WHERE ub.user_id = useri.id) `temp`
on `useri`.`id` = `temp`.`user_id`
left join `useri_ban` on `useri_ban`.`user_id` = `useri`.`id` and `useri_ban`.`start_ban` = `max_start`
Does this solve your problem? You need GROUP BY in the inner query instead of another join.
SELECT useri.id, useri.email, maxQuery.maxStartBan
FROM useri
INNER JOIN
(
SELECT useri_ban.user_id ubid, MAX(useri_ban.startban) maxStartBan
FROM useri_ban
GROUP BY useri_ban.user_id
) AS maxQuery
ON maxQuery.ubid = useri.id;