+ 1

SQL to select count of prices from distinct shopNames

I have a table with content similar to: date | shopName | product | price ----------------------------------------------- 20190701 | ShopA | Product 1 | 10.00 20190701 | ShopB | Product 1 | 15.00 20190708 | ShopA | Product 1 | 10.00 20190708 | ShopB | Product 1 | 15.00 ----------------------------------------------- I want a query that will select the COUNT(ALL) of each Product split on distinct ShopName If I do: SELECT DISTINCT date, shopName, product, COUNT(ALL price) as NumberOfPrices FROM table WHERE product = "Product 1"; I will get a record that gives me one result that contains the NumberOfPrices correctly, but will only have one shopName. How can I split the results into 2 records, 1 shopName per record each wit ha total of 2 NumberOfPrices? I am using SQLite if that matters.

7th Jul 2019, 11:34 AM
Nathan Stanley
Nathan Stanley - avatar
2 odpowiedzi
+ 1
Not quite sure if this works in sqlite but could try. select shopName, count(unique price) from table group by shopName
7th Jul 2019, 11:43 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Yes I found that I had to add an additional field name to my GROUP BY clause. I had GROUP BY product had to have: GROUP BY product,shopName thanks for the help!
7th Jul 2019, 11:45 AM
Nathan Stanley
Nathan Stanley - avatar