Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 28th, 2007, 03:35 PM
jon@jontillman.com
Guest
 
Posts: n/a
Default Select and limit results in one table?

Please Excuse the newbie question, but I have not been able to figure
out how to do the following on my own. Assume I have a table called
'reviews' and in that table there are three columns: reviewDateTime,
reviewID, and reviewItem. reviewItem is not unique, the other two are.

What I want to do in pseudo-code is write a select query that will:

for each unique reviewItem select 5 reviewID by reviewDateTime
ascending

Any ideas?

  #2  
Old February 28th, 2007, 03:55 PM
Allen Browne
Guest
 
Posts: n/a
Default Re: Select and limit results in one table?

Use a subquery to limit the query to the first 5 for the reviewItem based on
ascending reviewDateTime.

Something like this:

SELECT * FROM Table1
WHERE reviewItem IN
(SELECT TOP 5 reviewItem
FROM Table1 AS Dupe
WHERE Dupe.reviewItem = Table1.reviewItem
ORDER BY Dupe.reviewDateTime);

If subqueries are new, see:
How to Create and Use Subqueries
at:
http://support.microsoft.com/?id=209066

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

<jon@jontillman.comwrote in message
news:1172676016.019416.295710@v33g2000cwv.googlegr oups.com...
Quote:
Please Excuse the newbie question, but I have not been able to figure
out how to do the following on my own. Assume I have a table called
'reviews' and in that table there are three columns: reviewDateTime,
reviewID, and reviewItem. reviewItem is not unique, the other two are.
>
What I want to do in pseudo-code is write a select query that will:
>
for each unique reviewItem select 5 reviewID by reviewDateTime
ascending
>
Any ideas?
>
  #3  
Old February 28th, 2007, 05:05 PM
jon@jontillman.com
Guest
 
Posts: n/a
Default Re: Select and limit results in one table?

On Feb 28, 3:41 pm, "Allen Browne" <AllenBro...@SeeSig.Invalidwrote:
Quote:
Use a subquery to limit the query to the first 5 for the reviewItem based on
ascending reviewDateTime.
>
Something like this:
>
SELECT * FROM Table1
WHERE reviewItem IN
(SELECT TOP 5 reviewItem
FROM Table1 AS Dupe
WHERE Dupe.reviewItem = Table1.reviewItem
ORDER BY Dupe.reviewDateTime);
>
If subqueries are new, see:
How to Create and Use Subqueries
at:
http://support.microsoft.com/?id=209066
>
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users -http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
>
<j...@jontillman.comwrote in message
>
news:1172676016.019416.295710@v33g2000cwv.googlegr oups.com...
>
Quote:
Please Excuse the newbie question, but I have not been able to figure
out how to do the following on my own. Assume I have a table called
'reviews' and in that table there are three columns: reviewDateTime,
reviewID, and reviewItem. reviewItem is not unique, the other two are.
>
Quote:
What I want to do in pseudo-code is write a select query that will:
>
Quote:
for each unique reviewItem select 5 reviewID by reviewDateTime
ascending
>
Quote:
Any ideas?
Thanks, that did the trick!

  #4  
Old March 1st, 2007, 03:05 PM
Allen Browne
Guest
 
Posts: n/a
Default Re: Select and limit results in one table?

John, there's now a couple of new article on subqueries.

The first one explains the basics:
http://allenbrowne.com/subquery-01.html

The second one give trouble-shooting tips:
http://allenbrowne.com/subquery-02.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
<jon@jontillman.comwrote in message
news:1172681795.652324.117310@s48g2000cws.googlegr oups.com...
Quote:
On Feb 28, 3:41 pm, "Allen Browne" <AllenBro...@SeeSig.Invalidwrote:
Quote:
>Use a subquery to limit the query to the first 5 for the reviewItem based
>on
>ascending reviewDateTime.
>>
>Something like this:
>>
>SELECT * FROM Table1
>WHERE reviewItem IN
>(SELECT TOP 5 reviewItem
>FROM Table1 AS Dupe
>WHERE Dupe.reviewItem = Table1.reviewItem
>ORDER BY Dupe.reviewDateTime);
>>
>If subqueries are new, see:
> How to Create and Use Subqueries
>at:
> http://support.microsoft.com/?id=209066
>>
>--
>Allen Browne - Microsoft MVP. Perth, Western Australia
>Tips for Access users -http://allenbrowne.com/tips.html
>Reply to group, rather than allenbrowne at mvps dot org.
>>
><j...@jontillman.comwrote in message
>>
>news:1172676016.019416.295710@v33g2000cwv.googleg roups.com...
>>
Quote:
Please Excuse the newbie question, but I have not been able to figure
out how to do the following on my own. Assume I have a table called
'reviews' and in that table there are three columns: reviewDateTime,
reviewID, and reviewItem. reviewItem is not unique, the other two are.
>>
Quote:
What I want to do in pseudo-code is write a select query that will:
>>
Quote:
for each unique reviewItem select 5 reviewID by reviewDateTime
ascending
>>
Quote:
Any ideas?
>
Thanks, that did the trick!
>
 

Bookmarks

Thread Tools

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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles