Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - GROUP BY on one column

I'm trying to get orders from an orderview. In my view I do have some rows with exactly the same values, but I want to group these values on orderid and take the sum of the quantity of that order.

My view results something like:

 Order_id   Customer_id   Article_id   Delivery_date   Quantity
 ---------------------------------------------------------------------------
 PR10.001   11            20.001a      17-04-2013      1
 PR10.001   11            20.001a      17-04-2013      1
 PR10.001   11            20.001a      17-04-2013      1
 PR13.001   15            41.022b      19-04-2013      1
 PR13.001   15            41.022b      19-04-2013      1

I want to do something like:

SELECT Order_id, Customer_id Article_id, Delivery_date, sum(Quantity)
FROM Orders
GROUP BY Order_id

To get something like:

 Order_id   Customer_id   Article_id   Delivery_date   Quantity
 ---------------------------------------------------------------------------
 PR10.001   11            20.001a      17-04-2013      3
 PR13.001   15            41.022b      19-04-2013      2

But I know grouping by one single column is not possible, otherwise you'll get the message:

[...] is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Is there another possibility or workaround to group by one specific column in SQL Server?

like image 985
Jovano Avatar asked Apr 16 '13 09:04

Jovano


People also ask

Can you GROUP BY one column in SQL?

The SQL GROUP BY StatementThe GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

How do I Group A column in SQL?

To arrange similar (identical) data into groups, we use SQL GROUP BY clause. The SQL GROUP BY clause is used along with some aggregate functions to group columns that have the same values in different rows. We generally use the GROUP BY clause with the SELECT statement, WHERE clause, and ORDER BY clauses.

Does GROUP BY need to include all columns?

If you specify the GROUP BY clause, columns referenced must be all the columns in the SELECT clause that do not contain an aggregate function. These columns can either be the column, an expression, or the ordinal number in the column list.

Can we use GROUP BY on more than one column?

We can group the resultset in SQL on multiple column values. When we define the grouping criteria on more than one column, all the records having the same value for the columns defined in the group by clause are collectively represented using a single record in the query output.


1 Answers

You could use a CTE with SUM(Quantity)OVER(PARTITION BY Order_id) + ROW_NUMBER to pick out the desired row from the order-group:

WITH cte 
     AS (SELECT order_id, 
                customer_id, 
                article_id, 
                delivery_date, 
                quantity=Sum(quantity) 
                           OVER( 
                             partition BY order_id), 
                rn = Row_number() 
                       OVER( 
                         partition BY order_id 
                         ORDER BY delivery_date ASC) 
         FROM   orders) 
SELECT order_id, 
       customer_id, 
       article_id, 
       delivery_date, 
       quantity 
FROM   cte 
WHERE  rn = 1 

DEMO

However, your desired result seems to be incorrect (question edited)

This is my result:

ORDER_ID    CUSTOMER_ID ARTICLE_ID  DELIVERY_DATE   QUANTITY
PR10.001    11          20.001a         17-04-2013  3
PR13.001    15          41.022b         19-04-2013  2
like image 72
Tim Schmelter Avatar answered Oct 11 '22 03:10

Tim Schmelter