Maximum-per-group problem for this table : Group « Select Clause « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Select Clause » Group 
Maximum-per-group problem for this table
      
mysql>
mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  rec_id          INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  name            VARCHAR(20NOT NULL,
    ->  trav_date       DATE NOT NULL,
    ->  miles           INT NOT NULL,
    ->  PRIMARY KEY (rec_id)
    -> );
Query OK, rows affected (0.00 sec)

mysql>
mysql> INSERT INTO mytable (name,trav_date,miles)
    ->  VALUES
    ->          ('Ben','2010-11-30',152),
    ->          ('Suzi','2010-11-29',391),
    ->          ('Henry','2010-11-29',300),
    ->          ('Henry','2010-11-27',96),
    ->          ('Ben','2010-11-29',131),
    ->          ('Henry','2010-11-26',115),
    ->          ('Suzi','2010-12-02',502),
    ->          ('Henry','2010-12-01',197),
    ->          ('Ben','2010-12-02',79),
    ->          ('Henry','2010-11-30',203)
    -> ;
Query OK, 10 rows affected (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> CREATE TABLE tmp
    -> SELECT name, MAX(trav_dateAS trav_date
    -> FROM mytable GROUP BY name;
Query OK, rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT mytable.name, mytable.trav_date, mytable.miles
    -> FROM mytable, tmp
    -> WHERE mytable.name = tmp.name
    -> AND mytable.trav_date = tmp.trav_date
    -> ORDER BY mytable.name;
+-------+------------+-------+
| name  | trav_date  | miles |
+-------+------------+-------+
| Ben   | 2010-12-02 |    79 |
| Henry | 2010-12-01 |   197 |
| Suzi  | 2010-12-02 |   502 |
+-------+------------+-------+
rows in set (0.00 sec)

mysql>
mysql> drop table mytable;
Query OK, rows affected (0.00 sec)

mysql> drop table tmp;
Query OK, rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
  
Related examples in the same category
1.Use GROUP BY clause to list only the unique data
2.Another GROUP BY
3.Use GROUP BY
4.GROUP BY with order and HAVING
5.Use GROUP BY 2
6.Use GROUP BY and ORDER BY together
7.Get GROUP BY for COUNT
8.Simple GROUP BY
9.GROUP and sort the records
10.GROUP value and count
11.Grouping Data: Filtering Group Data
12.Grouping Data: 03 Using the HAVING Clause 1
13.Grouping Data: Using the HAVING Clause 1
14.Using math function in HAVING
15.GROUP and HAVING with sub query
16.Group by and order by for linked tables
17.Grouping by Expression Results
18.Give the expression an alias in the output column list and refer to the alias in the GROUP BY clause
19.Write the GROUP BY clause to refer to the output column position:
20.Group by multiple expressions if you like.
21.Group mail table records into categories of 100,000 bytes
22.Group by DAYOFWEEK( )
23.Working with Per-Group and Overall Summary Values Simultaneously
24.Finding Rows Containing Per-Group Minimum or Maximum Values
25.Another way to group statements is to turn off auto-commit mode explicitly.
26.To use a GROUP BY clause effectively, you should also include a select list element that contains a function t
27.Specifies two columns in the GROUP BY clause
28.Working with Grouped Data
29.Group by calculated value
30.Group value in subquery
31.Group by two columns
32.Group by then order by vs Group by only
33.GROUP BY for Several Columns
34.GROUP BY returns a final sum for the first column and supplementary partial sums for the second column.
35.Dividing a Summary into Subgroups
36.Use the name column to place the rows in groups, but the summary functions operate on the miles values:
37.Parentheses may be used to group alternations.
38.To be more specific and find out how many messages each sender sent from each host, use two grouping columns.
39.Categorize groups on a logarithmic scale.
40.Missing and non-missing counts can be determined for subgroups as well.
41.get the number of orders per customer
42.A less fine-grained summary can be obtained by using only the month values
43.Find out how many books you have per author, use this query
44.Ascertain the most common initial letter for state names like this:
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.