Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Selecting 1 most recent item from 3 categories
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Adam  
View profile   Translate to Translated (View Original)
 More options 20 July 2007, 17:25
Newsgroups: comp.databases.mysql
From: Adam <sty...@gmail.com>
Date: Fri, 20 Jul 2007 09:25:10 -0700
Local: Fri 20 July 2007 17:25
Subject: Selecting 1 most recent item from 3 categories
Hi, I have a table from which I want to select the most recent entry
from each category. The table looks like this:

+----------+-------------+-------------------+-------------------+
|    id     |    item    |    category    |    add_date   |

There are 3 different categories, just entered into the table as
varchar(30). "add_date" is datetime.

Any help would be much appreciated, thanks.

Adam


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rik  
View profile   Translate to Translated (View Original)
 More options 20 July 2007, 18:06
Newsgroups: comp.databases.mysql
From: Rik <luiheidsgoe...@hotmail.com>
Date: Fri, 20 Jul 2007 19:06:55 +0200
Local: Fri 20 July 2007 18:06
Subject: Re: Selecting 1 most recent item from 3 categories

On Fri, 20 Jul 2007 18:25:10 +0200, Adam <sty...@gmail.com> wrote:
> Hi, I have a table from which I want to select the most recent entry
> from each category. The table looks like this:

> +----------+-------------+-------------------+-------------------+
> |    id     |    item    |    category    |    add_date   |

> There are 3 different categories, just entered into the table as
> varchar(30). "add_date" is datetime.

Do you want the ID or just the list?

Without ID is the simplest:

SELECT category, MAX(add_date) FROM table GROUP BY category

With ID it's quite different:

SELECT
        x.id
        x.category
        x.add_date
 FROM table x
JOIN (SELECT category, MAX(add_date) FROM table GROUP BY category) y
ON y.category = x. category
AND y.add_date = x. add_date

(anyone know a shorter one, seems a bit cludgy?)
--
Rik Wasmus


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam  
View profile   Translate to Translated (View Original)
 More options 20 July 2007, 18:24
Newsgroups: comp.databases.mysql
From: Adam <sty...@gmail.com>
Date: Fri, 20 Jul 2007 10:24:47 -0700
Local: Fri 20 July 2007 18:24
Subject: Re: Selecting 1 most recent item from 3 categories
On Jul 20, 1:06 pm, Rik <luiheidsgoe...@hotmail.com> wrote:

Sorry, to clarify I actually want to pull the item names (defined by
"item"). I will try to use your second example, just substituting
where appropriate.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ZeldorBlat  
View profile   Translate to Translated (View Original)
 More options 20 July 2007, 20:04
Newsgroups: comp.databases.mysql
From: ZeldorBlat <zeldorb...@gmail.com>
Date: Fri, 20 Jul 2007 19:04:27 -0000
Local: Fri 20 July 2007 20:04
Subject: Re: Selecting 1 most recent item from 3 categories
On Jul 20, 1:06 pm, Rik <luiheidsgoe...@hotmail.com> wrote:

That probably is the best way.  You could probably do something like
this, but the performance probably won't be as good:

SELECT x.id,
       x.category,
       x.add_date
FROM table x
WHERE x.add_date = (SELECT MAX(y.add_date) FROM table y WHERE
y.category = x.category)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lark  
View profile   Translate to Translated (View Original)
 More options 20 July 2007, 20:33
Newsgroups: comp.databases.mysql
From: lark <ham...@sbcglobal.net>
Date: Fri, 20 Jul 2007 19:33:19 GMT
Local: Fri 20 July 2007 20:33
Subject: Re: Selecting 1 most recent item from 3 categories

couldn't you also use his first one like this:
select item, category, max(add_date) from table group by category limit 3;

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rik  
View profile   Translate to Translated (View Original)
 More options 20 July 2007, 20:56
Newsgroups: comp.databases.mysql
From: Rik <luiheidsgoe...@hotmail.com>
Date: Fri, 20 Jul 2007 21:56:31 +0200
Local: Fri 20 July 2007 20:56
Subject: Re: Selecting 1 most recent item from 3 categories

On Fri, 20 Jul 2007 21:40:26 +0200, Adam <sty...@gmail.com> wrote:
> To summarize, I'm wanting to choose the most recent item (and I'll be
> using the "item" column for my purposes) from each of the 3
> categories. So three items, one from each category.

Ahum, one?
Can you be absoutely 100% sure there is just 1 item with the maximum?
--
Rik Wasmus

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rik  
View profile   Translate to Translated (View Original)
 More options 20 July 2007, 20:59
Newsgroups: comp.databases.mysql
From: Rik <luiheidsgoe...@hotmail.com>
Date: Fri, 20 Jul 2007 21:59:19 +0200
Local: Fri 20 July 2007 20:59
Subject: Re: Selecting 1 most recent item from 3 categories

I think you're missing the point. He states he wants the most recent entry  
 from _each_ category. There just _happen_ to be three. But if you only  
want three, by all means, use a limit.
--
Rik Wasmus

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam  
View profile   Translate to Translated (View Original)
 More options 20 July 2007, 21:04
Newsgroups: comp.databases.mysql
From: Adam <sty...@gmail.com>
Date: Fri, 20 Jul 2007 13:04:34 -0700
Local: Fri 20 July 2007 21:04
Subject: Re: Selecting 1 most recent item from 3 categories
On Jul 20, 3:59 pm, Rik <luiheidsgoe...@hotmail.com> wrote:

There will definitely only be three, as it's a very small and simple
utility for someone to add items to be donated. There are 3 donation
categories, so I'm trying to show the 1 most recently requested
donation from each category.

At any rate, I think I, and others, have supplied the right method but
there must be something wrong with my data, because it seems to just
be picking one random one from each category. It's always the same 3,
but they're not the most recent.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Lautman  
View profile   Translate to Translated (View Original)
 More options 20 July 2007, 21:27
Newsgroups: comp.databases.mysql
From: "Paul Lautman" <paul.laut...@btinternet.com>
Date: Fri, 20 Jul 2007 21:27:50 +0100
Local: Fri 20 July 2007 21:27
Subject: Re: Selecting 1 most recent item from 3 categories

"Adam" <sty...@gmail.com> wrote in message

news:1184948710.062291.55830@w3g2000hsg.googlegroups.com...

> Hi, I have a table from which I want to select the most recent entry
> from each category. The table looks like this:

> +----------+-------------+-------------------+-------------------+
> |    id     |    item    |    category    |    add_date   |

> There are 3 different categories, just entered into the table as
> varchar(30). "add_date" is datetime.

> Any help would be much appreciated, thanks.

> Adam

Do a search on this forum for "Strawberry Query", that'll do exactly what
you want

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam  
View profile   Translate to Translated (View Original)
 More options 21 July 2007, 02:01
Newsgroups: comp.databases.mysql
From: Adam <sty...@gmail.com>
Date: Fri, 20 Jul 2007 18:01:18 -0700
Local: Sat 21 July 2007 02:01
Subject: Re: Selecting 1 most recent item from 3 categories
On Jul 20, 4:27 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:

Hey, thanks - I got it working with your "Strawberry Query" but I must
confess I have no idea how it works, or if it will always work. At any
rate, thanks very much.

Why do you call it the Strawberry Query?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Lautman  
View profile   Translate to Translated (View Original)
 More options 21 July 2007, 19:16
Newsgroups: comp.databases.mysql
From: "Paul Lautman" <paul.laut...@btinternet.com>
Date: Sat, 21 Jul 2007 19:16:56 +0100
Local: Sat 21 July 2007 19:16
Subject: Re: Selecting 1 most recent item from 3 categories

Somewhere in this forum, I remember posting a full explanation of how this
query works, but I'll be blowed if I can find it now!
But the following reference to the manual contains a very succinct
explanation of how it works:
http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-r...
(See: "The LEFT JOIN works on the basis that ...")

As to why I call it the Strawberry Query: The question that you asked comes
up just soooooo many times on this forum. It is so common that I am totally
amazed that Rik, Lark and ZeldorBlat didn't manage to point you at the
answer. Anyway, a while back when this sort of question was asked about 3
times in the same week, a poster by the handle of Strawberry, constantly
posted this query format to answer the question.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
strawberry  
View profile   Translate to Translated (View Original)
 More options 22 July 2007, 11:18
Newsgroups: comp.databases.mysql
From: strawberry <zac.ca...@gmail.com>
Date: Sun, 22 Jul 2007 10:18:14 -0000
Local: Sun 22 July 2007 11:18
Subject: Re: Selecting 1 most recent item from 3 categories
>I am totally amazed that Rik, Lark and ZeldorBlat didn't manage to point you at the

answer.

As am I.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rik  
View profile   Translate to Translated (View Original)
 More options 22 July 2007, 12:07
Newsgroups: comp.databases.mysql
From: Rik <luiheidsgoe...@hotmail.com>
Date: Sun, 22 Jul 2007 13:07:13 +0200
Local: Sun 22 July 2007 12:07
Subject: Re: Selecting 1 most recent item from 3 categories

On Sun, 22 Jul 2007 12:18:14 +0200, strawberry <zac.ca...@gmail.com> wrote:
>> I am totally amazed that Rik, Lark and ZeldorBlat didn't manage to  
>> point you at the
> answer.

> As am I.

Still not conditioned enough :-). Then again, I've never had the problem  
my database was the bottleneck, so there's probably a lot out there that  
still is easily optimisable. The moment I run into trouble I'll think  
'Strawberry', I promise :).

--
Rik Wasmus


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam  
View profile   Translate to Translated (View Original)
 More options 23 July 2007, 16:57
Newsgroups: comp.databases.mysql
From: Adam <sty...@gmail.com>
Date: Mon, 23 Jul 2007 08:57:07 -0700
Local: Mon 23 July 2007 16:57
Subject: Re: Selecting 1 most recent item from 3 categories
On Jul 22, 7:07 am, Rik <luiheidsgoe...@hotmail.com> wrote:

> On Sun, 22 Jul 2007 12:18:14 +0200, strawberry <zac.ca...@gmail.com> wrote:
> >> I am totally amazed that Rik, Lark and ZeldorBlat didn't manage to
> >> point you at the
> > answer.

> > As am I.

> Still not conditioned enough :-). Then again, I've never had the problem
> my database was the bottleneck, so there's probably a lot out there that
> still is easily optimisable. The moment I run into trouble I'll think
> 'Strawberry', I promise :).

> --
> Rik Wasmus

Well, thanks all for your help.

Adam


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google