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

NSFetchRequest sum, group by and sorting

In my app I'm trying to list the four most popular products. The product popularity is determined by the total quantity in orders. The query below is to illustrate what I'm trying to do: SELECT TOP 4 SUM(quantity) as sumQuantity, Product FROM …
Yvo
  • 18,681
  • 11
  • 71
  • 90
5
votes
3 answers

Database: exclude 'group by' group if field X in any row has value Y

I'm using MySQL for this. (I'm stripping out irrelevant joins to keep this a bit clearer) I'm trying to select companies' addresses, grouping by company_id, but exclude an entire user if they have any addresses in a specific country (with ID 242) So…
rca86
  • 79
  • 1
  • 4
5
votes
4 answers

SQL group by and count fixed column values

I'm facing a problem in a data importation script in SQL(MySQL) where I need to GROUP rows by type to COUNT how much rows there are from each type. So far, it isn't really a problem, because I know that I can do: SELECT data.type, …
Felypp Oliveira
  • 2,147
  • 1
  • 17
  • 17
5
votes
3 answers

Group array results in Alphabetic order PHP

I'm using below code to display Image & Name of webisites form database.

A

5
votes
5 answers

SQL: Getting the period start and end datetimes from data like this? Tricky little puzzle I'm struggling with

I have a table which lists every date within the year. Each row also contains a period number from 1 to 13 which loop around each year. It looks something like this: | Date | Period | | 2012-12-27 00:00:00 | 12 | | 2012-12-28…
user1636130
  • 1,615
  • 5
  • 29
  • 47
5
votes
6 answers

SQL limit for LEFT JOINed table

I have the following tables. Industry(id, name) Movie(id, name, industry_id) [Industry has many movies] Trailer(id, name, movie_id) [Movie has many trailers] I need to find 6 latest trailers for each Industry. Every movie does not need to have a…
Sanganabasu
  • 943
  • 7
  • 21
  • 39
5
votes
3 answers

MySQL : Make HAVING select also "null"

I have two tables : dilemme and like. The first one contains articles and the second one contains votes. The script SELECT randomly one article to show and the user can vote (it's like and dislike). I want to make a query that shows only the…
rachids
  • 350
  • 1
  • 3
  • 11
5
votes
2 answers

Finding duplicate value pairs in SQL

I have a database that stores first and last names with a user id. The table looks like this: uid value 1 Fred 1 Keller 2 Tim 2 LaChef 3 Adam 3 Adam Having a duplicate uid is fine, but I want to find all entries that have the same first…
Adam Esterle
  • 343
  • 2
  • 4
  • 13
5
votes
2 answers

Not a GROUP BY expression error

I'm relatively new to databases. I am using Oracle and I'm trying to implement this query to find the number of personal training sessions the member has had. The tables are; MEMBERS…
memyselfandmyiphone
  • 1,080
  • 4
  • 21
  • 43
5
votes
3 answers

Group rows by Year Band Interval

I have a table (tbl_people), in this table I have a datetime field I want to group and count the records in groups of 10 years... The result should be something like: | count    | year           | | 1000     | 1980-1989 | |…
IROEGBU
  • 948
  • 16
  • 33
5
votes
5 answers

Group table into 15 minute intervals

T-SQL, SQL Server 2008 and up Given a sample table of StatusSetDateTime | UserID | Status | StatusEndDateTime | StatusDuration(in seconds) ============================================================================ 2012-01-01 12:00:00 |…
Wjdavis5
  • 3,952
  • 7
  • 35
  • 63
5
votes
2 answers

mysql follow and retweet-like functionality

This is a bit challenging but fun question. Consider having these tables tweets tweet_id | retweet_of_id | user_id follow user_id | followed_user_id So we store each "retweet as a separate tweet" pointing to the original tweet's id…
tradecenter
  • 125
  • 5
5
votes
2 answers

Get consecutive values in a group identified by min and max value

I'm trying to get groups of consecutive values from a table. I've been trying to find an example about this but I haven't been able to use it in my case. This is a small part of a table I have. CardID CardSerial CardStateID…
5
votes
1 answer

Update multiple rows in a table based on result from subquery

I have the following query: SELECT stat.mcq_id, ROUND( stat.total_score / stat.num_taken, 2 ) AS avg_score FROM ( SELECT user_mcq.mcq_id, SUM( score ) AS total_score, COUNT( user_mcq.id ) AS num_taken …
Adam
  • 1,932
  • 2
  • 32
  • 57
5
votes
2 answers

MySQL GROUP BY optimization - InnoDB vs MyISAM?

Question Does anyone know why there is no GROUP BY optimization for MyISAM-based tables? (I'm using this version: 5.1.49-3) Test tables CREATE TABLE `_test2_innodb` ( `i` int(10) unsigned NOT NULL AUTO_INCREMENT, `n` smallint(5) unsigned NOT…