Questions tagged [group-by]

GROUP BY is a command in the SQL relational database standard for collapsing a group of rows that share common field value(s) into a single row. Aggregate functions can be performed on other fields in the group, such as SUM() or AVG(), to collate related data into a single value.

About

The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

Aggregate functions can be performed on other fields in the group, such as SUM() or AVG(), to collate related data into a single value.

Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name

Source

MySQL Handling of GROUP BY

In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the select list that are not named in the GROUP BY clause. For example, this query is illegal in standard SQL because the name column in the select list does not appear in the GROUP BY:

SELECT o.custid, c.name, MAX(o.payment)
  FROM orders AS o, customers AS c
  WHERE o.custid = c.custid
  GROUP BY o.custid;

For the query to be legal, the name column must be omitted from the select list or named in the GROUP BY clause.

Source

GROUP BY (Aggregate) Functions

Related Tags :

27499 questions
1491
votes
3 answers

Using group by on multiple columns

I understand the point of GROUP BY x. But how does GROUP BY x, y work, and what does it mean?
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
1308
votes
34 answers

Retrieving the last record in each group - MySQL

There is a table messages that contains data as shown below: Id Name Other_Columns ------------------------- 1 A A_data_1 2 A A_data_2 3 A A_data_3 4 B B_data_1 5 B B_data_2 6 C C_data_1 If…
Vijay Dev
  • 26,966
  • 21
  • 76
  • 96
1273
votes
10 answers

Group by in LINQ

Let's suppose if we have a class like: class Person { internal int PersonID; internal string car; } I have a list of this class: List persons; And this list can have multiple instances with same PersonIDs, for example:…
test123
  • 13,865
  • 9
  • 28
  • 33
1172
votes
14 answers

Group By Multiple Columns

How can I do GroupBy multiple columns in LINQ Something similar to this in SQL: SELECT * FROM GROUP BY , How can I convert this to LINQ: QuantityBreakdown ( MaterialID int, ProductID int, Quantity…
Sreedhar
  • 29,307
  • 34
  • 118
  • 188
904
votes
61 answers

Most efficient method to groupby on an array of objects

What is the most efficient way to groupby objects in an array? For example, given this array of objects: [ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" }, { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }, …
D'Arcy Rail-Ip
  • 11,505
  • 11
  • 42
  • 67
787
votes
18 answers

MySQL Query GROUP BY day / month / year

Is it possible to make a simple query to count how many records I have in a determined period of time like a year, month, or day, having a TIMESTAMP field, like: SELECT COUNT(id) FROM stats WHERE record_date.YEAR = 2009 GROUP BY…
Fernando Barrocal
  • 12,584
  • 9
  • 44
  • 51
778
votes
11 answers

Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

I have a dataframe df and I use several columns from it to groupby: df['col1','col2','col3','col4'].groupby(['col1','col2']).mean() In the above way, I almost get the table (dataframe) that I need. What is missing is an additional column that…
Roman
  • 124,451
  • 167
  • 349
  • 456
608
votes
5 answers

How can I pivot a dataframe?

What is pivot? How do I pivot? Long format to wide format? I've seen a lot of questions that ask about pivot tables, even if they don't know it. It is virtually impossible to write a canonical question and answer that encompasses all aspects of…
piRSquared
  • 285,575
  • 57
  • 475
  • 624
520
votes
14 answers

Using LIMIT within GROUP BY to get N results per group?

The following query: SELECT year, id, rate FROM h WHERE year BETWEEN 2000 AND 2009 AND id IN (SELECT rid FROM table2) GROUP BY id, year ORDER BY id, rate DESC yields: year id rate 2006 p01 8 2003 p01 7.4 2008 p01 6.8 2001 p01…
Wells
  • 10,415
  • 14
  • 55
  • 85
503
votes
18 answers

Error related to only_full_group_by when executing a query in MySql

I have upgraded my system and have installed MySql 5.7.9 with php for a web application I am working on. I have a query that is dynamically created, and when run in older versions of MySQL it works fine. Since upgrading to 5.7 I get this…
Dan Bemowski
  • 5,305
  • 3
  • 14
  • 10
497
votes
13 answers

How to query as GROUP BY in Django?

I query a model: Members.objects.all() And it returns: Eric, Salesman, X-Shop Freddie, Manager, X2-Shop Teddy, Salesman, X2-Shop Sean, Manager, X2-Shop What I want is to know the best Django way to fire a group_by query to my database,…
simplyharsh
  • 35,488
  • 12
  • 65
  • 73
475
votes
15 answers

Get the row(s) which have the max value in groups using groupby

How do I find all rows in a pandas DataFrame which have the max value for count column, after grouping by ['Sp','Mt'] columns? Example 1: the following DataFrame: Sp Mt Value count 0 MM1 S1 a **3** 1 MM1 S1 n 2 2 MM1 S3 …
jojo12
  • 4,853
  • 3
  • 14
  • 7
473
votes
14 answers

How to concatenate strings of a string field in a PostgreSQL 'group by' query?

I am looking for a way to concatenate the strings of a field within a group by query. So for example, I have a table: ID COMPANY_ID EMPLOYEE 1 1 Anna 2 1 Bill 3 2 Carol 4 2 Dave and I wanted to group by company_id to get something…
Guy C
  • 6,970
  • 5
  • 30
  • 30
464
votes
7 answers

must appear in the GROUP BY clause or be used in an aggregate function

I have a table that looks like this caller 'makerar' cname wmname avg canada zoro 2.0000000000000000 spain luffy 1.00000000000000000000 spain usopp 5.0000000000000000 And I want to select the maximum avg for each cname. SELECT cname,…
RandomGuy
  • 4,949
  • 4
  • 16
  • 15
458
votes
26 answers

Is there any difference between GROUP BY and DISTINCT

I learned something simple about SQL the other day: SELECT c FROM myTbl GROUP BY C Has the same result as: SELECT DISTINCT C FROM myTbl What I am curious of, is there anything different in the way an SQL engine processes the command, or are they…
Brettski
  • 19,351
  • 15
  • 74
  • 97
1
2 3
99 100