I try to calculate the number and percentage of items from 3 tables.
Tables:
categories
cat_id | title
1 | phone
2 | tablet
products
prod_id | cat_id | title
1001 | 1 | iphone
1002 | 1 | nokia
1003 | 1 | blackberry
1004 | 2 | ipad
user_products
id | prod_id | user_id
1 | 1001 | 1
2 | 1001 | 2
3 | 1001 | 3
4 | 1003 | 3
5 | 1004 | 4
What I have so far:
SELECT categories.cat_id, products.title, COUNT(products.title ) AS cnt
FROM categories
LEFT JOIN products ON categories.cat_id = products.cat_id
GROUP BY products.title
Desired result:
Tablets:
Ipad | 1 | 100%
Phones:
Iphone | 3 | 75%
Blackberry | 1 | 25%