Calculating SUM() results from subqueries into a total - mysql

My brain is starting to hurt with this query and I would appreciate some guidance. What I am trying to get as the result of this query are three values: attendanceScore, lootScore, and totalScore (attendanceScore - lootScore).
Attendance is tracked across three tables: attendance, attendancelog, and attendancevalues.
attendance records an individual's attendance status attached to an attendancelog record. There are types of attendance such as "attended", "missed", and "called out".
attendancelog is the parent record which records the event type, title and date as well as who logged the attendance and when.
attendancevalues is a config table that matches the attendance type from attendance and the event type from attendancelog and returns a configurable value FLOAT.
Loot is tracked across two tables: loot and loottypes.
loot logs each individual item, who received it and when and what type of loot it was (Primary, Secondary, Free-for-all).
loottypes is a config table that takes the type from loot and returns a configurable cost FLOAT.
After some work I have come up with a working query to get attendanceScore and lootScore:
SELECT
(SELECT SUM(t3.`value`)
FROM `attendance` t1
INNER JOIN `attendancelog` t2
ON t2.`id` = t1.`attendancelog_id`
INNER JOIN `attendancevalues` t3
ON t3.`eventtype_id` = t2.`type` AND t3.`attendancetype_id` = t1.`type`
WHERE t1.`user_id` = 3) as attendanceScore,
(SELECT SUM(t2.`cost`)
FROM `loot` t1
INNER JOIN `loottypes` t2
ON t2.`id` = t1.`type`
WHERE t1.`user_id` = 3) as lootScore
I know this doesn't work, but I tried to add (attendanceScore - lootScore) to the query but it says those fields are not available. That is ultimately what I need to complete the query.
I can get the result I want by copying each of the subqueries directly into (attendanceScore - lootScore) but it is just absolutely hideous and I'm sure unnecessary:
SELECT
(SELECT SUM(t3.`value`)
FROM `attendance` t1
INNER JOIN `attendancelog` t2
ON t2.`id` = t1.`attendancelog_id`
INNER JOIN `attendancevalues` t3
ON t3.`eventtype_id` = t2.`type` AND t3.`attendancetype_id` = t1.`type`
WHERE t1.`user_id` = 3) as attendanceScore,
(SELECT SUM(t2.`cost`)
FROM `loot` t1
INNER JOIN `loottypes` t2
ON t2.`id` = t1.`type`
WHERE t1.`user_id` = 3) as lootScore,
(
(SELECT SUM(t3.`value`)
FROM `attendance` t1
INNER JOIN `attendancelog` t2
ON t2.`id` = t1.`attendancelog_id`
INNER JOIN `attendancevalues` t3
ON t3.`eventtype_id` = t2.`type` AND t3.`attendancetype_id` = t1.`type`
WHERE t1.`user_id` = 3) - (SELECT SUM(t2.`cost`)
FROM `loot` t1
INNER JOIN `loottypes` t2
ON t2.`id` = t1.`type`
WHERE t1.`user_id` = 3)
) as totalScore
Could someone help me understand what methods to use for cleaning this up into something more streamlined and efficient?

You may use an inline view
SELECT attendanceScore,
lootScore,
attendanceScore - lootScore as totalScore
FROM
(
SELECT
(
SELECT SUM(t3.`value`)
FROM `attendance` t1
INNER JOIN `attendancelog` t2
ON t2.`id` = t1.`attendancelog_id`
INNER JOIN `attendancevalues` t3
ON t3.`eventtype_id` = t2.`type` AND t3.`attendancetype_id` = t1.`type`
WHERE t1.`user_id` = 3
) as attendanceScore,
(
SELECT SUM(t2.`cost`)
FROM `loot` t1
INNER JOIN `loottypes` t2 ON t2.`id` = t1.`type`
WHERE t1.`user_id` = 3) as lootScore
) t

Related

Update several select in MySQL

It's a sample to see the result.
Hello Everyone,
I wrote a query as a sample and I used several selects because I needed to add columns separately.
So, I got the result.
now I want to know if can I use an update statement for this query?
select T3.*,ao_00b950_aoproject_value.VALUE ODP from
(select T2.*,ao_00b950_aoproject_value.VALUE SOD from
(select T1.*,ao_00b950_aoproject_value.VALUE Platform from (SELECT project.LEAD,project.id,project.pname,project.pkey,ao_00b950_aoproject_value.VALUE Status FROM jira820db.project
LEFT JOIN jira820db.ao_00b950_aoproject_value
on project.ID=ao_00b950_aoproject_value.PROJECT_ID
and ao_00b950_aoproject_value.FIELD_ID =1) as T1
left join ao_00b950_aoproject_value
on T1.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=26) as T2
left join ao_00b950_aoproject_value
on T2.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=28) as T3
left join ao_00b950_aoproject_value
on T3.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=27
So, I wrote this update statement:
update
(select T3.*,ao_00b950_aoproject_value.VALUE ODP from
(select T2.*,ao_00b950_aoproject_value.VALUE SOD from
(select T1.*,ao_00b950_aoproject_value.VALUE Platform from (SELECT project.LEAD,project.id,project.pname,project.pkey,ao_00b950_aoproject_value.VALUE Status FROM jira820db.project
LEFT JOIN jira820db.ao_00b950_aoproject_value
on project.ID=ao_00b950_aoproject_value.PROJECT_ID
and ao_00b950_aoproject_value.FIELD_ID =1) as T1
left join ao_00b950_aoproject_value
on T1.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=26) as T2
left join ao_00b950_aoproject_value
on T2.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=28) as T3
left join ao_00b950_aoproject_value
on T3.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=27) as ww
set project.LEAD = 'kami'
where project.ID ='10000';
But unfortunately, it's not working and I'm not surprised because I'm not an expert in MySQL.
Does anybody have any suggestions?
Or I'm writing wrong the code like always.
With the first query, I get my result, and run perfectly.
With the second one, I get an error code 1288 "The target table ww of the UPDATE is not updatable".

MySQL - Joining table on itself based on criteria from other table

Here’s the problem I am trying to solve:
Table1 has Product ID’s, dates, and prices for those dates, Table2 has Product attributes.
I want to be able to compare prices for a client for different products on the same date based on a set of attributes. I’m easily able to get a list of products/dates/prices for a ‘simple’ product, as well as an ‘advanced’ product (see below).
I want to be able to join these two tables such that the output looks like:
[CLIENT] [PRODUCT] [DATE] [SIM_PROD] [SIM_PRICE] [ADV_PROD] [ADV_PRICE]
Here is as far as I've made it
SELECT b.NAME AS ‘CLIENT’, a.NAME AS ‘SIMPLE_PRODUCT’, t1.DATE AS ‘DATE’, t1.PIVOT_PRICE AS ‘SIMPLE_PRICE’
FROM TABLE1 t1
LEFT JOIN PRODUCT a
ON t1.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = TRUE;
SELECT b.NAME AS ‘CLIENT’, a.NAME AS ‘ADV_PRODUCT’, t2.DATE AS ‘DATE’, t2.PIVOT_PRICE AS ‘ADV_PRICE’
FROM TABLE1 t2
LEFT JOIN PRODUCT a
ON t2.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = FALSE;
I've been able to build similar tables where I pull in price from TABLE1 labeling it as t1 then pull in price again from TABLE1 and labeling it as t2, but only when using criteria in TABLE1, not criteria in a table that needs to be joined.
Is it possible to 'set' a table (EG simple) then 'set' a second one (EG advanced) and then join them on PARTNER_ID and DATE?
You can join the two subqueries:
SELECT t1.client, t1.date, t1.simple_product, t1.simple_price, t2.adv_product, t2.adv_price
FROM (
SELECT b.NAME AS CLIENT, a.NAME AS SIMPLE_PRODUCT, t1.DATE, t1.PIVOT_PRICE AS SIMPLE_PRICE
FROM TABLE1 t1
LEFT JOIN PRODUCT a
ON t1.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = TRUE
) AS t1
JOIN (
SELECT b.NAME AS CLIENT, a.NAME AS SIMPLE_PRODUCT, t1.DATE, t1.PIVOT_PRICE AS SIMPLE_PRICE
FROM TABLE1 t1
LEFT JOIN PRODUCT a
ON t1.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = FALSE
) AS t2
ON t1.client = t2.client AND t1.date = t2.date
You'll probably need to select additional criteria and add them to the ON condition. Otherwise this will produce a full cross product between all the products that have the same client and date.
Your desired output has an additional PRODUCT column, but I couldn't see where that comes from.

Operand should contain 1 column(s) in my sql query

Can someone help me with this query?
SELECT ((SELECT t6.`idreserva`, t5.`sala`, t6.`data`,t3.`inicio`, t4.`fim`, t6.`atividade`
FROM `req_reserva_detail` AS t1
INNER JOIN `req_material_tempo` AS t3 ON (t1.`idtempoInicio` = t3.`idtempo`)
INNER JOIN `req_material_tempo` AS t4 ON (t1.`idtempoFim` = t4.`idtempo`)
INNER JOIN `req_material_sala` AS t5 ON (t1.`idsala` = t5.`idsala`)
INNER JOIN `req_reservas` AS t6 ON (t1.`idreserva` = t6.`idreserva`)
INNER JOIN `utilizador` AS t7 ON (t6.`idutilizador` = t7.`idutilizador`) WHERE t6.`idutilizador` = 670 AND t6.`data` >= CURDATE() ORDER BY DATA ASC),
(SELECT nome FROM `req_reserva_detail` AS t1, `req_material_equipamento` AS t2 WHERE t1.`idequipamento` = t2.`idequipamento` GROUP BY nome))
It gives that error and i don't get how to fix that...
Regards
UPDATE
Schema
(SELECT t6. idreserva,
t5. sala,
t6. data,
t3. inicio,
t4. fim,
t6. atividade
FROM req_reserva_detail AS t1
INNER JOIN req_material_tempo AS t3
ON (t1. idtempoInicio = t3. idtempo)
INNER JOIN req_material_tempo AS t4
ON (t1. idtempoFim = t4. idtempo)
INNER JOIN req_material_sala AS t5
ON (t1. idsala = t5. idsala)
INNER JOIN req_reservas AS t6
ON (t1. idreserva = t6. idreserva)
INNER JOIN utilizador AS t7
ON (t6. idutilizador = t7. idutilizador)
WHERE t6. idutilizador = 670
AND t6. data >= CURDATE()
ORDER BY DATA ASC)
This subquery of yours returns 5 columns, but this is used as a subquery and mySQL expects it to return only one column

Union on simple MySQL recursion

Is there a way to do a UNION between the LEFT JOIN lines, so that the results are not in a separate columns (lev1, lev2, lev3 and lev4), but in a single column (i.e. "ItemNo")?
Here's the MySQL query:
SELECT t1.ItemID AS lev1, t2.ItemID as lev2, t3.ItemID as lev3, t4.ItemID as lev4
FROM TableOfRelations AS t1
LEFT JOIN TableOfRelations AS t2 ON t2.ParentItemID = t1.ItemID
LEFT JOIN TableOfRelations AS t3 ON t3.ParentItemID = t2.ItemID
LEFT JOIN TableOfRelations AS t4 ON t4.ParentItemID = t3.ItemID
WHERE t1.ParentItemID = (SELECT ID FROM TableOfItems WHERE ItemID = 3599);
EDIT:
Here is a sample of result I am looking for:
ItemID ParentItemID JoinedDescription1 JoinedDescription2
3599 NULL MyString1A MyString1B
35 3599 MyString35A MyString35B
168 3599 MyString168A MyString168B
192 168 MyString192A MyString192B
238 3599 MyString238A MyString238B
266 168 MyString266A MyString266B
This result will be used for filling up a TreeView using VB.NET. Also the "JoinedDescriptions" are yet to be joined from a different table according to ItemID, but I think I can handle that easily once I get the basic table correctly.
Important note: Those selected lines are only belonging to one item (root item "3599" in this example), meaning it's only few lines out of thousands. Some recursion examples presume that all the table lines are used in the query, which is not my case.
No, there's not really a way to do a UNION between the LEFT JOIN operations.
But you can use the resultset from your query to get the result you want, by turning your query into an inline view.
Here's one way:
SELECT u.i AS lev
, CASE u.i
WHEN 1 THEN t.lev1
WHEN 2 THEN t.lev2
WHEN 3 THEN t.lev3
WHEN 4 THEN t.lev4
END AS levItemID
FROM ( SELECT 1 AS i
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
) u
CROSS
JOIN ( SELECT t1.ItemID AS lev1
, t2.ItemID AS lev2
, t3.ItemID AS lev3
, t4.ItemID AS lev4
FROM TableOfItems t0
JOIN TableOfRelations t1
ON t1.ParentItemID = t0.id
LEFT
JOIN TableOfRelations t2
ON t2.ParentItemID = t1.ItemID
LEFT
JOIN TableOfRelations t3
ON t3.ParentItemID = t2.ItemID
LEFT
JOIN TableOfRelations t4
ON t4.ParentItemID = t3.ItemID
WHERE t0.ItemID = 3599
) t
Note: this is really more of a UNION ALL operation, it doesn't remove duplicates, it returns NULL values, and it returns the level of the parent.
You can tweak the query to get the results you want. If you don't care about the level number, remove the u.i from the SELECT list.
To remove duplicates, you can either add the DISTINCT keyword following SELECT, or add a GROUP BY clause. To eliminate NULL values, you can add a HAVING levItemID IS NOT NULL clause, etc.
I believe you are looking for concatenating strings.
See the Mysql Documentation on Concat:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
How about using a query as below.
select t1.ItemID as ItemNo
LEFT JOIN TableOfRelations AS t2 ON t2.ParentItemID = t1.ItemID
LEFT JOIN TableOfRelations AS t3 ON t3.ParentItemID = t2.ItemID
LEFT JOIN TableOfRelations AS t4 ON t4.ParentItemID = t3.ItemID
WHERE t1.ParentItemID = (SELECT ID FROM TableOfItems WHERE ItemID = 3599)
UNION
select t2.ItemID as ItemNo
LEFT JOIN TableOfRelations AS t2 ON t2.ParentItemID = t1.ItemID
LEFT JOIN TableOfRelations AS t3 ON t3.ParentItemID = t2.ItemID
LEFT JOIN TableOfRelations AS t4 ON t4.ParentItemID = t3.ItemID
WHERE t1.ParentItemID = (SELECT ID FROM TableOfItems WHERE ItemID = 3599)
UNION
select t3.ItemID as ItemNo
LEFT JOIN TableOfRelations AS t2 ON t2.ParentItemID = t1.ItemID
LEFT JOIN TableOfRelations AS t3 ON t3.ParentItemID = t2.ItemID
LEFT JOIN TableOfRelations AS t4 ON t4.ParentItemID = t3.ItemID
WHERE t1.ParentItemID = (SELECT ID FROM TableOfItems WHERE ItemID = 3599)
UNION
select t4.ItemID as ItemNo
LEFT JOIN TableOfRelations AS t2 ON t2.ParentItemID = t1.ItemID
LEFT JOIN TableOfRelations AS t3 ON t3.ParentItemID = t2.ItemID
LEFT JOIN TableOfRelations AS t4 ON t4.ParentItemID = t3.ItemID
WHERE t1.ParentItemID = (SELECT ID FROM TableOfItems WHERE ItemID = 3599)
Assuming you mean that you want the itemID on leaf nodes (I see no mention of itemNo in your query)....
SELECT
IFNULL(t4.ItemID, IFNULL(t3.ItemID, IFNULL(t2.ItemID, t1.ItemId))
(+remainder of your query)

How to optimize IN constraint query?

Following is my query.
SELECT * FROM t1 WHERE t1.record_id IN (
SELECT t2.record_id FROM t2
INNER JOIN t3 ON CONCAT(t2.case_number,t2.courtfile_type) = CONCAT(t3.case_number,t3.courtfile_type))
It contain IN constraint which is taking lot of time to extract result from database.Database is huge obviously.
How can I optimize this query?
Try this:
USING JOIN
SELECT DISTINCT t1.*
FROM t1
INNER JOIN t2 ON t1.record_id = t2.record_id
INNER JOIN t3 ON t2.case_number = t3.case_number AND t2.courtfile_type = t3.courtfile_type
USING EXISTS
SELECT *
FROM t1
WHERE EXISTS (SELECT t2.record_id
FROM t2 INNER JOIN t3 ON t2.case_number = t3.case_number AND t2.courtfile_type = t3.courtfile_type
WHERE t1.record_id = t2.record_id )
Check the execution plan of the query using EXPLAIN keyword and do proper indexing on tables.