SQL query - two group bys on intermediate result in a query - mysql

I have a fairly long and complex query that ends up producing a result like this:
item_id | shop_id | sold
5 3 0
5 3 1
5 3 1
8 4 0
8 6 1
8 9 0
I need to then to somehow do a SELECT item_id, COUNT(DISTINCT (shop_id)) FROM tableAbove GROUP BY item_id to get:
item_id | number of shops that sell/have sold item
5 1
8 3
However, I also need for each item the number of shops that have successfully sold it, So something like this:
item_id | number of shops that have sold item
5 1
8 1
Does anyone know how I can put them bother together in a query to give
item_id | shops that sell item |number of shops that have sold item
5 1 1
8 3 1

SELECT item_id,
COUNT(DISTINCT shop_id),
COUNT(DISTINCT CASE WHEN sold = 1 THEN shop_id END)
FROM my_query
GROUP BY
item_id

Related

JOIN WHERE Right Table Have No Entries

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

SQL query that finds all the column, that is in multiple rows, but have different value in another column?

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

How to count how many times that each pair of item has same order id in mySQL

i want to count that how many times that each pair of SKU have been ordered together(have same orderid)
I have an order table of customers that has 2 column ORDERID and SKUID
for an example
ORDERID SKUID
1 1
1 2
1 3
2 1
2 2
2 4
3 1
3 4
i want a result table as
SKU1 SKU2 COUNT
1 2 2
1 3 1
1 4 2
2 3 1
2 4 1
3 4 0
thank you
You can do this with a self-join and aggregation:
select o1.sku, o2.sku, count(distinct o1.orderid) as numorders
from orders o1 join
orders o2
on o1.orderid = o2.orderid and o1.sku < o2.sku;
If you know that the skus are unique in each order, then you can use count(*) instead of count(distinct).

Most recent distinct record from a joined MySQL table

I have two tables, one of a list of competition results, and one with ELO ratings (based on previous competitions).
Fetching the list of competitors for an arbitrary competition is trivial enough, but I also need to get the most recent rating value for them.
score:
id | eventid | competitorid | position
1 1 1 1
2 1 2 2
3 1 3 3
4 2 2 1
5 2 3 2
6 3 1 1
7 3 3 2
8 3 2 3
rating:
id | competitorid | rating
1 1 1600
2 2 1500
3 3 1500
4 2 1600
5 3 1590
Expected output for a query against score.eventid = 3 would be
id | competitorid | position | rating
6 1 1 1600
7 3 2 1590
8 2 3 1600
At the moment my code looks like:
SELECT score.scoreID, score.competitorID, score.position,
rating.id, rating.rating
FROM score, rating
WHERE score.competitorid = rating.competitorid
AND score.eventid = 3
ORDER BY score.position
which gives an output of
id | competitorid | position | rating.id | rating
6 1 1 1 1600
7 3 2 2 1500
7 3 2 4 1590
8 2 3 3 1500
8 2 3 5 1600
basically it's showing the data from the score table for that correct event, but giving me a row for every rating available against that competitorID unfortunately I have no idea where to build in the DISTINCT statement or how to limit it to the most recent result.
MySQL noob, and managed DISTINCT statements, but not with joins. Unfortunately most previous questions seemed to deal with getting distinct results from a single table, which is not quite what I'm after. Thanks!
One way to get the rating is with a correlated subquery:
SELECT s.scoreID, s.eventID, s.competitorID, s.position,
(select r.rating
from rating r
where s.competitorID = r.competitorID
order by r.id desc
limit 1
) as rating
FROM score s
WHERE s.eventID = 3
ORDER BY s.position;
I'm not sure what ratingprid is, so this only includes the rating.

My sql query sum of highest points for each category

upload table
id category_id
1 1
2 2
3 3
4 1
In Ratings Table
id upload_id points
1 1 5
2 2 3
3 3 2
4 4 2
5 1 5
I want to display all the records from ratings table except id 4 because id 4 is the same category id 1
I excepted result
Uploaded_id 1 has sum of 10 points
Uploaded_id 2 has sum of 3 points
Uploaded_id 3 has sum of 2 points
Please help me.
Thanks In advance
Sasikumar
select upload_id, sum(points)
from rating r,
(select min(id) id from upload group by category_id) a
where a.id = r.upload_id group by upload_id