dbTalk Databases Forums  

Sorting and then grouping a table

comp.databases.mysql comp.databases.mysql


Discuss Sorting and then grouping a table in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
jhaagmans
 
Posts: n/a

Default Sorting and then grouping a table - 11-09-2010 , 08:05 AM






Hi,

I have a table that contains orders, but I only want to select the
latest orders from every client. Every order has a client_id, so I
tried this:

SELECT * FROM `orders` ORDER BY date DESC GROUP BY client_id;

However, that doesn't work. This does:

SELECT * FROM `orders` GROUP BY client_id ORDER BY date DESC;

But it doesn't return the latest orders from every client, it selects
orders by unique clients and then sorts it by date.

How can I get the result I want?

Thank you!
Jaap Haagmans

Reply With Quote
  #2  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Sorting and then grouping a table - 11-09-2010 , 08:30 AM






On 11/9/2010 9:05 AM, jhaagmans wrote:
Quote:
Hi,

I have a table that contains orders, but I only want to select the
latest orders from every client. Every order has a client_id, so I
tried this:

SELECT * FROM `orders` ORDER BY date DESC GROUP BY client_id;

However, that doesn't work. This does:

SELECT * FROM `orders` GROUP BY client_id ORDER BY date DESC;

But it doesn't return the latest orders from every client, it selects
orders by unique clients and then sorts it by date.

How can I get the result I want?

Thank you!
Jaap Haagmans
An often asked question. Google this group for "strawberry query".

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
  #3  
Old   
Matthew Leonhardt
 
Posts: n/a

Default Re: Sorting and then grouping a table - 11-09-2010 , 08:52 AM



"jhaagmans" <jaap.haagmans (AT) gmail (DOT) com> wrote

Quote:
Hi,

I have a table that contains orders, but I only want to select the
latest orders from every client. Every order has a client_id, so I
tried this:

SELECT * FROM `orders` ORDER BY date DESC GROUP BY client_id;

However, that doesn't work. This does:

SELECT * FROM `orders` GROUP BY client_id ORDER BY date DESC;

But it doesn't return the latest orders from every client, it selects
orders by unique clients and then sorts it by date.

How can I get the result I want?

SELECT client_id, MAX(`date`) FROM `orders` GROUP BY client_id ORDER BY
`date` DESC;

This is the danger in selecing fields that are not part of the grouping and
have no aggregate function...

Reply With Quote
  #4  
Old   
jhaagmans
 
Posts: n/a

Default Re: Sorting and then grouping a table - 11-11-2010 , 11:56 AM



Thank you both. The term "strawberry query" is new to me, but it has
helped me understand this. Great stuff!

Jaap

Reply With Quote
  #5  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Sorting and then grouping a table - 11-11-2010 , 02:58 PM



On 11/11/2010 12:56 PM, jhaagmans wrote:
Quote:
Thank you both. The term "strawberry query" is new to me, but it has
helped me understand this. Great stuff!

Jaap
That's because the term is specific to this newsgroup - from the famous
poster "strawberry" who has been very eloquent on the subject.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
  #6  
Old   
Peter H. Coffin
 
Posts: n/a

Default Re: Sorting and then grouping a table - 11-11-2010 , 09:55 PM



On Thu, 11 Nov 2010 15:58:29 -0500, Jerry Stuckle wrote:
Quote:
On 11/11/2010 12:56 PM, jhaagmans wrote:
Thank you both. The term "strawberry query" is new to me, but it has
helped me understand this. Great stuff!

Jaap

That's because the term is specific to this newsgroup - from the famous
poster "strawberry" who has been very eloquent on the subject.
The method of strawberry reproduction via a runner to create a clone
plant at another location is a happy coincidence. The two or more
strawberry plants thusly connected are a very apt visualization of how
one might represent the behavior of the query....

--
A way of life that is odd or even erratic but interferes with no rights
or interests of others is not to be condemned because it is different.
-- Chief Justice Warren E. Burger

Reply With Quote
  #7  
Old   
strawberry
 
Posts: n/a

Default Re: Sorting and then grouping a table - 11-12-2010 , 11:55 AM



On Nov 12, 3:55*am, "Peter H. Coffin" <hell... (AT) ninehells (DOT) com> wrote:
Quote:
On Thu, 11 Nov 2010 15:58:29 -0500, Jerry Stuckle wrote:
On 11/11/2010 12:56 PM, jhaagmans wrote:
Thank you both. The term "strawberry query" is new to me, but it has
helped me understand this. Great stuff!

Jaap

That's because the term is specific to this newsgroup - from the famous
poster "strawberry" who has been very eloquent on the subject.

The method of strawberry reproduction via a runner to create a clone
plant at another location is a happy coincidence. The two or more
strawberry plants thusly connected are a very apt visualization of how
one might represent the behavior of the query....

--
A way of life that is odd or even erratic but interferes with no rights
or interests of others is not to be condemned because it is different.
* * * * * * * * * * * * * * -- Chief Justice Warren E. Burger
Just for clairification, that is not how I reproduce. ;-)

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.