473,666 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select and limit results in one table?

jon
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?

Feb 28 '07 #1
3 5688
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.reviewIt em
ORDER BY Dupe.reviewDate Time);

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.

<jo*@jontillman .comwrote in message
news:11******** **************@ v33g2000cwv.goo glegroups.com.. .
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?
Feb 28 '07 #2
jon
On Feb 28, 3:41 pm, "Allen Browne" <AllenBro...@Se eSig.Invalidwro te:
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.reviewIt em
ORDER BY Dupe.reviewDate Time);

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...@jontillma n.comwrote in message

news:11******** **************@ v33g2000cwv.goo glegroups.com.. .
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?
Thanks, that did the trick!

Feb 28 '07 #3
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.
<jo*@jontillman .comwrote in message
news:11******** **************@ s48g2000cws.goo glegroups.com.. .
On Feb 28, 3:41 pm, "Allen Browne" <AllenBro...@Se eSig.Invalidwro te:
>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.reviewIt em
ORDER BY Dupe.reviewDate Time);

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...@jontillm an.comwrote in message

news:11******* *************** @v33g2000cwv.go oglegroups.com. ..
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?

Thanks, that did the trick!
Mar 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2285
by: jagg | last post by:
Hi, i save junior football results in a mysql table (FIELDS id jugend autor sptag ergebnis spdatum zeit ) With the following lines I generate a site whichs gives me ALL results Code: <table border="0" cellspacing="1" cellpadding="2" class="content" width="100%">
3
2011
by: Bob Bedford | last post by:
I've a site where companies add their article. I'de like to provide a "lasts articles" table. By this, I'll show last articles inserted. But I won't always the same articles at any refresh. Question 1: how to get a "random" selection from the database, giving more priority to the last inserted (the ones with higher articleID) Question 2: I'd like to provide one article by client. I won't show 3 articles from the same client only because...
3
2056
by: Sven Reifegerste | last post by:
Hi, i have a table with INT columns id,key,b1,b2,c1,c2, having 1.500.000 rows. 'key' and 'id' are indexed (Kardinality 385381) and id (Kardinality 1541525). Performing a SELECT * FROM table WHERE key IN (10,11,12) OR key BETWEEN 20000 AND 28000 AND b1<4500000 AND b2>3954545 AND c1<4543554 AND c2>4400000
3
2392
by: Wim Roffil | last post by:
Hi, When I do a select with a limit I get always the same records. Usually this is the desidered effect. But now I want to do a random select. So every time I do a select I should get a different set of results. What is the best way to achieve this effect?
11
6070
by: wackyphill | last post by:
I want to create a stored procedure that returns a list of records from a table. But depending on a userID value given only certain records will be returned that they have access to. I think this might be hard to do in a single SELECT statement because the user might also belong to a group that might have permission, etc. Can you do something like this pseudo code in a T-SQL procedure? DECLARE cur CURSOR FOR SELECT * FROM myTable
3
6450
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
7
19304
by: Milos Prudek | last post by:
I need to insert a value = max(value)+1, where max is a select limited by a 'where' clause. Like this: INSERT INTO table (idthread, idsection,txt) VALUES ( (SELECT max(idthread)+1 FROM table WHERE idsection = 'CZE'), 'CZE', 'sample text') ); This works fine, except when the result of SELECT is empty - which is
0
2321
by: Steve Crawford | last post by:
I'm still mulling the best way to handle this situation. I have a table that describes work to be processed. This table includes a description of the work as well as priority and scheduling information (certain records can only be handled by certain client processes or at particular times of the day or week). I have several hundred client processes to handle the work, most, but not all, of which can handle any of the items in the...
0
1261
by: SC | last post by:
I'm developing a site that may eventually have a very large database of users (hopefully, but who knows). Often there will be queries with multiple joins and sometimes may include a few fulltext searches. Each page of results will have a limited number of results displayed (obviously). As I understand it, MySQL caches only exact queries, so doing a LIMIT (x,y) on each query to get results for the specific page will cause MySQL to run the...
0
8449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.