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
5
votes
1 answer

select total count() group by year and month?

For example imagine a table as below select accountid, createdon from account 73C56F61-5FF1-E111-A4F8-005056977FBC 2012-08-28 22:26:47.000 A7C56F61-5FF1-E111-A4F8-005056977FBC 2012-08-28 22:26:48.000 B7C56F61-5FF1-E111-A4F8-005056977FBC …
angel
  • 4,474
  • 12
  • 57
  • 89
5
votes
2 answers

SQL-statement for two group-columns

I have a problem creating a SQL-statement for sqlserver2008. I have the following data: city person priority ----------------------------------- Linz Mike 1 Wien Mike 1 Linz Tom 1 Wien Tom 1 Linz …
Mathias
  • 153
  • 3
5
votes
2 answers

How to return one row from group by multiple columns

I am trying to extract a list of unique customers from a database where some customers are listed more than once. The (almost) duplicate rows exist because customers have been moved from one division to another or because the customers have been…
Peter_DK
  • 53
  • 2
5
votes
5 answers

GROUP BY foreign key or primary key?

I have two tables : user ======= id name class marks ======= id user_id sub_id mark user table contains the details of user (student) marks table contains the marks of a student in different subjects with subject id I want to fetch name, class and…
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
5
votes
2 answers

SQL Server left joining

I'm trying to make left join in one query, but it seems that I'm wrong somewhere. table machines -------------- machineID FaNo Barcode RoutingCode Name table log ------------- logID lineBarcode machineBarcode In the log table there are records…
David White
  • 141
  • 2
  • 3
  • 9
5
votes
2 answers

SQL fastest 'GROUP BY' script

Is there any difference in how I edit the GROUP BY command? my code: SELECT Number, Id FROM Table WHERE(....) GROUP BY Id, Number is it faster if i edit it like this: SELECT Number, Id FROM Table WHERE(....) GROUP BY…
nionios
  • 190
  • 3
  • 11
5
votes
2 answers

linq after groupby unable to get column values

I am getting data from multiple tables by joining and i want to group data on particular column value but after group by statement i can access my aliases and their properties. What mistake i am making? public List
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
5
votes
3 answers

Limit each group in group by

Now I understand that this has been asked several times before, but I have tried to apply different existing solutions to my specific problems for quite a while without success. So I turn here in hope of some guidance. I have a table called…
Adam
  • 115
  • 8
5
votes
2 answers

Faster way to groupby time of day in pandas

I have a time series of several days of 1-minute data, and would like to average it across all days by time of day. This is very slow: from datetime import datetime from pandas import date_range, Series time_ind = date_range(datetime(2013, 1, 1),…
joeb1415
  • 527
  • 2
  • 7
  • 12
5
votes
2 answers

MySQL COUNT() on groups returns wrong number of rows

I have this table called "values": value 12 13 5 56 3 56 79 98 58 74 52 2 8 32 4 I want to get the lowest value for each block of 5, so I tried this query: SET @a = -1; SELECT FLOOR((@a:=@a+1)/5) AS block, MIN(value) FROM values GROUP BY block It…
Truls
  • 53
  • 1
  • 5
5
votes
5 answers

T-SQL: Sorting by date, then grouping?

Let's say I have a database table that looks like this: ID name salary start_date city region ----------- ---------- ----------- ----------------------- ---------- ------ 1 Jason 40420…
David
  • 103
  • 2
  • 4
5
votes
2 answers

Multiple Aggregate functions with a group by clause

I have the following WorkflowID FK_UA DateApprobation ----------- -------------------- ----------------------- 1 3 NULL 2 1 NULL 3 1 NULL 4 …
user2440674
  • 55
  • 1
  • 4
5
votes
4 answers

Monthly totals month returning NULL, and only outputting running total

I am using the following to try and output my monthly totals in a format such as : January Quoted Total : £678 Parts Total : £432 Profit Total : £244 February Quoted Total : £650 Parts Total : £345 Profit Total : £123 etc.......... //…
Iain Simpson
  • 8,011
  • 13
  • 47
  • 66
5
votes
2 answers

MySQL Left Join Many to One Row

To simplify my problem: Let's say I have 3 tables. Rooms People Things -------- -------- -------- id| name id | name | fk_rooms id | name | fk_rooms ----------- …
NewInTheBusiness
  • 1,465
  • 1
  • 9
  • 14
5
votes
1 answer

What's the simplest way to include the COUNT of associations in a SQL query?

Say I have a [Products] table, and a [Products.record] can have many [Sales.records] associated with it. I want to have a single dataset that gives me number of sales a product has. Of course a product can have zero sales. The first version of my…
andy
  • 8,775
  • 13
  • 77
  • 122