I'm trying to write a query that gets the counts for a table (call it item) categorized by two different things, call them type and code. What I'm hoping for as output is the following:
Type Code Count
1 A 3
1 B 0
1 C 10
2 A 0
2 B 13
2 C 2
And so forth.
Both type and code are found in lookup tables, and each item can have just one type but more than one code, so there's also a pivot (aka junction or join) table for the codes. I have a query that can get this result:
Type Code Count
1 A 3
1 C 10
2 B 13
2 C 2
and it looks like (with join conditions omitted):
SELECT typelookup.name, codelookup.name, COUNT(item.id)
FROM typelookup
LEFT OUTER JOIN item JOIN itemcodepivot
RIGHT OUTER JOIN codelookup
GROUP BY typelookup.name, codelookup.name
Is there any way to alter this query to get the results I'm looking for? This is in MySQL, if that matters. I'm not actually sure this is possible all in one query, but if it is I'd really like to know how. Thanks for any ideas.
A CROSS JOIN between typelookup and codelookup should solve this problem:
SELECT t.name, c.name, COUNT(item.id)
FROM typelookup t
CROSS JOIN codelookup c
LEFT JOIN itemCodePivot icp ON icp.codeId = c.codeId
LEFT JOIN item i ON i.itemId = icp.itemId AND i.TypeId = t.TypeId
GROUP BY t.name, c.name
Can you use cross join for this?
http://dev.mysql.com/doc/refman/5.0/en/join.html
SELECT typelookup.name, codelookup.name, COUNT(item.id)
FROM codelookup CROSS JOIN typelookup
LEFT OUTER JOIN item JOIN itemcodepivot
GROUP BY typelookup.name, codelookup.name
Cross join should get all types joined with all codes.. Hard to make correct SQL when you don't have all the table declarations but you can give it a try..
Note, that you should substitute your actual JOIN conditions instead of using mine.
SELECT tl.name, cl.name, IFNULL(COUNT(i.id),0)
FROM typelookup tl
LEFT JOIN item i ON tl.item_id = i.id
LEFT JOIN itemcodepivot icp ON icp.item_id = i.id
LEFT JOIN codelookup cl ON cl.id = icp.codelookup_id
GROUP BY tl.name, cl.name;
Try this and see how your counts look.
I'm not understanding your need for a RIGHT JOIN on codelookup. If this (edit) doesn't give you what you want, try adding that back in.
Related
I have two tables, like so:
table "a" contains:
id|name
stock1|fullname
stock2|fullname2
stock3|fullname3
table "b" contains product quantities for given stock.
id|stock_id|quantity|product_id|
1|stock1|3|13
2|stock3|4|13
3|stock1|1|5
4|stock2|2|2
Now I would need to combine those two tables, so that each product takes its stock full name from table "a", and if its quanitity is not given for stock, it would still show the row with the quanitity as 0.
So from my example, product_id 13 would show as:
stock|quanitity|product_id|stock_fullname
stock1|3|13|fullname1
stock2|0|13|fullname2
stock3|4|13|fullname3
You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13
It sounds like you need to use a LEFT JOIN, although the records with no quantity might show as NULL rather than zero. Something like:
SELECT a.*, b.*
FROM table_a a
LEFT JOIN table_b b ON a.stock_id = b.stock_id
try this:
SELECT stock,COALESCE(quanitity,0),product_id,stock_fullname FROM stock JOIN product
You need an outer join so that rows from the a table without a corresponding row in b are still considered. An inner join, by contrast, insists that you have a matching row. If you are pulling a value from the table where you don't have a row, you get NULL. Syntax varies between DBs and there is a distinction made depending on if it's the table on the left or right that gets the fake rows.
see other answers for syntax.
I think this query should work for your example:
SELECT a.id stock if(b.quantity IS NULL, 0, b.quantity),
b.product_id, a.name stock_fullname
FROM b
LEFT JOIN a b.stock = a.id
WHERE b.product_id = 13;
You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13
I need to get data from 3 tables. However, I can only get correct results as long as there are 2 tables. As soon as I join the 3rd table, I get null results. The query that works is:
select a.first_name,a.last_name,a.email, a.birthdate,b.mobile_phone
from REGISTRATION a
left join TRIPS b on a.trip_id=b.trip_id
where a.trip_id=9 and registration_status='Active'
However, as soon as I use following query to get data from 3rd table, I get null results:
select a.first_name,a.last_name,a.email, a.birthdate,b.mobile_phone,d.country_name
from REGISTRATION a
left join TRIPS b
on a.trip_id=b.trip_id and registration_status='Active'
left join DESTINATION_COUNTRY d
on b.destination_country_id=d.destination_country_id
where a.trip_id=9
Please advice me what I am doing wrong.
Thanks a lot.
you should remove this from join "and registration_status='Active'" and move it to where clause :
select a.first_name,a.last_name,a.email, a.birthdate,b.mobile_phone,d.country_name
from REGISTRATION a
left join TRIPS b
on a.trip_id=b.trip_id
left join DESTINATION_COUNTRY d
on b.destination_country_id=d.destination_country_id
where a.trip_id=9 and registration_status='Active'
I have 4 MySQL tables on identity column is in common between all these tables,
Tables in Sequence:
1- Items.
2- Sales.
3- Puchases.
4- Returned.
ItemID appears in all of these tables, WHEN i use LEFT JOIN i get duplicates like:
select
a.ItemID AS ItemID,
a.Item_title AS ItemTitle,
SUM(b.qty) AS SoldQty,
SUM(c.qty) AS PurQty,
SUM(d.qty) AS RetQty
from items a
left join sales b on a.ItemID = b.items_ItemID
left join purchases c on a.ItemID = c.items_itemID
left join returned d on a.ItemID = d.items_ItemID
group by a.ItemID
That query was one of the many tries that i've tried :D the result i get is always unique for sales but duplicates for other tables ..
Thanks for the answer.
If I understand what you're trying to do correctly, you want the total number of sales, purchases, returned etc per item id from a. If that's the case, try thinking about an easier piece of the problem first: how do I get the sum of each sales quantity, grouped by item id?
To do that you'd do something like:
select b.items_ItemID, sum(b.qty) as total_sales_qty
from sales b group by b.items_ItemID
You could do the same thing for tables c and d.
Once you've got those, you can join them all together like this:
select a.ItemID, bb.total_sales_qty, cc.total_purchases_qty, dd.total_returned_qty
from items a
left join (
select b.items_ItemID, sum(b.qty) as total_sales_qty
from sales b group by b.items_ItemID) bb
on a.ItemID = bb.items_ItemID
left join (
select c... etc) cc
on a.ItemID = cc.items_ItemID
... etc
I don't really use MySQL, but in SQL Server, you could use the distinct directive, so duplicate rows appear as just a single row. All the returned columns must be identical, mind you.
Select Distinct a.ItemID AS ItemID,... From....
Hope this helps
I have three tables People, Items and Locations. People can have only 1 Items. Locations has no relation to any of 2 tables. I want to get I record join all 3. I did 2 so far people + items but 3rd I keep getting MySQL errors. There's no JOIN ON for location. Any help?
SELECT * FROM ITEMS i
RIGHT JOIN PEOPLE p
ON (p.ITEM_ID =i.ID) where
p.ID=3
RIGHT JOIN
SELECT * FROM LOCATIONS lo where lo.ID=7
If there are no join keys in common, then you might want to do a cross join. This produces a Cartesian product, that is, every location for each row selected from People/items:
SELECT *
FROM ITEMS i RIGHT JOIN
PEOPLE p
ON (p.ITEM_ID =i.ID) cross join
location l
WHERE p.ID=3
By the way, MySQL has a very flexible (and non-standard) join syntax. You can actually leave the on clause off of a join and it will behave the same as a cross join. That is a bad habit, of course. If you want a cross join, then use cross join explicitly.
I'm assuming you want the location with the ID of 7 to appear on every row...
SELECT *
,(SELECT loc.name FROM Location loc WHERE loc.ID = 7) AS Location
FROM ITEMS i
RIGHT JOIN PEOPLE p
ON (p.ITEM_ID =i.ID)
WHERE p.ID=3
OR
SELECT *
FROM ITEMS i
RIGHT JOIN PEOPLE p
ON (p.ITEM_ID =i.ID)
CROSS JOIN (SELECT * FROM LOCATIONS lo where lo.ID=7) l
WHERE p.ID=3
OR
[several other ways to go about it]
Try something like this:
SELECT peo.id, it.id, loc.id
FROM People as peo
INNER JOIN Items as it on it.id = peo.id
INNER JOIN Locations as loc on loc.id = peo.id
WHERE peo.ID=3
Edit:
Your question was edited while I was typing this so my example doesn't match like it used to. Use ITEM_ID and ID as needed.
Although not recommended, you can also use
SELECT *
FROM People as peo
INNER JOIN Items as it on it.id = peo.id
INNER JOIN Locations as loc on loc.id = peo.id
WHERE peo.ID=3
I have two tables, like so:
table "a" contains:
id|name
stock1|fullname
stock2|fullname2
stock3|fullname3
table "b" contains product quantities for given stock.
id|stock_id|quantity|product_id|
1|stock1|3|13
2|stock3|4|13
3|stock1|1|5
4|stock2|2|2
Now I would need to combine those two tables, so that each product takes its stock full name from table "a", and if its quanitity is not given for stock, it would still show the row with the quanitity as 0.
So from my example, product_id 13 would show as:
stock|quanitity|product_id|stock_fullname
stock1|3|13|fullname1
stock2|0|13|fullname2
stock3|4|13|fullname3
You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13
It sounds like you need to use a LEFT JOIN, although the records with no quantity might show as NULL rather than zero. Something like:
SELECT a.*, b.*
FROM table_a a
LEFT JOIN table_b b ON a.stock_id = b.stock_id
try this:
SELECT stock,COALESCE(quanitity,0),product_id,stock_fullname FROM stock JOIN product
You need an outer join so that rows from the a table without a corresponding row in b are still considered. An inner join, by contrast, insists that you have a matching row. If you are pulling a value from the table where you don't have a row, you get NULL. Syntax varies between DBs and there is a distinction made depending on if it's the table on the left or right that gets the fake rows.
see other answers for syntax.
I think this query should work for your example:
SELECT a.id stock if(b.quantity IS NULL, 0, b.quantity),
b.product_id, a.name stock_fullname
FROM b
LEFT JOIN a b.stock = a.id
WHERE b.product_id = 13;
You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13