Count up different products - mysql

I have the following table:
order_id product_id
1 102
2 105
3 102
4 96
5 96
How would I get a count of the product_ids. The result I am looking to attain is:
product_id count
96 2
102 2
105 1
The query I have tried is:
SELECT product_id, sum(product_id) from orders
But this of course produces an incorrect result with only one row. What would be the correct query here?

SELECT product_id, COUNT(*) FROM orders GROUP BY product_id
Or if you want to name it...
SELECT product_id, COUNT(*) as product_count FROM orders GROUP BY product_id

try this
SELECT product_id, count(*) as count from orders GROUP BY product_id

Related

name and sum from 2 different tables

I have 2 tables.
table customer have. id , name , age
table order have . id, customer_id , order_amount , order date.
I want to show all name from customer table and sum of order amount from order table according to customer.
customer_id
Name
age
1
Alice
24
2
Bob
52
3
Carol
45
4
Dave
51
order_id
customer_id
order_amount
order_date
1
2
50
2012-4-5
2
1
27
2012-8-1
3
2
12
2013-5-20
4
4
25
2014-1-25
5
4
30
2014-5-30
6
1
20
2014-6-22
EDIT
I tried this but it gives me only bob and sum of all columns instead of separate sum of customers
SELECT customers.name, SUM(orders.order_amount) FROM `orders` INNER JOIN customers WHERE orders.customer_id = customers.customer_id;
Joining condition must be on ON clause, not in WHERE.
You must specify for what group the sum must be calculated.
SELECT customers.name, SUM(orders.order_amount)
FROM `orders`
INNER JOIN customers ON orders.customer_id = customers.customer_id
GROUP BY customers.name;

difference between having column_A in & having column_A = in Mysql

The question comes from leetcode sql question sales analysis I (https://leetcode.com/problems/sales-analysis-i/).
The question asks for the best seller, output expected to be:
| seller_id |
| 1 |
| 3 |
table schema:
Sales table:
seller_id
product_id
buyer_id
sale_date
quantity
price
1
1
1
2019-01-21
2
2000
1
2
2
2019-02-17
1
800
2
2
3
2019-06-02
1
800
3
3
4
2019-05-13
2
2800
I used two ways:
way 1:
SELECT seller_id
FROM sales
GROUP BY seller_id
HAVING Sum(price) IN (SELECT Max(total_sales) AS top_sales
FROM (SELECT seller_id,
Sum(price) AS total_sales
FROM sales
GROUP BY seller_id)T)
output is 1,2,3
way 2:
SELECT seller_id
FROM sales
GROUP BY seller_id
HAVING Sum(price) = (SELECT Max(total_sales) AS top_sales
FROM (SELECT seller_id,
Sum(price) AS total_sales
FROM sales
GROUP BY seller_id)T)
output is 1,3
The only differences between two queries is first one use 'IN', second one use '=', but the output is different. What's wrong with the first one?
These are the same. IN works where there are multiple rows in the subquery. However, the MAX() subquery can only return one row, because it is an aggregation query with no GROUP BY.
This construct can be written without nested subqueries:
HAVING Sum(price) = (SELECT Sum(price) AS total_sales
FROM sales
GROUP BY seller_id
ORDER BY total_sales DESC
LIMIT 1
)
However, nowadays, it is better to learn how to do this with window functions.

How to get right sum from inner join in mysql

I have two tables one is orders and second is order_product in which I have to find out orders count, product count, totalamount in corresponding to store using store id from which I have successfully find out the orders count and product count but my totalamount is not coming correct.
orders:
...........................
order_id or_total_amt
...........................
1 10
2 10
3 10
order_product
.................................
op_id op_order_id st_id
.................................
1 1 1
2 2 2
3 3 1
4 3 1
I want below output but my totalamount value is coming wrong it is coming 30,but the correct value is 20 which i have mentioned in the right output below.
output which i want:
.........................................
st_id orders product totalmount
.........................................
1 2 3 20
2 1 1 10
I have tried the below query which is giving 30 value of totalamount which is wrong.
SELECT `op_st_id`,count(distinct orders.`order_id`)as orders,count(order_product.op_pr_id) as product
,sum(orders.or_total_amt) as totalamount from orders
inner JOIN order_product on orders.order_id=order_product.op_order_id
group by `op_st_id`
SELECT
`st_id`,
count(DISTINCT orders.`order_id`) AS orders,
count(order_product.op_id) AS product,
count(DISTINCT orders.`order_id`)*(sum(orders.or_total_amt)/count(order_product.op_id)) AS totalamount
FROM
orders
INNER JOIN order_product ON orders.order_id = order_product.op_order_id
GROUP BY
`st_id`

How to get list of product having a specific property id's

I have a table having columns 'product_id' and 'property_id'. Some products having both property 20 & 21, some of them only have 20 or 21.
product_id property_id
1 20
1 21
2 20
3 21
3 20
4 21
I need to get list of product having both property_id 20 and 21. In here I needed the product list of 1 & 3 like below,
product_id
1
3
This should gives you the correct result.
select product_id
from `xy`
where property_id in (20,21)
group by product_id
having count( distinct property_id)=2
The the sqlfiddle
Use IN and COUNT(distinct property_id):
SELECT product_id
FROM TableName
WHERE property_id IN (20,21)
GROUP BY product_id
HAVING COUNT(distinct property_id) = 2
Explanation:
IN checks if property_id has values either 20 or 21.
COUNT (distinct property_id) = 2 verifies having two different values of property_id. So, when product 2 has 2 same values in property_id like (20,20), it will not be selected.
Result:
product_id
----------
1
3
Sample result in SQL Fiddle.
Make use of IN to check multiple values.
Select product_id
from products
where property_id in(20,21)
group by product_id
having count(property_id) >=2
SELECT `product_id` FROM TableName
WHERE `property_id` IN (20,21)
GROUP BY `product_id`
HAVING COUNT(*) = 2

DISTINCTROW by id

SELECT
DISTINCTROW ProductsId AS pid,( SELECT SUM(QuantityByID) WHERE ProductsId=pid )
FROM
cartitem
I want to get this result
ProductId Quantity
7 7
13 2
14 2
9 1
It's hard to help without a question but I think you want a GROUP BY:
SELECT ProductsId as pid, SUM(QuantityByID)
FROM products
GROUP BY ProductsId
This will give the sum of quantity for each product id.