table 1
categoryid categoryname categorydescription
1 a zzzzzzz
2 b yyyyyy
3 c uuuuu
table 2
carid caryear carmodel carprice catid(foreign key to category id )
1 xxxx aaaa xxxx 1
2 xxxx bbbb xxxx 3
3 xxxx cccc xxxx 4
3 xxxx dddd xxxx 3
4 xxxxx eeee xxxx 1
results
categoryname averageprice total cars
a sum price of same category car / no of same category cars 1
b sum price of same category car / no of same category cars 2
c sum price of same category car / no of same category cars 2
You can write:
SELECT category.categoryname,
AVG(car.carprice) AS "averageprice",
COUNT(car.carid) AS "total cars"
FROM category
LEFT
OUTER
JOIN car
ON car.catid = category.categoryid
GROUP
BY category.categoryname
;
Notes:
You didn't mention the names of your tables, so I had to guess.
This will include categories that don't have any cars. If you'd only like to include categories that have at least one car, drop the LEFT OUTER part.
Related
I have 3 tables product, version and Currency.
Is it possible to query products that have the MAX version but has not yet have any currency where currency version_id = the id of the MAX version?
EDIT
Requested to have example data.
product table
id name
1 product a
2 product b
3 product c
4 product d
version table
id product_id version_no
1 1 1
2 2 1
3 2 2
4 3 1
5 3 2
6 4 1
currency table
id product_id version_id currency
1 1 1 USD
2 2 2 USD
3 2 3 USD
4 3 4 USD
From this data, I should get this returned:
product_id product_name
3 product c
4 product d
This is because on Max versions of the following products
product_id version
1 1
2 2
3 2
4 1
Only product 3 and 4 have no entries on the currency table with these version_nos
Is this clear enough?
Use left join: and the where condition with null value will give you your desired result
select product_id,name
from
(select product_id, max(version_id) as vid
from versiontable
group by product_id) v
left join currencytable c on v.product_id=c.product_id and v.vid=c.version_id
left join procuttable p on p.id=v.product_id
where c.product_id is null and c.version_id is null
I have three tables: Products, Inventory and Ingredients
Product Table
ID[PK] Name Type
1 OrdinaryBurger Burger
2 CheeseBurger Burger
Inventory table
ID[PK] Item_name Stocks
100 Buns 5
101 Patties 5
103 Cheese 0
Ingredients table
ID[PK] ProductID[FK] InventoryID[FK] Quantity
1001 1 100 1
1002 1 101 1
1003 2 100 1
1004 2 101 1
1005 2 103 1
I want to write a query that can filter all the products to not display if the connected inventory stock is 0, for example this will not display Cheeseburger because the stock of cheese is 0. THanks
The following will return only products that have at least enough ingredients to make.
SELECT * FROM Products WHERE ProductID NOT IN (
SELECT i.ProductID
FROM Ingredients i
JOIN Inventory iv ON i.InventoryID = iv.InventoryId
GROUP BY i.ProductID, i.InventoryId
HAVING SUM(iv.Stocks) < SUM(i.Quantity)
)
If I have a "SALES" table, with columns of SaleID, Product#, and CustomerName. and a PRODUCTS table with two columns product_ID and Name. The contains 5 differnt products. In the SALES table populates when a sale is made.
How would I query customer_name with only Product_ID of 1 and 2?
sales table
SALES_ID PRODUCT_ID CUSTOMER_NAME
1 1 DAVE
2 2 DAVE
3 3 DAVE
4 1 TOM
5 2 TOM
6 1 JANE
7 1 MIKE
8 1 MIKE
9 3 MIKE
10 4 MARY
I would like a table result to be
SALES_ID PRODUCT_ID CUSTOMER_NAME
1 1 TOM
2 2 TOM
Select s.CustomerName from SALES s
INNER JOIN PRODUCTS p ON s.Product#=p.Product#
WHERE p.Product# =1
INTERSECT
Select s.CustomerName from SALES s
INNER JOIN PRODUCTS p ON s.Product#=p.Product#
WHERE p.Product# =2
If I have a MySQL table looking something like this:
breeds
id name
-------------------------------
1 Labrador
2 Jack Russel Terrier
3 Shetland Sheepdog
And a MySQL table looking like this:
dogs
id owner breed sex
-----------------------------------
1 Sara 1 f
2 Kent 1 f
3 Billy 1 m
4 Joe 2 f
5 Billy 2 m
Is it possible to run a MySQL query to get output like this:
id name females males
------------------------------------------------
1 Labrador 2 1
2 Jack Russel Terrier 1 1
3 Shetland Sheepdog 0 0
I would like to have a JOIN or similar that count the number of females/males from the dogs table.
You can do this:
SELECT b.id,b.name,
IFNULL(SUM(CASE WHEN sex='f' THEN 1 ELSE 0 END),0) as females,
IFNULL(SUM(CASE WHEN sex='m' THEN 1 ELSE 0 END),0) as males
FROM breeds b LEFT JOIN
dogs d on b.id=d.breed
GROUP BY b.id,b.name
Explanation:
using LEFT JOIN will include the record eventhough there is male/female count. IFNULL will replace the null value with 0.
Result:
id name females males
-------------------------------------
1 Labrador 2 1
2 Jack Russel Terrier 1 1
3 Shetland Sheepdog 0 0
Sample result in SQL Fiddle.
Or alternatively:
SELECT id, name,
(SELECT COUNT(*) FROM dogs WHERE breed=b.id AND sex='f') females,
(SELECT COUNT(*) FROM dogs WHERE breed=b.id AND sex='m') males
FROM breeds b
see here: http://www.sqlfiddle.com/#!9/03da0/1
I have 3 tables (CompanyProfile, IndustryTable, MainTable)
CompanyProfile
CompanyCode CompanyName IndustryCode
AAAA Company A 3
BBBB Company B 1
CCCC Company C 4
DDDD Company D 1
EEEE Company E 1
GGGG Company F 2
IndustryTable
IndustryCode IndustryName status
1 Manufacturing ACTIVE
2 Sales ACTIVE
3 Logistics ACTIVE
4 Energy DEACTIVATED
MainTable
CompanyCode field2
AAAAA SampleRecord1
AAAAA SampleRecord2
DDDDD SampleRecord3
CCCCC SampleRecord4
EEEEE SampleRecord5
Now I need a Query to Get all the RecordCount from MainTable Group By IndustryTable.IndustryCode (ACTIVE Only), So the above example should give the following output
IndustryCode IndustryName RecordCount (Explanation)
1 Manufacturing 2 -Record 3 and 5
2 Sales 0
3 Logistics 2 -Record 1 and 2
Record 4 (CCCCC) will not be show since IndustryTable.IndustryCode 4 is DEACTIVATED
Something like
select industrycode, industryname, count(*) as total from companyprofile t1 join industry table t2 join maintable t3 where t1.companycode=t3.companycode and t1.industrycode=t2.industrycode and t2.status="ACTIVE" group by industrycode
?