Normally the query below gives me only one result (row).
SELECT
`s`.`FIRMA_UNVANI` AS `FIRMA_UNVANI`,
`s`.`RECNO` AS `RECNO`,
`s`.`BOLGE` AS `BOLGE`,
`s`.`BOLGE_NO` AS `BOLGE_NO`,
`s`.`DURUM` AS `DURUM`,
l.ILCE,
IL.SEHIR,
count(i.recno) AS NUMBER_OF_WORKS
FROM
`SERVISLER` `s`
LEFT JOIN KULLANICI k ON (s.BOLGE = k.KULLANICI)
LEFT JOIN kullanici_cihaz kc ON (k.RECNO = kc.KUL_RECNO)
LEFT JOIN servisler_ilceler c ON (s.RECNO = c.SER_RECNO)
INNER JOIN ILCE l ON (l.RECNO = c.ILCE_RECNO)
INNER JOIN IL ON (IL.ID = l.ILID)
LEFT JOIN ISEMRI i ON (
i.bolge = s.bolge_no
AND i.`SERVIS_DURUMU` = 1
)
WHERE
1 = 1
GROUP BY
s.BOLGE
ORDER BY
IS_SAYISI
LIMIT 0,
15
I get only one result
+----------------+-------+------+---------+------+-----+----------+-----------------+
|FIRMA_UNVANI |RECNO |BOLGE |BOLGE_NO |DURUM |ILCE | SEHIR | NUMBER_OF_WORKS |
+----------------+-------+------+---------+------+-----+----------+-----------------+
|Pirana |2501 |Tekkt |58 |-1 |NT |Istanbul |1428 |
+----------------+-------+------+---------+------+-----+----------+-----------------+
Here the key is RECNO.
I want to count the results:
SELECT
count(0) AS _count
FROM
`SERVISLER` `s`
LEFT JOIN KULLANICI k ON (s.BOLGE = k.KULLANICI)
LEFT JOIN kullanici_cihaz kc ON (k.RECNO = kc.KUL_RECNO)
LEFT JOIN servisler_ilceler c ON (s.RECNO = c.SER_RECNO)
INNER JOIN ILCE l ON (l.RECNO = c.ILCE_RECNO)
INNER JOIN IL ON (IL.ID = l.ILID)
LEFT JOIN ISEMRI i ON (
i.bolge = s.bolge_no
AND i.`SERVIS_DURUMU` = 1
)
WHERE
1 = 1
GROUP BY
s.BOLGE
And I get this wired result:
1428
It was supposed to be 1. Isn't it?
The result is perfectly fine, since you are just counting a 0 for every row instead of i.recno, therefore both resulting in 1428.
To count the number of results, you could wrap your whole query like this to get a resultcount:
SELECT count(*) AS resultcount FROM (
SELECT
`s`.`FIRMA_UNVANI` AS `FIRMA_UNVANI`,
`s`.`RECNO` AS `RECNO`,
`s`.`BOLGE` AS `BOLGE`,
`s`.`BOLGE_NO` AS `BOLGE_NO`,
`s`.`DURUM` AS `DURUM`,
l.ILCE,
IL.SEHIR,
count(i.recno) AS NUMBER_OF_WORKS
FROM
`SERVISLER` `s`
LEFT JOIN KULLANICI k ON (s.BOLGE = k.KULLANICI)
LEFT JOIN kullanici_cihaz kc ON (k.RECNO = kc.KUL_RECNO)
LEFT JOIN servisler_ilceler c ON (s.RECNO = c.SER_RECNO)
INNER JOIN ILCE l ON (l.RECNO = c.ILCE_RECNO)
INNER JOIN IL ON (IL.ID = l.ILID)
LEFT JOIN ISEMRI i ON (
i.bolge = s.bolge_no
AND i.`SERVIS_DURUMU` = 1
)
GROUP BY
s.BOLGE
ORDER BY
IS_SAYISI
LIMIT 0, 15) AS temp
Also note, that WHERE 1=1 is not necessary.
You have used GROUP BY in your query so you will get each group count in result, For example if there 3 group then you will get 3 count result..
If you need to get count of result rows then please use sub query see below
SELECT count(0) AS _count FROM(
SELECT
*
FROM
`SERVISLER` `s`
LEFT JOIN KULLANICI k ON (s.BOLGE = k.KULLANICI)
LEFT JOIN kullanici_cihaz kc ON (k.RECNO = kc.KUL_RECNO)
LEFT JOIN servisler_ilceler c ON (s.RECNO = c.SER_RECNO)
INNER JOIN ILCE l ON (l.RECNO = c.ILCE_RECNO)
INNER JOIN IL ON (IL.ID = l.ILID)
LEFT JOIN ISEMRI i ON (
i.bolge = s.bolge_no
AND i.`SERVIS_DURUMU` = 1
)
WHERE
1 = 1
GROUP BY
s.BOLGE
) AS Temp;
Related
How can I optimize this join query? how can I get the latest start_dt in this query? Is any way can make it faster to load and the start_dt is the latest one? If I delete the subquery for this case, I will get the record which the start_dt is not the latest one. Please Help!!
SELECT
matrix.matrix_uuid,
matrix.name AS matrix_name,
courses.courses_uuid,
courses.name AS courses_name,
employees.employees_uuid,
CONCAT(employees.first_name,
' ',
employees.last_name) AS Name,
courses.validity,
courses.duration,
(SELECT
MAX(start_dt)
FROM
courses_schedule C
WHERE
C.courses_schedule_uuid = CS.courses_schedule_uuid) AS start_dt,
COUNT(courses.courses_uuid) AS refresher,
courses.courses_refresher_uuid,
courses_taken.overall_status,
courses_taken.status
FROM
employees
INNER JOIN
courses_taken ON courses_taken.employees_uuid = employees.employees_uuid
LEFT JOIN
courses_schedule CS ON CS.courses_schedule_uuid = courses_taken.courses_schedule_uuid
LEFT JOIN
matrix_courses matrix_courses ON matrix_courses.courses_uuid = CS.courses_uuid
LEFT JOIN
matrix ON matrix.matrix_uuid = matrix_courses.matrix_uuid
LEFT JOIN
courses ON courses.courses_uuid = matrix_courses.courses_uuid
WHERE
CS.start_dt = (SELECT
MAX(start_dt)
FROM
courses_taken CT
LEFT JOIN
courses_schedule ON courses_schedule.courses_schedule_uuid = CT.courses_schedule_uuid
LEFT JOIN
matrix_courses matrix_courses ON matrix_courses.courses_uuid = courses_schedule.courses_uuid
LEFT JOIN
roles_courses ON roles_courses.courses_uuid = matrix_courses.courses_uuid
LEFT JOIN
matrix M ON M.matrix_uuid = matrix_courses.matrix_uuid
LEFT JOIN
courses C ON C.courses_uuid = matrix_courses.courses_uuid
WHERE
C.courses_uuid = courses.courses_uuid
AND M.matrix_uuid = matrix.matrix_uuid
GROUP BY matrix.matrix_uuid , courses.courses_uuid)
AND courses_taken.overall_status = 1
AND employees.status = 2
AND IFNULL(employees.STATUS, 1) != 0
GROUP BY matrix.matrix_uuid , courses.courses_uuid , employees.employees_uuid
How to sum this query in single column.
SELECT SUM(IFNULL(ih.amount,0.00)) AS amount
FROM human_resources hr
JOIN functions f
ON hr.function_id = f.id
JOIN params_hours_cost phc
ON phc.function_id = f.id
AND phc.function_id = 2
LEFT
JOIN mca_to_hrc ih
ON hr.id = ih.human_resource_id
JOIN management_cost_action ia
ON ia.id = ih.mca_id
WHERE ia.structure_id = 3
AND ia.year = 2018
AND ia.status_id = 0;
SELECT SUM(IFNULL(ih.amount,0.00)) amount
FROM human_resources hr
JOIN functions f
ON hr.function_id = f.id
JOIN params_hours_cost phc
ON phc.function_id = f.id
AND phc.function_id = 2
LEFT
JOIN mca_to_thrc ih
ON hr.id = ih.human_resource_id
AND ih.mca_id = 2
JOIN management_cost_action ia
ON ia.id = ih.mca_id
WHERE ia.structure_id = 3
AND ia.year = 208
AND ia.status_id = 0;
SELECT SUM(IFNULL(ie.distributed_amount,0.00)) distributed_amount
FROM revenue_and_expenses re
LEFT
JOIN mca_to_ee ie
ON re.id = ie.revenue_expenses_id
WHERE re.transaction_type = 2
AND ie.amount > 0
AND re.year = 2018
AND re.structure_id = 3
Wrap all three queries and SUM the alias amount.
Changed distributed_amount to amount
Amended year typo 208 to 2018
SELECT SUM(a.amount) FROM (
SELECT SUM(IFNULL(ih.amount,0.00)) AS amount
FROM human_resources hr INNER JOIN functions f ON(hr.function_id=f.id)
INNER JOIN params_hours_cost phc ON(phc.function_id = f.id AND phc.function_id=2)
LEFT JOIN mca_to_hrc ih ON(hr.id=ih.human_resource_id)
INNER JOIN management_cost_action ia ON(ia.id=ih.mca_id)
WHERE ia.structure_id = '3'
AND ia.year='2018'
AND ia.status_id='0'
UNION ALL
SELECT SUM(IFNULL(ih.amount,0.00)) AS amount
FROM human_resources hr
INNER JOIN functions f ON(hr.function_id=f.id)
INNER JOIN params_hours_cost phc ON(phc.function_id = f.id AND phc.function_id=2)
LEFT JOIN mca_to_thrc ih ON(hr.id=ih.human_resource_id AND ih.mca_id='2')
INNER JOIN management_cost_action ia ON(ia.id=ih.mca_id)
WHERE ia.structure_id = '3'
AND ia.year='2018'
AND ia.status_id=0
UNION ALL
SELECT SUM(IFNULL(ie.distributed_amount,0.00)) AS amount
FROM revenue_and_expenses re
LEFT JOIN mca_to_ee ie ON(re.id=ie.revenue_expenses_id)
WHERE re.transaction_type=2
AND ie.amount>0
AND re.year='2018'
AND re.structure_id='3') a
Here's the code:
SELECT RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,
ip.dblItemPAmount, MAX(ip.dtmItemPasOf) AS EXR
FROM TBLREQUESTDETAILS RD,tblitem I,tblvendor v,tblitemunit iu,tblitemprice ip`
WHERE RD.strReqDItemCode = I.strItemCode
AND RD.strReqDItemUnitCode = iu.strItemUnitCode
AND RD.strReqDVendCode = v.strVendCode
AND i.strItemCode = ip.strItemPItemCode
and RD.strReqDReqHCode = 'RQST121'
GROUP BY RD.INTREQDQUANTITY,I.strItemName,v.strVendName,iu.strItemUnitName, ip.dblItemPAmount
ORDER BY EXR desc ;
AND Here's The result:
What should I do If I want to fetch the current price for each itemname,vendorname and itemunit?? I want to fetch only those rows who's price is the Latest... Help me please those with boxes are the rows that i want to fetch
You can use where in the grouped value (and use explicict join notatio)
SELECT RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,
ip.dblItemPAmount, ip.dtmItemPasOf AS EXR
FROM TBLREQUESTDETAILS RD
INNER JOIN tblitem I ON RD.strReqDItemCode = I.strItemCode
INNER JOIN tblvendor v ON D.strReqDVendCode = v.strVendCode
INNER JOIN tblitemunit iu ON RD.strReqDItemUnitCode = iu.strItemUnitCode
INNER JOIN tblitemprice ip ON i.strItemCode = ip.strItemPItemCode
WHERE RD.strReqDReqHCode = 'RQST121'
and ( RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName, ip.dtmItemPasOf)
in ( SELECT RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,
MAX(ip.dtmItemPasOf)
FROM TBLREQUESTDETAILS RD
INNER JOIN tblitem I ON RD.strReqDItemCode = I.strItemCode
INNER JOIN tblvendor v ON D.strReqDVendCode = v.strVendCode
INNER JOIN tblitemunit iu ON RD.strReqDItemUnitCode = iu.strItemUnitCode
INNER JOIN tblitemprice ip ON i.strItemCode = ip.strItemPItemCode
WHERE RD.strReqDReqHCode = 'RQST121'
GROUP BY RD.INTREQDQUANTITY,I.strItemName,v.strVendName,iu.strItemUnitName
)
ORDER BY EXR desc ;
(and use explicict join notation .. i think is more readable)
I understand about how to use the count with the group by, but I need a specific use of this reverse, I try to do this query:
SELECT
COUNT(pac.strid_paciente)
FROM tb_paciente AS pac
LEFT JOIN tb_entidades AS ent
ON pac.strid_entidad = ent.strid_entidad
LEFT JOIN tb_escolaridad AS esc
ON pac.strid_escolaridad = esc.strID_Escolaridad
LEFT JOIN tb_etnia AS etn
ON pac.strid_etnia = etn.strID_Etnia
LEFT JOIN tb_raza AS raza
ON pac.strid_raza = raza.strid_raza
LEFT JOIN tb_religion AS rel
ON pac.strid_religion = rel.strid_religion
LEFT JOIN tb_ocupacion AS ocu
ON pac.strOfi_Paciente = ocu.strID_Ocupacion
LEFT JOIN tb_consulta AS con
ON pac.strid_paciente = con.strid_paciente
LEFT JOIN tb_usuarios AS usu
ON usu.strid_usuario = con.strid_medico
GROUP BY pac.strid_paciente
And it gives me:
COUNT(pac.strid_paciente)
1
1
2
3
4
5
2
I need the result of every count: 18, the problem is that I cant delete the group by because give a different result. Something like that:
COUNT(pac.strid_paciente)
18
Have you tried SUM( COUNT(pac.strid_paciente) ) OR if that fails then the following will work:
SELECT SUM( t.cnt ) FROM(
SELECT
COUNT(pac.strid_paciente) AS cnt
FROM tb_paciente AS pac
LEFT JOIN tb_entidades AS ent
ON pac.strid_entidad = ent.strid_entidad
LEFT JOIN tb_escolaridad AS esc
ON pac.strid_escolaridad = esc.strID_Escolaridad
LEFT JOIN tb_etnia AS etn
ON pac.strid_etnia = etn.strID_Etnia
LEFT JOIN tb_raza AS raza
ON pac.strid_raza = raza.strid_raza
LEFT JOIN tb_religion AS rel
ON pac.strid_religion = rel.strid_religion
LEFT JOIN tb_ocupacion AS ocu
ON pac.strOfi_Paciente = ocu.strID_Ocupacion
LEFT JOIN tb_consulta AS con
ON pac.strid_paciente = con.strid_paciente
LEFT JOIN tb_usuarios AS usu
ON usu.strid_usuario = con.strid_medico
GROUP BY pac.strid_paciente
) AS t
I've got the query below that's pulling data from a number of tables to create an update:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
It works fine but when there are plenty of records in papr_down then it takes ages to process. Any ideas about how it can be optimized?
What I think is the join between the emailAddress is the issue here, you can try out with the join with the Id's.
If you provide us the screen shot of the below query ::
EXPLAIN Select * from
en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
WHERE sd.fieldid = 33
As I know we should use joins only for the columns which are preset in SELECT clause and for other joins we should implement using WHERE clause
Please try following query:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls
on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down AS pd1
INNER JOIN email.papr_data AS pd2 on pd1.paper_id = pd2.id
WHERE
email.papr_exam.id in (select exam from email.papr_data where exam = 1)
AND
email.papr_levl.id in (select level from email.papr_data where level = 4 )
AND
email.papr_subj.id in (select subject from email.papr_data)
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
I can not execute this at my machine since i don't have the schema