DISTINCTROW by id - mysql

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.

Related

Getting the Maximum Average price based on Product Name

I have a Products table with three headers namely ProductID, ProductName and Price.I need to find the name of the product which is having maximum average price. There are multiple products and they are duplicate as well for different product ID and i need their average price.
Sample Data looks like below :
ProductID ProductName Price
1 A 18
2 A 19
3 B 10
4 C 22
5 D 21.35
6 C 25
7 A 30
8 B 40
9 E 97
10 A 31
Query that i have written is as below:
SELECT ProductName
FROM Products
group by ProductName
order by avg(Price) desc
limit 1;
But i was informed that it is not a generic solution. Can anyone please inform, what is wrong in this query.
The only problem with your query is the case where there are more than 1 products with average price equal to the maximum average price, because it returns only 1 row.
To cover this case you need to group again and use your query in the HAVING clause :
select productname
from products
group by productname
having avg(price) = (
select avg(price)
from products
group by productname
order by avg(price) desc
limit 1
);
See the demo.

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

MySQL: Select id if not repeated exactly 4 times

I'm wondering what is the right way to display all id_product if is NOT repeated 4 time.
This is my table:
id_product id_related
55 1
55 2
55 3
55 4
11 1
11 123
11 12
36 12
36 9
36 14
36 654
I need to find products without added 4 related products.
In this case the result i expect is 11.
Something like that
select id_product, count(*)
from <table>
group by id_product
having count(*) < 4
Following query
SELECT id_product
FROM table
GROUP BY id_product
HAVING COUNT(id_product) < 4
Select
p.id_product,
p.id_related
from product p
join
(
select
id_product,
count(id_related) as tot
from product
group by id_product
having tot <> 4
)p1
on p1.id_product = p2.id_product
If you want to filter out duplicate id_related meaning multiple occurrence of same id_related is counted as one you can use distinct as
count(distinct id_related)

Count up different products

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

MySql Result Set Combine Columns

I have this result set from my query:
OrderId CustomerId ProducerId CustomerPayment ProducerPayment
1 1 3 10 5
1 1 4 10 5
1 2 3 10 5
1 2 4 10 5
I need to return this result into this:
OrderId UserId Payment
1 1 20
1 2 20
1 3 10
1 4 10
Just combining the CustomerId and ProducerId into UserId. Same with the Payment Columns.
Is there any way to achieve this with using just a simple select and group by? I'm avoiding temp tables, calling multiple same queries and like for optimization. I hope this is possible.
Thanks a lot
SELECT
OrderId,
CustomerID AS UserId,
SUM (CustomerPayment) As Payment
FROM orders
UNION ALL
SELECT
OrderId,
ProducerId AS UserId,
SUM (ProducerPayment) As Payment
FROM orders
Try something like this:
select
OrderId,
CustomerId,
sum(CustomerPayment) Payment,
group_concat(OrderId separator ',') listOrders /* list all OrderID's from the user and separates these with a , */
from your_table
group by CustomerId
Dont know how you query looks like atm?