Problems Summing up result of MySQL query - mysql

The following query returns what is displayed in the attached image.
What I thought would be very simple has turned out to be very complex. All I simply want to do is now total up the result into one row and column. In this case the sum would be 161. How do I do this? I've literally tried everything. I hope I've provided enough information.
SELECT
TRUNCATE
(
SUM(
`assignment`.`percentage_achieved` *(
SELECT
`unitComponentWeighting`.`percentage_weighting` / 100
FROM
`unitComponentWeighting`
WHERE
`assignment`.`assignment_component_id` = `unitComponentWeighting`.`component_lookup_id`
LIMIT 1
)
),
2
) AS `unit_percentage_grade`
FROM
`assignment`
LEFT JOIN `assignmentType` ON `assignment`.`assignment_type_id` = `assignmentType`.`id`
LEFT JOIN `assignmentComponentLookup` ON `assignmentComponentLookup`.`id` = `assignment`.`assignment_component_id`
LEFT JOIN `unit` ON `unit`.`id` = `assignment`.`unit_id`
LEFT JOIN `assignmentSequence` ON `assignmentSequence`.`id` = `assignment`.`assignment_sequence_id`
LEFT JOIN `yearGroup` ON `yearGroup`.`id` = `unit`.`year_group_id`
WHERE
`yearGroup`.`id` = 1
GROUP BY
`assignment`.`unit_id`

try removing this part
" GROUP BY
assignment.unit_id "

Related

Getting wrong COUNT values in query

I am using the following query to get COUNT items from rows from the same table in LEFT JOIN.
This is the query:
SELECT
pac.id_sat as id_sat,
pac.nombre_contacto as nombre_contacto,
pac.centro_contacto as centro_contacto,
pac.tel_contacto as tel_contacto,
pac.horario_contacto as horario_contacto,
pac.email_contacto as email_contacto,
pac.num_factura as num_factura,
pac.fecha_factura as fecha_factura,
eq.nombre_equipo as modelo_equipo,
pac.num_serie as num_serie,
pac.tipo_incidencia as tipo_incidencia,
pac.cod_sat as cod_sat,
pac.estado as estado,
pac.clinica as clinica,
pac.fecha_sat as fecha_sat,
COUNT(medfotos.id_media_sat) as num_fotos,
COUNT(medvideos.id_media_sat) as num_videos
FROM tb_sat pac
LEFT JOIN tb_equipos eq ON pac.modelo_equipo = eq.id_equipo
LEFT JOIN tb_media_sat medfotos ON pac.cod_sat = medfotos.cod_sat AND medfotos.tipo = 1
LEFT JOIN tb_media_sat medvideos ON pac.cod_sat = medvideos.cod_sat AND medvideos.tipo = 2
WHERE pac.clinica = '".$idclinica."'
GROUP BY pac.id_sat
ORDER BY pac.fecha_sat DESC
My issue is that I am getting a wrong amount of COUNT items.
The real value for num_fotos should be 3 and for num_videos should be 2.
I am getting num_fotos = 6 and num_videos = 6.
EDIT
Table tb_sat
Table tb_media_sat
Sub-query will work better in your case like as follows:
SELECT pac.*, (SELECT COUNT(id_media_sat) FROM tb_media_sat WHERE cod_sat=pac.cod_sat AND tipo=1) AS num_fotos, (SELECT COUNT(id_media_sat) FROM tb_media_sat WHERE cod_sat=pac.cod_sat AND tipo=2) AS num_videos FROM tb_sat pac WHERE pac.clinica = '".$idclinica."' ORDER BY pac.fecha_sat DESC
Rest columns, please add yourself slowly slowly. I hope you will get correct output.

SSRS Report Running slowly

Using Report Builder 3.0, I have a report using the following dataset:
Declare #Hierarchy nVarChar (1000) = EAC.GetHierarchy (#UserName, DEFAULT)
SELECT TOP(100)
CASE
WHEN #ShowSourceTime = 1 THEN SourceTime
ELSE Dateadd(Minute, #TimeZoneOffset, Time)
END as Time
,Warehouse.EventInfo.TimeZoneShortName
,Warehouse.EventInfo.Category
,Warehouse.EventInfo.[Action]
,Warehouse.EventInfo.[Result]
,Warehouse.EventInfo.Reason
,Warehouse.EventInfo.PersonId
,Warehouse.EventInfo.Title
,Warehouse.EventInfo.FirstName
,Warehouse.EventInfo.MiddleName
,Warehouse.EventInfo.LastName
,Warehouse.EventInfo.Suffix
,Warehouse.EventInfo.Nickname
,Warehouse.EventInfo.DoorName
,Warehouse.EventInfo.DoorBehaviorName
,Warehouse.EventInfo.EnterZone
,Warehouse.EventInfo.LeaveZone
,Warehouse.EventInfo.SiteCode
,Warehouse.EventInfo.CardCode
,Warehouse.EventInfo.SiteCode + '-' + Warehouse.EventInfo.CardCode as Badge
FROM
Warehouse.EventInfo
WHERE
Warehouse.EventInfo.[Time] Between #StartTime and #EndTime
AND (1 = #PersonIdExpr OR EventInfo.PersonId IN (#PersonId))
AND WareHouse.EventInfo.ZoneHierarchy LIKE #Hierarchy
order by Time
The dataset is calling the view below:
SELECT EAC.Event.CreatedUTC AS Time
,DATEADD(MINUTE, EAC.Event.CreatedUTCOffset
, EAC.Event.CreatedUTC) AS SourceTime
, EAC.TimeZoneMap.TimeZoneShortName
, EAC.EventClass.Name AS Class
, EAC.EventCategory.Name AS Category
, EAC.EventAction.Name AS Action
, EAC.EventResult.Name AS Result
, EAC.EventReason.Name AS Reason
, EAC.Person.Id AS PersonId
, EAC.Person.Title
, EAC.Person.FirstName
, EAC.Person.MiddleName
, EAC.Person.LastName
, EAC.Person.Suffix
, EAC.Person.DisplayName AS Nickname
, EAC.Door.Name AS DoorName
, Zone_1.Name AS EnterZone
, (SELECT TOP (1) Name
FROM EAC.Zone
WHERE (Id IN (EAC.Door.Zone1Id, EAC.Door.Zone2Id))
AND (Id <> EAC.Event.ZoneId)) AS LeaveZone
, EAC.DoorBehavior.Name AS DoorBehaviorName
, EAC.Event.CardCode
, EAC.Event.SiteCode
, ISNULL(EAC.Event.Alarmed, 0) AS AlarmType
, Zone_1.ZoneHierarchy
FROM EAC.Event
INNER JOIN EAC.EventType ON EAC.EventType.Id = EAC.Event.EventTypeId
LEFT OUTER JOIN EAC.EventClass ON EAC.EventClass.Id = EAC.Event.EventClassId
LEFT OUTER JOIN EAC.EventCategory ON EAC.EventCategory.Id = EAC.EventType.CategoryId
LEFT OUTER JOIN EAC.EventAction ON EAC.EventAction.Id = EAC.EventType.ActionId
LEFT OUTER JOIN EAC.EventResult ON EAC.EventResult.Id = EAC.EventType.ResultId
LEFT OUTER JOIN EAC.EventReason ON EAC.EventReason.Id = EAC.EventType.ReasonId
LEFT OUTER JOIN EAC.Person ON EAC.Person.Id = EAC.Event.PersonId
LEFT OUTER JOIN EAC.Door ON EAC.Door.Id = EAC.Event.DoorId
LEFT OUTER JOIN EAC.Zone AS Zone_1 ON Zone_1.Id = EAC.Event.ZoneId
LEFT OUTER JOIN EAC.DoorBehavior ON EAC.DoorBehavior.Id = EAC.Door.DoorBehaviorId
LEFT OUTER JOIN EAC.Credential ON EAC.Credential.Id = EAC.Event.CredentialId
LEFT OUTER JOIN EAC.WiegandCredential ON EAC.WiegandCredential.CredentialId = EAC.Credential.Id
LEFT OUTER JOIN EAC.TimeZoneMap on EAC.TimeZoneMap.Id = EAC.Door.TimeZoneMapId
The table it is querying has millions of records but in order to get anything to come back I limited it to 100 records but it still takes 15 minutes.
When I run the report I see this in the database:
Here is the execution plan:
Execution plan
What can I do to make this report run more efficiently?
with that much LEFT OUTER JOIN It is not surprising that your query runs slow. Each join introduces complexity to your code. Besides, LEFT|RIGHT [OUTER] JOIN is much slower then regular join since it has to do all the work of an INNER JOIN (regular join) plus the extra work of null-extending the results.
Also it is impossible to make comment or pinpoint the problem without seeing explain plan of the query. Possibly you have missing indexes on tables(s)

this Query is taking 15 seconds

i added indexes as well but still it is taking 13 sec
I added compound index for all the columns that i've used here
SELECT carrierbil2_.IDENTITY AS col_0_0_,
carrier4_.CARRIER_NAME AS col_1_0_,
carrier4_.IDENTITY AS col_2_0_,
carrier4_.CARRIER_ID AS col_3_0_,
shipmentor0_.EXTERNAL_REFERENCE_ID AS col_4_0_,
invoicedet5_.INVOICE_NUMBER AS col_5_0_,`enter code here`
shipmentca1_.CARRIER_REFERENCE_NUMBER AS col_6_0_,
SUM(shipmentco9_.RATED_COST) AS col_7_0_,
SUM(shipmentco9_.COST) AS col_8_0_,
invoice6_.TOTAL_PAID_AMOUNT AS col_9_0_,
invoice6_.INVOICE_GENERATED_DATE AS col_10_0_,
shipmentor0_.ACTUAL_SHIP_DATE AS col_11_0_,
bolstatus15_.BOL_STATUS_ID AS col_12_0_,
shipmentlo10_.LOCATION_NAME AS col_13_0_,
country11_.COUNTRY_NAME AS col_14_0_,
postal14_.POSTAL_CODE AS col_15_0_,
state12_.STATE_NAME AS col_16_0_,
city13_.CITY_NAME AS col_17_0_,
shipmentlo16_.LOCATION_NAME AS col_18_0_,
country17_.COUNTRY_NAME AS col_19_0_,
postal20_.POSTAL_CODE AS col_20_0_,
state18_.STATE_NAME AS col_21_0_,
city19_.CITY_NAME AS col_22_0_,
shipmentor0_.IDENTITY AS col_23_0_,
shipmentca1_.IDENTITY AS col_24_0_,
shipmentno7_.NOTE AS col_25_0_
FROM
SHIPMENT_ORDER shipmentor0_
INNER JOIN
SHIPMENT_CARRIER shipmentca1_ ON shipmentor0_.SHIPMENT_ORDER_ID = shipmentca1_.SHIPMENT_ORDER_ID
AND (shipmentca1_.IS_DELETED = 0)
LEFT OUTER JOIN
CARRIER_BILL_DETAILS carrierbil2_ ON shipmentca1_.SHIPMENT_CARRIER_ID = carrierbil2_.SHIPMENT_CARRIER_ID
LEFT OUTER JOIN
CARRIER_BILLS carrierbil3_ ON carrierbil2_.CARRIER_BILL_ID = carrierbil3_.CARRIER_BILL_ID
INNER JOIN
CARRIER carrier4_ ON shipmentca1_.CARRIER_ID = carrier4_.CARRIER_ID
LEFT OUTER JOIN
INVOICE_DETAILS invoicedet5_ ON shipmentor0_.SHIPMENT_ORDER_ID = invoicedet5_.SHIPMENT_ORDER_ID
LEFT OUTER JOIN
INVOICE invoice6_ ON invoicedet5_.INVOICE_ID = invoice6_.INVOICE_ID
LEFT OUTER JOIN
SHIPMENT_NOTES shipmentno7_ ON shipmentor0_.SHIPMENT_ORDER_ID = shipmentno7_.SHIPMENT_ORDER_ID
AND (shipmentno7_.NOTE_TYPE = 4)
LEFT OUTER JOIN
SHIPMENT_COST shipmentco8_ ON shipmentor0_.SHIPMENT_ORDER_ID = shipmentco8_.SHIPMENT_ID
LEFT OUTER JOIN
SHIPMENT_COST_DETAILS shipmentco9_ ON shipmentco8_.SHIPMENT_COST_ID = shipmentco9_.SHIPMENT_COST_ID
AND (shipmentco9_.IS_DELETED = 0)
LEFT OUTER JOIN
SHIPMENT_LOCATION shipmentlo10_ ON shipmentor0_.ORIGIN_ID = shipmentlo10_.SHIPMENT_LOCATION_ID
AND (shipmentlo10_.LOCATION_TYPE_ID = 3)
LEFT OUTER JOIN
COUNTRY country11_ ON shipmentlo10_.COUNTRY_ID = country11_.COUNTRY_ID
LEFT OUTER JOIN
STATE state12_ ON shipmentlo10_.STATE_ID = state12_.STATE_ID
LEFT OUTER JOIN
CITY city13_ ON shipmentlo10_.CITY_ID = city13_.CITY_ID
LEFT OUTER JOIN
POSTAL postal14_ ON shipmentlo10_.POSTAL_ID = postal14_.POSTAL_ID
LEFT OUTER JOIN
BOL_STATUS bolstatus15_ ON shipmentor0_.ORDER_STATUS = bolstatus15_.BOL_STATUS_ID
LEFT OUTER JOIN
SHIPMENT_LOCATION shipmentlo16_ ON shipmentor0_.DESTINATION_LOCATION_ID = shipmentlo16_.SHIPMENT_LOCATION_ID
AND (shipmentlo16_.LOCATION_TYPE_ID = 4)
LEFT OUTER JOIN
COUNTRY country17_ ON shipmentlo16_.COUNTRY_ID = country17_.COUNTRY_ID
LEFT OUTER JOIN
STATE state18_ ON shipmentlo16_.STATE_ID = state18_.STATE_ID
LEFT OUTER JOIN
CITY city19_ ON shipmentlo16_.CITY_ID = city19_.CITY_ID
LEFT OUTER JOIN
POSTAL postal20_ ON shipmentlo16_.POSTAL_ID = postal20_.POSTAL_ID
CROSS JOIN
CLIENT client21_
WHERE
shipmentor0_.CLIENT_ID = client21_.CLIENT_ID
AND bolstatus15_.SEQUENCE_ID >= 700
AND (carrierbil3_.IS_APPROVED = 0
OR carrierbil3_.IS_APPROVED IS NULL)
AND (carrierbil3_.IS_DELETED = 0
OR carrierbil3_.IS_DELETED IS NULL)
AND (carrierbil2_.IS_DELETED = 0
OR carrierbil2_.IS_DELETED IS NULL)
AND (shipmentor0_.IS_DELETED = 0
OR shipmentor0_.IS_DELETED IS NULL)
GROUP BY invoice6_.INVOICE_GENERATED_DATE , shipmentca1_.IDENTITY , invoicedet5_.INVOICE_NUMBER , invoice6_.TOTAL_PAID_AMOUNT , shipmentca1_.CARRIER_REFERENCE_NUMBER , carrier4_.CARRIER_ID , CAST(carrier4_.IDENTITY AS SIGNED) , carrier4_.CARRIER_NAME , CAST(carrierbil2_.IDENTITY AS SIGNED) , shipmentor0_.SHIPMENT_ORDER_ID , shipmentno7_.NOTE , shipmentor0_.EXTERNAL_REFERENCE_ID , shipmentlo10_.LOCATION_NAME , country11_.COUNTRY_NAME , postal14_.POSTAL_CODE , state12_.STATE_NAME , city13_.CITY_NAME , shipmentlo16_.LOCATION_NAME , country17_.COUNTRY_NAME , postal20_.POSTAL_CODE , state18_.STATE_NAME , city19_.CITY_NAME , shipmentor0_.IDENTITY
ORDER BY shipmentor0_.SHIPMENT_ORDER_ID DESC;
The indexes are mostly useless because of OR, as in
AND (carrierbil3_.IS_APPROVED = 0
OR carrierbil3_.IS_APPROVED IS NULL)
The simple way to fix that is to pick either 0 or NULL to represent the flag. Then make sure all the data is consistent, and change the WHERE to just check for the one case.
Do you really mean
CROSS JOIN
CLIENT client21_
That is likely to be a performance-killer and generate a huge resultset.
Never mind. You have the ON in WHERE. Please use ON for relations and WHERE for filtering.
WHERE
shipmentor0_.CLIENT_ID = client21_.CLIENT_ID
I see a mixture of LEFT JOIN and JOIN. Check that the LEFT JOINs really need to be LEFT; that is, the 'right' table might have missing data.
To discuss further, please provide EXPLAIN SELECT ....
Eschew over-normalization:
You have 5 tables to describe a location (name, country, postal, state, city). Instead, I recommend a single table with those 5 columns. This, alone, would get rid of 8 JOINs.
CAST(carrier4_.IDENTITY AS SIGNED) -- Can't you fix the datatype to be SIGNED, or allow the value to be UNSIGNED?
But perhaps the main performance-killer is the "explode-implode" syndrone. First, it does a lot of JOINs, building a huge intermediate table, then it collapses that by doing GROUP BY. The remedy is
SELECT ...
FROM ( SELECT SUM(...), SUM(...) FROM ... GROUP BY ... ) AS a
JOIN ((whatever else is needed));
That is, first devise a minimal "derived table" that does the GROUP BY (and/or ORDER BY and/or LIMIT). Then see what else is needed to complete the query (namely all the normalization lookups).
After you have acted on most of my comments, we can discuss whether you have the optimal indexes. (It is premature to do so now.) If so, please start a new Question; it would be too much clutter to add to this one.
First of all, that's a lot of joins. However, the main reason your query is taking a significant time is because you're adding an order by clause. You need to figure out a way to avoid it, or may be come up with a different strategy

SQL Query based on multiple where conditions

I've got a script where checked in employee's can see the stock inventory from each other, linked with their personal stock location, so each checked in employee can see which items are in stock at different locations. However, I want the main stock (id of 1, which is not attached to an employee) to be showed always, but I can't get the query right because one of the where statements is clearly not correct:
`stock_locations`.`location_id` = 1 AND
`workschedule`.`checkedIn` = 1 AND
Rememeber, the main stock is not linked to an employee, so it doesn't show up at the workschedule table. If I remove the first statement, It clearly shows up all the checked in employee's with their location, but that doesn't give me the main stock. If I remove the second statement, it only shows me the main stock.
How can I solve this issue within SQL? This is btw the full statement:
SELECT
`item_quantities`.`item_id`,
`stock_locations`.`location_name`,
`item_quantities`.`quantity`,
`people`.`first_name`
FROM
`item_quantities`
JOIN `stock_locations` ON `item_quantities`.`location_id` = `stock_locations`.`location_id`
JOIN `items` ON `item_quantities`.`item_id` = `items`.`item_id`
LEFT JOIN `workschedule` ON `workschedule`.`linked_storage` = `stock_locations`.`location_id`
LEFT JOIN `people` ON `workschedule`.`employee_id` = `people`.`person_id`
WHERE
`stock_locations`.`location_id` = 1 AND
`workschedule`.`checkedIn` = 0 AND
`items`.`unit_price` != 0 AND
`items`.`deleted` = 0 AND
`stock_locations`.`deleted` = 0 NULL
Thanks in advance!
Make it an OR statement inside of parens.
(`stock_locations`.`location_id` = 1 OR `workschedule`.`checkedIn` = 1) AND
This will return all records that match either the main stock or the employee.
You need to use the OR operator. Clearly both things can't happen at the same time, so you need to specify each set of acceptable conditions.
SELECT
`item_quantities`.`item_id`,
`stock_locations`.`location_name`,
`item_quantities`.`quantity`,
`people`.`first_name`
FROM
`item_quantities`
JOIN `stock_locations`
ON `item_quantities`.`location_id` = `stock_locations`.`location_id`
JOIN `items`
ON `item_quantities`.`item_id` = `items`.`item_id`
LEFT JOIN `workschedule`
ON `workschedule`.`linked_storage` = `stock_locations`.`location_id`
LEFT JOIN `people`
ON `workschedule`.`employee_id` = `people`.`person_id`
WHERE
`stock_locations`.`location_id` = 1
OR (
AND `workschedule`.`checkedIn` = 1
AND `items`.`unit_price` != 0
AND `items`.`deleted` = 0
AND `stock_locations`.`deleted` = 0
NULL
)
You have LEFT JOIN, but your WHERE clause turns them into inner joins. Fixing that will probably fix your problem:
SELECT . . .
FROM item_quantities iq JOIN
stock_locations sl
ON iq.`location_id` = sl.`location_id` JOIN
items i
ON iq.`item_id` = i.`item_id` LEFT JOIN
workschedule ws
ON ws.`linked_storage` = sl.`location_id` AND
ws.`checkedIn` = 0 LEFT JOIN
--------^
people p
ON ws.`employee_id` = p.`person_id`
WHERE sl.`location_id` = 1 AND
i.`unit_price` != 0 AND
i.`deleted` = 0 AND
sl.`deleted` = 0

Mysql Distinct select with replace

I have the following mySql select statement it returns the below result and battling to get the result I am after.
select `tvshow`.`idShow` AS `idShow`,`tvshow`.`c00` AS `ShowName`,
if(count(distinct `episode`.`c12`),
count(distinct `episode`.`c12`),0) AS `TotalSeasons`,
if(count(`episode`.`c12`),count(`episode`.`c12`),0) AS `TotalEpisodeCount`
from
((((`tvshow`
left join `tvshowlinkpath` ON ((`tvshowlinkpath`.`idShow` = `tvshow`.`idShow`)))
left join `path` ON ((`path`.`idPath` = `tvshowlinkpath`.`idPath`)))
left join `episode` ON ((`episode`.`idShow` = `tvshow`.`idShow`)))
left join `files` ON ((`files`.`idFile` = `episode`.`idFile`)))
group by `tvshow`.`idShow`
having (count(`episode`.`c12`) > 0)
Select Result
I am trying to get a 4th column that would have the seasons listed in it e.g "Season 1,Season 2,Season 3"
I can get the the data I need by running the following select
select distinct c12 from episode where idShow = 1
It returns the following.
So i thought I could use the replace to change the results to read "Season1" but not sure how to get it to just return one string containing "Seasin1,Season2,Season3" and then add it to the select statement at the top of the view and bring it all together?
The Result I am trying to get(used Photoshop for this just so you could get the idea)
Just add GROUP_CONCAT(episode.c12) as additional column:
select `tvshow`.`idShow` AS `idShow`,`tvshow`.`c00` AS `ShowName`,
if(count(distinct `episode`.`c12`),
count(distinct `episode`.`c12`),0) AS `TotalSeasons`,
if(count(`episode`.`c12`),count(`episode`.`c12`),0) AS `TotalEpisodeCount`,
`GROUP_CONCAT(episode.c12)` as `Seasons`
from
((((`tvshow`
left join `tvshowlinkpath` ON ((`tvshowlinkpath`.`idShow` = `tvshow`.`idShow`)))
left join `path` ON ((`path`.`idPath` = `tvshowlinkpath`.`idPath`)))
left join `episode` ON ((`episode`.`idShow` = `tvshow`.`idShow`)))
left join `files` ON ((`files`.`idFile` = `episode`.`idFile`)))
group by `tvshow`.`idShow`
having (count(`episode`.`c12`) > 0)
See http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat for the documentation of this MySQL specific function.