Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can I use other than Group By?

I have a question that uses this DML statement

SELECT SupplierID, COUNT(*) AS TotalProducts
FROM Products
GROUP BY SupplierID;

I'm trying to get the same results without using "Group By". I can use a table variable or a temp table, with Insert and Update if needed. Also, using While and IF-Else is allowed.

I'm really lost any help would be awesome. Thanks SO Community.

This is used in SQL Server. Thanks again.

like image 424
Ryan Butler Avatar asked Nov 05 '17 14:11

Ryan Butler


Video Answer


1 Answers

You can always use SELECT DISTINCT with window functions:

SELECT DISTINCT SupplierID,
       COUNT(*) OVER (PARTITION BY SupplierId) AS TotalProducts
FROM Products;

But GROUP BY is the right way to write an aggregation query.

like image 163
Gordon Linoff Avatar answered Sep 20 '22 20:09

Gordon Linoff