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

Find duplicate and merge record into single datatable c#

I am able to find the duplicates out of DataTable rows. Like following: var groups = table.AsEnumerable() .GroupBy(r => new { c1 = r.Field("Version"), }); var tblDuplicates = groups …
Jay Nanavaty
  • 1,089
  • 1
  • 15
  • 29
5
votes
4 answers

Crystal Reports Need to Group by Derived Date Range

Long time listner, first time caller. I'm using Crystal Reports 2010. I have daily trade information that I need to group together if the volume doesn't change. Here's what the data looks like. Trade# BegDate EndDate Volume 1 1/1/2012…
5
votes
4 answers

SQL to select Unique list of user, along with other data depending upon timestamp

I have a table in SQL, The table is like this: +-------------+---------------+-------------------------+ | quotes_user | email_address | ________Sent_at________ | +-------------+---------------+-------------------------+ | user1 | email1 …
Sunny R Gupta
  • 5,026
  • 1
  • 31
  • 40
5
votes
2 answers

SQL query mixing aggregated results and single values

I have a table with transactions. Each transaction has a transaction ID, and accounting period (AP), and a posting value (PV), as well as other fields. Some of the IDs are duplicated, usually because the transaction was done in error. To give an…
5
votes
1 answer

MySQL GroupBy and Shows it horizontally

Supposed that I have table below: 1) tblScore ============================ Date VendorID Score ============================ 12/09/01 12001 A 12/09/01 12001 A 12/09/01 12002 B 12/09/02 12003 …
mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95
5
votes
3 answers

Mysql speed up max() group by

I have an easy query for grouping rows which takes 0.0045 sec. for 300.000 rows SELECT cid FROM table GROUP BY cid When I add MAX() to query it takes 0.65 sec to return. SELECT MAX(id) id, cid FROM table GROUP BY cid How can I speed up this query?…
kent ilyuk
  • 119
  • 1
  • 4
  • 9
5
votes
3 answers

LINQ Sum Nested collection with GroupBy and OrderByDescending

I have: public class Order { public string Customer; public List OrderLines; } public class OrderLine { public decimal Quantity; public decimal UnitPrice; } My goal is to search the list of customer order by descending…
Demons
  • 53
  • 6
5
votes
3 answers

Mysql - it´s possible use Group By semester?

I need to use a group by per semester in a query. Anyone know any way to do this? If I use: `HAVING Date Between '2012-01 'and '2012-06'` I believe it will work, but will not be the correct result for the query. Any help is appreciated
Bruno
  • 99
  • 1
  • 8
5
votes
4 answers

SQL - One record per date

Right now im having the following data: Name Time picture 2012-07-23 17:00:00 picture 2012-07-24 18:00:00 picture 2012-07-24 19:00:00 picture 2012-08-03 08:40:06 picture 2012-08-03 08:42:39 …
joxxe
  • 261
  • 1
  • 3
  • 13
5
votes
4 answers

SQL: finding differences between rows

I want to count how many times each user has rows within '5' of eachother. For example, Don - 501 and Don - 504 should be counted, while Don - 501 and Don - 1600 should not be counted. Start: Name value _________ ______________ Don …
Don P
  • 60,113
  • 114
  • 300
  • 432
5
votes
2 answers

Group items by total amount

Suppose I have this number list: List nu = new List(); nu.Add(2); nu.Add(1); nu.Add(3); nu.Add(5); nu.Add(2); nu.Add(1); nu.Add(1); nu.Add(3); Keeping the list items in the same order, is it possible to group the items in linq that are…
Chris
  • 470
  • 1
  • 10
  • 19
5
votes
3 answers

MySQL subquery on each record in group by

This is the more advanced sequel to an earlier question. Here's a link to an SQLFiddle to make things a bit clearer I need to return something like (for each location): Ground Floor --------------- Garage - Radiator Kitchen - Cooker -…
Ian
  • 251
  • 1
  • 5
  • 15
5
votes
4 answers

How to avoid GROUP BY in SQL view

I have a sql view. Example is below. The problem is that I want to use all of the fields but I do not want to group by every field. How can I circumvent that? I only need to group on fieldA, but not the others...actually grouping with the others…
dido
  • 3,347
  • 11
  • 34
  • 42
5
votes
4 answers

MySQL Within-group aggregates with no sub-queries - suggested test data updated

I have two tables in MySQL sales database: Orders table: CREATE TABLE salestest.`orders` ( `ID` int(11) unsigned NOT NULL auto_increment, `OrderDate` datetime NOT NULL, `CustomerID` int(11) unsigned NOT NULL, PRIMARY KEY (`ID`), UNIQUE…
5
votes
5 answers

Grouping while preserving ordering in Linq

I've got an IQueryable(Of Job) where Job has, amongst other things: Property CreatedOn as DateTime Property JobType as JobTypes Enum JobTypes JobType1 JobType2 JobType3 End Enum What I want to get out of it is a list, ordered by…
Basic
  • 26,321
  • 24
  • 115
  • 201
1 2 3
99
100