Mysql subquery per row - mysql

I'm trying to show a matrix table with the results from a vendor in a category. Like:
+-----------+-------------+-------------+------+
| | Category 1 | Category 2 | ... |
+-----------+-------------+-------------+------+
| Vendor 1 | 8900 | 0 | ... |
| Vendor 2 | 56890 | 4000 | ... |
| ... | ... | ... | ... |
+-----------+-------------+-------------+------+
I already made a query for just one vendor:
SELECT ut.unit_name, IFNULL(SUM(commission_fix_out),0)
FROM transaction t
JOIN office_per_transaction opt
ON t.id = opt.transaction_id
JOIN_vendor_per_transaction ppt
ON t.id = ppt.transaction_id
AND ppt.vendor_id = 2
RIGHT JOIN unit_type ut
ON t.unit_type_id = ut.id
AND transaction_end_week BETWEEN 0 AND 14
AND YEAR(transaction_end_date) = 2012
GROUP BY ut.id
Which gives me following result:
+-------------+---------+
| Category | Result |
+-------------+---------+
| Category 1 | 56890 |
| Category 2 | 4000 |
| ... | ... |
+-------------+---------+
Now i would like to see a concatenation of this query per vendor in a given office.
First, I tried replacing AND ppt.vendor_id = 2 with AND ppt.vendor_id IN (SELECT id FROM vendor WHERE office_id = 1) but that gave me the total result of all the vendors in that office.
Secondly, I tried using a subquery. But that gave me an error stating unknown column.
What i want to achieve is a resultset like:
+-----------+--------- -------+
| Vendor | Results |
+-----------+-----------------+
| Vendor 1 | 8900,0,... |
| Vendor 2 | 56890,4000,... |
| ... | ...,...,... |
+-----------+-----------------+
Am i trying something unrealistic and should i rather perform above query per vendor. Or am i missing something here?
#SashiKant I've tried many queries last days, but it was something like this:
SELECT v.id, GROUP_CONCAT(r.result) FROM vendor v,
( SELECT ut.unit_name, IFNULL(SUM(commission_fix_out),0) as result
FROM transaction t JOIN office_per_transaction opt ON t.id = opt.transaction_id
JOIN vendor_per_transaction ppt ON t.id = ppt.transaction_id
AND ppt.vendor_id = p.id
RIGHT JOIN unit_type ut ON unit_type_id = ut.id
AND transaction_end_week BETWEEN 0 AND 14
AND YEAR(transaction_end_date) = 2012 GROUP BY ut.id) as r

You can use group_concat.
Pseudo SQL:
select vendor, group_concat(result)
from mytable
group by vendor
You might "simplify" it by creating a view. Since I don't know your table structure, this is just guessing
create view commissions as
SELECT ppt.vendor_id as vendor, ut.unit_name as category,
IFNULL(SUM(commission_fix_out),0) as commission
FROM transaction t
JOIN office_per_transaction opt
ON t.id = opt.transaction_id
JOIN_vendor_per_transaction ppt
ON t.id = ppt.transaction_id
RIGHT JOIN unit_type ut
ON t.unit_type_id = ut.id
AND transaction_end_week BETWEEN 0 AND 14
AND YEAR(transaction_end_date) = 2012
GROUP BY ppt.vendor_id, ut.id;
and then select
select vendor, group_concat(commission)
from commissions
group by vendor;

You probably need to join against the vendors table to get the vendors you want (rather than a single vendor). Also you need more clauses in the GROUP BY (and you really should use all the non aggregate columns from the SELECT in the GROUP BY clause - mysql doesn't object but most flavours of SQL do object if you miss some out).
Something like this (not tested so please excuse any typos)
SELECT vendor_id, ut.id, ut.unit_name, IFNULL(SUM(commission_fix_out),0)
FROM transaction t
INNER JOIN office_per_transaction opt
ON t.id = opt.transaction_id
INNER JOIN_vendor_per_transaction ppt
ON t.id = ppt.transaction_id
INNER JOIN vendor ve
ON ve.id = ppt.vendor_id
RIGHT JOIN unit_type ut
ON t.unit_type_id = ut.id
AND transaction_end_week BETWEEN 0 AND 14
AND YEAR(transaction_end_date) = 2012
WHERE ve.office_id = 1
GROUP BY vendor_id, ut.id, ut.unit_name
ORDER BY vendor_id, ut.id, ut.unit_name
If you want a row per vendor with a single column for all the amounts for that vendor then something like this.
SELECT vendor_id, GROUP_CONCAT(CommisionAmount)
FROM (
SELECT vendor_id, ut.id, ut.unit_name, IFNULL(SUM(commission_fix_out),0) AS CommisionAmount
FROM transaction t
INNER JOIN office_per_transaction opt
ON t.id = opt.transaction_id
INNER JOIN_vendor_per_transaction ppt
ON t.id = ppt.transaction_id
INNER JOIN vendor ve
ON ve.id = ppt.vendor_id
RIGHT JOIN unit_type ut
ON t.unit_type_id = ut.id
AND transaction_end_week BETWEEN 0 AND 14
AND YEAR(transaction_end_date) = 2012
WHERE ve.office_id = 1
GROUP BY vendor_id, ut.id, ut.unit_name
ORDER BY vendor_id, ut.id, ut.unit_name) Sub1

Related

Using parent id in child query?

SELECT li_1.carrier, li_1.product_id, li_1.quantity, products_description.products_name, sites.sites_id, sites.sites_name, counted_table.counted
FROM inventory li_1
INNER JOIN products_description ON li_1.product_id = products_description.products_id
INNER JOIN sites ON products_description.data = sites.data
INNER JOIN (
SELECT SUM( li_2.quantity ) AS counted
FROM inventory li_2
WHERE li_1.product_id = li_2.product_id
) counted_table
GROUP BY li_1.product_id
ORDER BY li_1.id DESC
I'm attempting to use the parent id (product_id) to count the total amount of quantity for each product in the subquery - But I only get the standard mysql error message.
So something like
id | quantity | total
---------------------------------
0001 | 2 | 6
| |
0001 | 4 | 6
What could be wrong?
if you change sub-query, it could be fixed
SELECT li_1.carrier, li_1.product_id, li_1.quantity, products_description.products_name, sites.sites_id, sites.sites_name, counted_table.counted
FROM inventory li_1
INNER JOIN products_description ON li_1.product_id = products_description.products_id
INNER JOIN sites ON products_description.data = sites.data
INNER JOIN (
SELECT product_id,SUM( li_2.quantity ) AS counted
FROM inventory li_2
GROUP BY li_2.product_id
) counted_table ON li_1.product_id = counted_table.product_id
ORDER BY li_1.id DESC

MySQL multiple inner join between 2 tables on different columns for one of the tables

Table transport
Id | FirstLevSubcat | SecondLevSubcat | ThirdLevSubcat
--------------------------------------------------------
8 | 4 | 27 | 1418
Table categories
Id | CategoriesUrl
--------------------
4 | cars
27 | audi
1418 | audi-100
Query if not to use categories table (without inner join) would be like
SELECT count(*) FROM transport
WHERE FirstLevSubcat = 4 AND SecondLevSubcat = 27 AND ThirdLevSubcat = 1418
Trying to get the same result using INNER JOIN
SELECT count(*) FROM transport main_table
INNER JOIN categories cat_table_first ON cat_table_first.IdRows = main_table.FirstLevSubcat
INNER JOIN categories cat_table_second ON cat_table_second.IdRows = main_table.SecondLevSubcat
INNER JOIN categories cat_table_third ON cat_table_third.IdRows = main_table.ThirdLevSubcat
WHERE
cat_table_first.CategoriesUrl = 'cars'
AND cat_table_second.CategoriesUrl = 'audi'
AND cat_table_third.CategoriesUrl = 'audi-100'
At first sight all works
But is such query ok? May be can improve something?
Your query is correct. You can also do it in following way:
SELECT count(*) FROM transport main_table
INNER JOIN categories cat_table_first ON cat_table_first.IdRows = main_table.FirstLevSubcat and cat_table_first.CategoriesUrl = 'cars'
INNER JOIN categories cat_table_second ON cat_table_second.IdRows = main_table.SecondLevSubcat and cat_table_second.CategoriesUrl = 'audi'
INNER JOIN categories cat_table_third ON cat_table_third.IdRows = main_table.ThirdLevSubcat and cat_table_third.CategoriesUrl = 'audi-100'
You can also do it using 3 EXISTS block.
SELECT count(*) FROM transport main_table
WHERE
EXISTS (SELECT NULL FROM categories WHERE main_table.FirstLevSubcat=categories.IdRows AND categories.CategoriesUrl ='cars')
AND
EXISTS (SELECT NULL FROM categories WHERE main_table.SecondLevSubcat=categories.IdRows AND categories.CategoriesUrl ='audi')
AND
EXISTS (SELECT NULL FROM categories WHERE main_table.ThirdLevSubcat=categories.IdRows AND categories.CategoriesUrl ='audi-100')

Mysql nested select with multiple joins with condition on join table

I've got a SELECT with multiple JOINS for a paginated Tableview. In general this is working for unfiltered results.
The query looks like this:
SELECT seltable.*,
tbl2.name AS tbl2name,
tbl3.name AS tbl3name,
tbl4.name AS tbl4name
FROM
( SELECT * FROM selecttable
WHERE value = 99
ORDER BY datetime DESC
LIMIT 50 OFFSET 0 )
AS seltable
LEFT JOIN table1 AS tbl1 ON seltable.tbl1_uid = tbl1.uid
LEFT JOIN table2 AS tbl2 ON tbl1.tbl2_uid = tbl2.uid
LEFT JOIN table3 AS tbl3 ON tbl2.tbl3_uid = tbl3.uid
LEFT JOIN table4 AS tbl4 ON tbl3.tbl4_uid = tbl4.uid;
Now I've got no clue how to accomplish filtering the results with a condition related to one of the join tables.
When I just set a:
LEFT JOIN tablex AS table ON foreign_table.tblx_uid = table.uid AND {condition}
this condition regards only to the 50 results of the nested SELECT.
Is there any way to achieve using WHERE clauses on the JOIN tables in this scenario?
For sample data see http://sqlfiddle.com/#!2/fad4d/2
Expected results:
to get x team records limited to 5 team uids, where Tournament2 is one of the related tournaments for the team.
Best regards
w1ll1
Try not controlling the pagination in that subquery, instead just use a more conventional query with a composite where clause. HOWEVER, because you are using left joins take care adding filters through the where clause that would override the outer join to produce the effect of an inner join.
SELECT seltable.*,
tbl2.name AS tbl2name,
tbl3.name AS tbl3name,
tbl4.name AS tbl4name
FROM selecttable AS seltable
LEFT JOIN table1 AS tbl1 ON seltable.tbl1_uid = tbl1.uid
LEFT JOIN table2 AS tbl2 ON tbl1.tbl2_uid = tbl2.uid
LEFT JOIN table3 AS tbl3 ON tbl2.tbl3_uid = tbl3.uid
LEFT JOIN table4 AS tbl4 ON tbl3.tbl4_uid = tbl4.uid
WHERE seltable.value = 99
...
ORDER BY seltable.datetime DESC
LIMIT 50 OFFSET 0
Alternatively use more subqueries, like this:
SELECT seltable.*,
tbl2.name AS tbl2name,
tbl3.name AS tbl3name,
tbl4.name AS tbl4name
FROM
( SELECT * FROM selecttable
WHERE value = 99
ORDER BY datetime DESC
LIMIT 50 OFFSET 0 )
AS seltable
LEFT JOIN ( SELECT uid, name
FROM table1
WHERE 1=1 -- amend to suit
) AS tbl1 ON seltable.tbl1_uid = tbl1.uid
LEFT JOIN ( SELECT uid, name
FROM table2
WHERE 1=1 -- amend to suit
) AS tbl2 ON tbl1.tbl2_uid = tbl2.uid
LEFT JOIN ( SELECT uid, name
FROM table3
WHERE 1=1 -- amend to suit
) AS tbl3 ON tbl2.tbl3_uid = tbl3.uid
LEFT JOIN ( SELECT uid, name
FROM table4
WHERE 1=1 -- amend to suit
) AS tbl4 ON tbl3.tbl4_uid = tbl4.uid;
Here is another attempt, based on your sqlfiddle it appears that INNER JOINS may be used:
SELECT theteam.*,
trnmnt.name AS tournamentname,
cat.name AS categoryname,
sport.name AS sportname
FROM (
SELECT * FROM team
ORDER BY team.name ASC )
AS theteam
INNER JOIN tournament_team AS tntm ON tntm.team_uid = theteam.uid
INNER JOIN tournament AS trnmnt ON tntm.tournament_uid = trnmnt.uid AND trnmnt.name = 'Tournament2'
INNER JOIN category AS cat ON trnmnt.category_uid = cat.uid
INNER JOIN sport ON cat.sport_uid = sport.uid
LIMIT 5 OFFSET 0
;
The result of that query is:
| UID | NAME | TOURNAMENTNAME | CATEGORYNAME | SPORTNAME |
|-----|--------|----------------|--------------|-----------|
| 2 | Team02 | Tournament2 | Germany | Soccer |
| 3 | Team03 | Tournament2 | Germany | Soccer |
| 4 | Team04 | Tournament2 | Germany | Soccer |
| 5 | Team05 | Tournament2 | Germany | Soccer |
| 6 | Team06 | Tournament2 | Germany | Soccer |

SQL Query that counts grouped by results not working as intended

My goal is to get a table that counts the correct answers from a game.
For example I want this
| G.Name | E.Action
| game 1 | correctAnswer
| game 1 | correctAnswer
| game 2 | correctAnswer
| game 3 | correctAnswer
| game 3 | correctAnswer
to become this
| G.Name | Count(*)
| game 1 | 2
| game 2 | 1
| game 3 | 2
the problem is that im getting this instead:
| G.Name | Count(*)
| game 1 | 5
| game 2 | 5
| game 3 | 5
where 5 is the sum of 2+1+2
This is my query
SELECT G.Name, Count(*)
FROM enduser EU
INNER JOIN prescription P
ON EU.UserRefID = P.EndUserRefID
INNER JOIN prescriptiongame PG
ON PG.PrescriptionRefID = P.PrescriptionID
INNER JOIN games G
ON G.GameID = PG.GameRefID
INNER JOIN session S
ON S.PrescriptionRefID = P.PrescriptionID
INNER JOIN entries E
ON E.SessionGameRefID = S.Session
WHERE P.EndUserRefID = 889
AND E.Action = 'correctAnswer'
GROUP BY G.Name
ORDER BY G.Name
How can I solve this issue, when I googled people are using the same method but they get good results
Thanks in advance
I'd bet money that one of your joins is inflating the result. Strip your query down to just the critical tables: prescription, session, and entries. Do you still get excessive results? If not, try adding joins back in, one at a time, until you get over-counting.
Your joins are somehow multiplying the number of rows. This happens when you join tables along different dimensions that are not related to each other. This is a common problem, but I don't understand your data structure so the following is a best-guess.
Your example can be fixed by using count(distinct) on something. So try this:
SELECT G.Name, Count(distinct SessionGameRefID)
Should there be a relationship between session and game?
I wonder if this simplification of your query would fix the problem (it removes the first two tables):
SELECT G.Name, Count(*)
FROM prescriptiongame PG
INNER JOIN games G
ON G.GameID = PG.GameRefID
INNER JOIN session S
ON S.PrescriptionRefID = PG.PrescriptionRefID
INNER JOIN entries E
ON E.SessionGameRefID = S.Session
WHERE PG.EndUserRefID = 889
AND E.Action = 'correctAnswer'
GROUP BY G.Name
ORDER BY G.Name;
supposing all the joins are needed try this query:
SELECT G.Name, Count(G.Name)
FROM enduser EU
INNER JOIN prescription P
ON EU.UserRefID = P.EndUserRefID
INNER JOIN prescriptiongame PG
ON PG.PrescriptionRefID = P.PrescriptionID
INNER JOIN games G
ON G.GameID = PG.GameRefID
INNER JOIN session S
ON S.PrescriptionRefID = P.PrescriptionID
INNER JOIN entries E
ON E.SessionGameRefID = S.Session
WHERE P.EndUserRefID = 889
AND E.Action = 'correctAnswer'
GROUP BY G.Name
ORDER BY G.Name
select G.Name , count(*) from enduser EU, prescription P,prescriptiongame PG ,
games G ,session S ,entries E where P.EndUserRefID = 889 and E.action='correctAnswer' and (EU.UserRefID = P.EndUserRefID) and (PG.PrescriptionRefID = P.PrescriptionID) and (G.GameID = PG.GameRefID) and (S.PrescriptionRefID = P.PrescriptionID) and (E.SessionGameRefID = S.Session)
GROUP BY G.Name
ORDER BY G.Name

Can I fire two select statements in a single MySQL command and count resulting rows?

Here's my code as is, working, but very slow:
$graded = R::getAll("SELECT posts.id, posts.discussion, rating.rating, rating.itemid
FROM uv_forum_posts posts
JOIN uv_rating rating ON ( posts.id = rating.itemid )
WHERE posts.discussion = :discussion_id
GROUP BY posts.userid",
array(':discussion_id' => $discussion['id']));
$total = R::getAll("SELECT posts.userid
FROM uv_forum_posts posts
WHERE posts.discussion = :discussion_id
GROUP BY userid",
array(':discussion_id' => $discussion['id']));
$percentages[] = count($graded) / count($total) * 100;
$graded represents all rows that have a rating.
$total represents all user participation, regardless of being graded or not.
I'm only interested in the numerical values of the resulting sets, can I combine these two MySQL calls into a single call that returns two numbers graded and total?
If I correctly understand your requirements you can try
SELECT p.userid,
COUNT(*) total,
COUNT(r.itemid) graded
FROM uv_forum_posts p LEFT JOIN
uv_rating r ON p.id = r.itemid
WHERE p.discussion = :discussion_id
GROUP BY p.userid
Output:
| USERID | TOTAL | GRADED |
---------------------------
| 1 | 8 | 2 |
| 2 | 4 | 4 |
SQLFiddle
UPDATE: If you just want grand total for all posts and users then
SELECT COUNT(*) total,
COUNT(r.itemid) graded
FROM uv_forum_posts p LEFT JOIN
uv_rating r ON p.id = r.itemid
WHERE p.discussion = 1
Output:
| TOTAL | GRADED |
------------------
| 12 | 6 |
SQLFiddle
You could simply use a query as in the format :
Select
( SELECT posts.id, posts.discussion, rating.rating, rating.itemid
FROM uv_forum_posts posts
JOIN uv_rating rating ON ( posts.id = rating.itemid )
WHERE posts.discussion = :discussion_id
GROUP BY posts.userid,
array(':discussion_id' => $discussion['id']))) gradeCount,
(SELECT posts.userid
FROM uv_forum_posts posts
WHERE posts.discussion = :discussion_id
GROUP BY userid,
array(':discussion_id' => $discussion['id'])))totalCount