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
2 answers

complex SQL query to order by joined table data

Here is the sample data, boiled down as much as possible: Table: items (id / manufacturer) 1 / Microsoft 2 / Microsoft 3 / Microsoft 4 / ACME 5 / Microsoft Table: order_rows (item_id / date) 1 / 2012-12-01 1 / 2013-01-01 2 / 2013-01-02 2 /…
5
votes
2 answers

How to group by column having spelling mistakes

While working with some legacy data, I want to group the data on a column ignoring spelling mistakes. I think SOUNDEX() could do the job to achieve the desired result. Here is what I tried: SELECT soundex(AREA) FROM MASTER GROUP BY…
Khadim Ali
  • 2,548
  • 3
  • 33
  • 61
5
votes
1 answer

Select and group rows in summary by hour

I have a table containing view/click records. The time is stored in a unix timestamp and I need to be able to pull out all of them within the specific month/day (based off of timestamps), but more importantly and the part I don't know how to do is…
Ben
  • 60,438
  • 111
  • 314
  • 488
5
votes
1 answer

Mysql GROUP BY and COUNT for multiple WHERE clauses

Simplified Table structure: CREATE TABLE IF NOT EXISTS `hpa` ( `id` bigint(15) NOT NULL auto_increment, `core` varchar(50) NOT NULL, `hostname` varchar(50) NOT NULL, `status` varchar(255) NOT NULL, `entered_date` int(11) NOT NULL, …
Daren Schwenke
  • 5,428
  • 3
  • 29
  • 34
5
votes
1 answer

Counting Instances of Unique Value in Field

Suppose you have a table in SQL: Prices ------ 13.99 14.00 52.00 52.00 52.00 13.99 How would you count the amount of times a DIFFERENT field has been entered in? Therefore an example of such a count would output: 13.99 - 2 times. 14.00 - 1…
Federer
  • 33,677
  • 39
  • 93
  • 121
5
votes
3 answers

What is the best way to select the first two records of each group by a "SELECT" command?

For instance I have the following table: id group data 1 1 aaa 2 1 aaa 3 2 aaa 4 2 aaa 5 2 aaa 6 3 aaa 7 3 aaa 8 3 aaa What is the best way to select the first two records of each group by a "SELECT" command? If there is no good way to do so, what…
Abby Chau Yu Hoi
  • 1,378
  • 3
  • 15
  • 37
5
votes
5 answers

Mysql merge rows

I have this table: ip country 1 A 2 A 3 B 4 B 4 B I am trying to write a query, which will return something like: A 1,2 B 3,4 E.g. SELECT * FROM table GROUP BY country returns: A 1 B 3 But it's not the desired result. Ι can…
Chris P
  • 2,059
  • 4
  • 34
  • 68
5
votes
2 answers

MS Access Rounding Precision With Group By

Why doesn't the average of the score of an employee of each month, when summed, equal the average of the employees score (ever)? Average SELECT Avg(r.score) AS rawScore FROM (ET INNER JOIN Employee AS e ON ET.employeeId = e.id) INNER JOIN (Employee…
CRice
  • 12,279
  • 7
  • 57
  • 84
5
votes
2 answers

Grouping items by a key?

I feel like Python ought to have a built-in to do this. Take a list of items and turn them into a dictionary mapping keys to a list of items with that key in common. It's easy enough to do: # using defaultdict lookup =…
FogleBird
  • 74,300
  • 25
  • 125
  • 131
5
votes
3 answers

Select, group and sum results from database

I have a database with some fields I'd like to sum. But that's not the big problem, I want to group those fields by the month they were created. ActiveRecord automaticaly created a field named "created_at". So my question; how can I group the result…
ThoKra
  • 2,959
  • 2
  • 27
  • 38
5
votes
2 answers

Linq GroupBy. Return top one item of a subset of data

I have a table of flight price data and I want to return the top 1 cheapest flight to each destination destination. The table has the following basic fields: FlightInfoID AirportFrom AirportTo Price I tried the following but it did not return the…
Damo
  • 361
  • 4
  • 16
5
votes
1 answer

F# - GroupBy and apply function to each property inside second tuple item

I have a an F# list of classes for which I am using properties to access data (i'm using a library developed in C#). I would like to group by one property then apply a separate function to each property in the second item of the resulting…
Richard Todd
  • 2,406
  • 5
  • 32
  • 40
5
votes
1 answer

python return lists of continuous integers from list

I have a list of integers, and I want to generate a list containing a list of all the continuous integers. #I have: full_list = [0,1,2,3,10,11,12,59] #I want: continuous_integers = [[0,1,2,3], [10,11,12], [59]] I have the following which works, but…
ded
  • 420
  • 2
  • 13
5
votes
2 answers

SQL SUM GROUP BY two tables

I'm having difficulty writing an SQL query that will correctly group account_no together and subtracting an amount. Firstly I wrote this query which updates everything fine except ACCOUNT_NO A-102 should end up as 4500 not as two different correct…
Sjrsmile
  • 253
  • 1
  • 5
  • 20
5
votes
2 answers

LINQ MySQL Group by Year, Month, Day select Year, Month, Day, Count

var res = from r in db.myTable group r by new { Year = r.DateVal.Year, Month = r.DateVal.Month, Day = r.DateVal.Day } into g …
Faizan Kazi
  • 545
  • 1
  • 10
  • 18