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 :

21953 questions
1556
votes
18 answers

Select first row in each GROUP BY group?

As the title suggests, I'd like to select the first row of each set of rows grouped with a GROUP BY. Specifically, if I've got a purchases table that looks like this: SELECT * FROM purchases; My Output: id | customer | total ---+----------+------ …
David Wolever
  • 130,273
  • 78
  • 311
  • 472
1215
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
  • 51,480
  • 273
  • 609
  • 976
1136
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
  • 12,405
  • 7
  • 24
  • 32
1103
votes
30 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
  • 24,306
  • 20
  • 73
  • 96
1053
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
  • 26,251
  • 31
  • 104
  • 163
705
votes
15 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
  • 11,562
  • 8
  • 41
  • 50
635
votes
52 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
  • 8,635
  • 9
  • 35
  • 60
602
votes
20 answers

Get top 1 row of each group

I have a table which I want to get the latest entry for each group. Here's the table: DocumentStatusLogs Table |ID| DocumentID | Status | DateCreated | | 2| 1 | S1 | 7/29/2011 | | 3| 1 | S2 | 7/30/2011 | | 6| 1 …
dpp
  • 25,478
  • 28
  • 95
  • 150
571
votes
8 answers

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

I have a data frame 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 (data frame) that I need. What is missing is an additional column that…
Roman
  • 97,757
  • 149
  • 317
  • 426
476
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,015
  • 3
  • 11
  • 10
456
votes
3 answers

How to pivot a dataframe?

What is pivot? How do I pivot? Is this a pivot? Long format to wide format? I've seen a lot of questions that ask about pivot tables. Even if they don't know that they are asking about pivot tables, they usually are. It is virtually impossible…
piRSquared
  • 240,659
  • 38
  • 359
  • 510
390
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…
Guy C
  • 5,713
  • 4
  • 25
  • 30
384
votes
9 answers

How to get multiple counts with one SQL query?

I am wondering how to write this query. I know this actual syntax is bogus, but it will help you understand what I am wanting. I need it in this format, because it is part of a much bigger query. SELECT distributor_id, COUNT(*) AS TOTAL, COUNT(*)…
Crobzilla
  • 3,963
  • 4
  • 14
  • 11
358
votes
2 answers

C# Linq Group By on multiple columns

public class ConsolidatedChild { public string School { get; set; } public string Friend { get; set; } public string FavoriteColor { get; set; } public List Children { get; set; } } public class Child { public string…
Kasy
  • 3,591
  • 2
  • 13
  • 6
356
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
  • 17,234
  • 13
  • 67
  • 85
1
2 3
99 100