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
429
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
  • 4,301
  • 2
  • 15
  • 6
405
votes
31 answers

SELECT list is not in GROUP BY clause and contains nonaggregated column .... incompatible with sql_mode=only_full_group_by

I'm using MySQL 5.7.13 on my windows PC with WAMP Server My problem is while executing this query SELECT * FROM `tbl_customer_pod_uploads` WHERE `load_id` = '78' AND `status` = 'Active' GROUP BY `proof_type` I'm getting always error like…
Dhanu K
  • 11,288
  • 6
  • 24
  • 38
404
votes
7 answers

Apply multiple functions to multiple groupby columns

The docs show how to apply multiple functions on a groupby object at a time using a dict with the output column names as the keys: In [563]: grouped['D'].agg({'result1' : np.sum, .....: 'result2' : np.mean}) .....: Out[563]:…
beardc
  • 20,283
  • 17
  • 76
  • 94
401
votes
11 answers

Pandas 'count(distinct)' equivalent

I am using Pandas as a database substitute as I have multiple databases (Oracle, SQL Server, etc.), and I am unable to make a sequence of commands to a SQL equivalent. I have a table loaded in a DataFrame with some columns: YEARMONTH, CLIENTCODE,…
Adriano Almeida
  • 5,186
  • 5
  • 20
  • 28
382
votes
6 answers

What does SQL clause "GROUP BY 1" mean?

Someone sent me a SQL query where the GROUP BY clause consisted of the statement: GROUP BY 1. This must be a typo right? No column is given the alias 1. What could this mean? Am I right to assume that this must be a typo?
Spencer
  • 21,348
  • 34
  • 85
  • 121
381
votes
11 answers

How do I Pandas group-by to get sum?

I am using this dataframe: Fruit Date Name Number Apples 10/6/2016 Bob 7 Apples 10/6/2016 Bob 8 Apples 10/6/2016 Mike 9 Apples 10/7/2016 Steve 10 Apples 10/7/2016 Bob 1 Oranges 10/7/2016 Bob 2 Oranges 10/6/2016 Tom …
Trying_hard
  • 8,931
  • 29
  • 62
  • 85
365
votes
4 answers

Reason for Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I got an error - Column 'Employee.EmpID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. select loc.LocationID, emp.EmpID from Employee as emp full join Location as loc on…
david blaine
  • 5,683
  • 12
  • 46
  • 55
342
votes
4 answers

Count unique values per groups with Pandas

I need to count unique ID values in every domain. I have data: ID, domain 123, 'vk.com' 123, 'vk.com' 123, 'twitter.com' 456, 'vk.com' 456, 'facebook.com' 456, 'vk.com' 456, 'google.com' 789, 'twitter.com' 789, 'vk.com' I try df.groupby(['domain',…
Arseniy Krupenin
  • 3,800
  • 3
  • 13
  • 18
333
votes
9 answers

MongoDB SELECT COUNT GROUP BY

I am playing around with MongoDB trying to figure out how to do a simple SELECT province, COUNT(*) FROM contest GROUP BY province But I can't seem to figure it out using the aggregate function. I can do it using some really weird group…
Steven
  • 13,250
  • 33
  • 95
  • 147
319
votes
16 answers

What's faster, SELECT DISTINCT or GROUP BY in MySQL?

If I have a table CREATE TABLE users ( id int(10) unsigned NOT NULL auto_increment, name varchar(255) NOT NULL, profession varchar(255) NOT NULL, employer varchar(255) NOT NULL, PRIMARY KEY (id) ) and I want to get all unique values of…
vava
  • 24,851
  • 11
  • 64
  • 79
307
votes
9 answers

pandas groupby, then sort within groups

I want to group my dataframe by two columns and then sort the aggregated results within those groups. In [167]: df Out[167]: count job source 0 2 sales A 1 4 sales B 2 6 sales C 3 3 sales D 4 …
JoeDanger
  • 3,325
  • 4
  • 13
  • 14
303
votes
3 answers

LINQ with groupby and count

This is pretty simple but I'm at a loss: Given this type of data set: UserInfo(name, metric, day, other_metric) and this sample data set: joe 1 01/01/2011 5 jane 0 01/02/2011 9 john 2 01/03/2011 0 jim 3 01/04/2011 1 jean 1 01/05/2011 3 jill 2…
Gio
  • 4,099
  • 3
  • 30
  • 32
299
votes
7 answers

Is it possible to GROUP BY multiple columns using MySQL?

Is it possible to GROUP BY more than one column in a MySQL SELECT query? For example: GROUP BY fV.tier_id AND 'f.form_template_id'
Rhys Thought
  • 3,019
  • 2
  • 17
  • 5
297
votes
16 answers

Pandas percentage of total with groupby

This is obviously simple, but as a numpy newbe I'm getting stuck. I have a CSV file that contains 3 columns, the State, the Office ID, and the Sales for that office. I want to calculate the percentage of sales per office in a given state (total of…
erikcw
  • 10,787
  • 15
  • 58
  • 75
295
votes
13 answers

MySQL order by before group by

There are plenty of similar questions to be found on here but I don't think that any answer the question adequately. I'll continue from the current most popular question and use their example if that's alright. The task in this instance is to get…
Rob Forrest
  • 7,329
  • 7
  • 52
  • 69