All In One Script



PHP,HTLM,CSS,Jquery,AJAX,Javascript and etc doubts and sample codes

  • Home
  • Javascript
  • PHP
  • CSS
  • SQL/MYSQL

Select first row in each GROUP BY group?

by Blogger 1:11:00 AM greatest-n-per-group group-by postgresq SQL Sqllite

Select first row in each GROUP BY group?

As the title suggests, I'd like to select the first row of each set of rows grouped with a GROUP BY.
Specifically, if I've got a purchases table that looks like this:
SELECT * FROM purchases;
id | customer | total
---+----------+------
 1 | Joe      | 5
 2 | Sally    | 3
 3 | Joe      | 2
 4 | Sally    | 1
I'd like to query for the id of the largest purchase (total) made by each customer. Something like this:
SELECT FIRST(id), customer, FIRST(total)
FROM  purchases
GROUP BY customer
ORDER BY total DESC;
FIRST(id) | customer | FIRST(total)
----------+----------+-------------
        1 | Joe      | 5
        2 | Sally    | 3


Answer :

On Oracle 8i+, SQL Server 2005+, PostgreSQL 8.4+, DB2, Firebird 3.0+, Teradata, Sybase, Vertica:

WITH summary AS (
    SELECT p.id, 
           p.customer, 
           p.total, 
           ROW_NUMBER() OVER(PARTITION BY p.customer 
                                 ORDER BY p.total DESC) AS rk
      FROM PURCHASES p)
SELECT s.*
  FROM summary s
 WHERE s.rk = 1

Supported by any database:

But you need to add logic to break ties:
  SELECT MIN(x.id),  -- change to MAX if you want the highest
         x.customer, 
         x.total
    FROM PURCHASES x
    JOIN (SELECT p.customer,
                 MAX(total) AS max_total
            FROM PURCHASES p
        GROUP BY p.customer) y ON y.customer = x.customer
                              AND y.max_total = x.total
GROUP BY x.customer, x.total


http://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group
READ MORE
SHARE :

Search This Blog

Followers

  • Popular
  • Recent
  • Comments
    How to efficiently iterate over each Entry in a Map?
    How to avoid Java code in JSP files?
    Serving XHTML and self-closing tags
    Why does this code using random strings print “hello world”?
    Get first key in a (possibly) associative array?
    Does finally always execute in Java?
    In Java, difference between default, public, protected, and private
    Solved : curl_init() function not working in Ubuntu
    Length of a JavaScript object
    How can I make the cursor a hand when a user hovers over a list item?

Instagram

About

Popular Posts

  • How to efficiently iterate over each Entry in a Map?
    How to efficiently iterate over each Entry in a Map? If I have an object implementing the  Map  interface in Java and I wish to iterate...
  • How to avoid Java code in JSP files?
    How to avoid Java code in JSP files? I'm new to Java EE and I know that something like the following three lines <%= x + 1 %...
  • Serving XHTML and self-closing tags
    Serving XHTML and self-closing tags I am trying to follow the xhtml 1.0 strict standard as I am creating my website. Right now, validat...
  • Why does this code using random strings print “hello world”?
    Why does this code using random strings print “hello world”? The following print statement would print "hello world". Could a...
  • Get first key in a (possibly) associative array?
    Get first key in a (possibly) associative array? What's the best way to determine the first key in a possibly associative array? My...
  • Does finally always execute in Java?
    Does finally always execute in Java? I have a try/catch block with  return s inside it. Will the finally block be called? For example...
  • In Java, difference between default, public, protected, and private
    In Java, difference between default, public, protected, and private In Java , are there clear rules on when to use each of access modifi...
  • Solved : curl_init() function not working in Ubuntu
    Solved : curl_init() function not working in Ubuntu  Here solved the error  Fatal error: Call to undefined function curl_init() ...
  • Length of a JavaScript object
    Length of a JavaScript object If I have a JavaScript object, say var myObject = new Object (); myObject [ "firstname" ] ...
  • How can I make the cursor a hand when a user hovers over a list item?
    How can I make the cursor a hand when a user hovers over a list item? I've got a list, and I have a click handler for its items: ...

statcounter



statcounter



Template Created By ThemeXpose & Blogger Templates