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

Group users by age for a range

I have some data which i need to make some statistics. I need to group the users by age. var byAge = displayResult.GroupBy(x => x.Age); Which i can do as above. However, this gives me ages like 19, 20, 21 etc. what I want is grouping age by 10…
DarthVader
  • 52,984
  • 76
  • 209
  • 300
5
votes
3 answers

How to group by two conditions in rails 3 and loop through them

Ok so I have a sale model that recent_sales = Sale.recent => [#, fulfilled_at: nil, skip_print: nil, convention_id: 6, refund_fee:…
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
5
votes
1 answer

Grouping records hour by hour or day by day and filling gaps with zero or null

I have written a query that counts records hour by hour: select TO_CHAR(copied_timestamp, 'YYYY-MM-DD HH24'),count(*) from req group by TO_CHAR(copied_timestamp, 'YYYY-MM-DD HH24'); the result is: 2012-02-22 13 2280 2012-02-22 15 …
Mehdi
  • 4,396
  • 4
  • 29
  • 30
5
votes
4 answers

Group user agents by browser

I have a table with each row containing data about a page load by a user. One of the fields is the user agent for that hit. How can I group the results by the browser? I can group by common user agents: SELECT useragent, COUNT(useragent) AS c FROM…
user1390490
  • 51
  • 1
  • 2
5
votes
1 answer

Order Players on the SUM of their association model

I have a database with 6500 players and each player has an average of 15 game results. Use case I want to generate a list of players, ordered by the sum of their prize money (a field in the results table). I prefer this to be in some sort of scope,…
Fred Fickleberry III
  • 2,439
  • 4
  • 34
  • 50
5
votes
2 answers

create nested objects in javascript like groupby in C#

IList Customers = flat.GroupBy(cust => new { cust.ReferenceNumber, cust.Name, cust.Address }) .Select(c => new Customer() { ReferenceNumber = c.Key.ReferenceNumber, …
Schotime
  • 15,707
  • 10
  • 46
  • 75
5
votes
4 answers

How to use Group By in Marklogic?

I want to use Group By in xquery. Can someone tell me please how to use Group By in Marklogic ?
Puneet Pant
  • 918
  • 12
  • 37
5
votes
1 answer

SQL query to retrieve the single line of data with desired values for the columns

I have a database table with the following kind of data data S_Acc_RowID BU_Customer_Segment PBU 1111-00 PSG SMB -1 1111-00 SMB -1 1111-00 EB Seg 1 1111-01 PSG SMB 1 1111-01 …
harry
  • 310
  • 1
  • 4
  • 17
5
votes
5 answers

How to use a group by with wildcards in Postgres

I'm having trouble with this code in Postgres. I'd like to get all fields in the user table, but only group by id. This works well on MySQL and SQLite, but after googling I've found out that this behaviour isn't part of the standard. SELECT u.*,…
Okal Otieno
  • 345
  • 4
  • 13
5
votes
2 answers

SQL Geography data type column in GROUP BY clause

I'm using SQL Server and creating a script which will get the workers geographical location out of my database. The script is below. SELECT w.display_name, w.geo_location FROM jobs j WITH(NOLOCK) INNER JOIN workers w WITH(NOLOCK) ON w.worker_id =…
Richard Nixon
  • 159
  • 1
  • 12
5
votes
2 answers

Group records by time

I have a table containing a datetime column and some misc other columns. The datetime column represents an event happening. It can either contains a time (event happened at that time) or NULL (event didn't happen) I now want to count the number of…
dtech
  • 13,741
  • 11
  • 48
  • 73
4
votes
3 answers

Grouping latitude and longitude pairs in sequence

I've a table in which every x seconds a position is being written to. The table consists of the following columns: lat | lon | time | ..... The ultimate goal is to make the track visible on a map with on the points where the device halted…
stUrb
  • 6,612
  • 8
  • 43
  • 71
4
votes
2 answers

MYSQL shows incorrect rows when using GROUP BY

I have two tables: article('id', 'ticket_id', 'incoming_time', 'to', 'from', 'message') ticket('id', 'queue_id') where tickets represent a thread of emails between support staff and customers, and articles are the individual messages that compose…
Han
  • 5,374
  • 5
  • 31
  • 31
4
votes
1 answer

Conditional Group By statement using LINQ

I have what seems to be a fairly simple requirement, but looking around I'm not able to get a simple answer for this. I have looked on MSDN forums, Exper Exchange and nothing substantial was given to me. I have the following LINQ code Dim…
Tomasz
  • 63
  • 1
  • 7
4
votes
3 answers

Avoid specifying every table field in GROUP BY clause when using Left Join

I have a simple SQL Server 2008 database with two tables like this: TableA: (PK)"ID" "Field" and TableB: (PK)"ID" (FK)"ID_TableA" "Field" I want to select all the fields in TableA and also how many corresponding rows in…
Claudiu Constantin
  • 2,138
  • 27
  • 38
1 2 3
99
100