A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Retrieving variable number of rows



 
 
Thread Tools Display Modes
  #1  
Old July 14th, 2008, 06:29 PM posted to microsoft.public.access.queries
Amy Blankenship
external usenet poster
 
Posts: 539
Default Retrieving variable number of rows

Hi, all;

I need to be able to use one query to retrieve either the most recent result
or all results, based on a parameter to the query. I don't seem to be able
to find a syntax that will allow me to do something like:

SELECT TOP [Enter how many things] things FROM MyTable ORDER BY thingNum;

Has anyone solved this before?

TIA;

Amy


  #2  
Old July 14th, 2008, 06:41 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default Retrieving variable number of rows

You can't use a parameter to set the Top predicates value.

You can build the query's sql on the fly using VBA or you can use a ranking query

Select Top N records where N is variable ( a parameter)

One method that can be used is to create a ranking query to assign a number to
the records and then use the ranking to return N records.

'Probably not updatable
SELECT MyTable.*
FROM MyTable INNER JOIN
(
SELECT A.MyField, Count(B.MyField) as TheCount
FROM MyTable as A LEFT JOIN MyTable As B
ON A.MyField B.MyField
GROUP BY A.MyField
) as Ranking
ON MyTable.MyField = Ranking.MyField
WHERE Ranking.TheCount [Top How Many Number]

'Probably updatable
SELECT MyTable.*
FROM MyTable
WHERE MyField in
(SELECT A.MyField
FROM MyTable as A LEFT JOIN MyTable As B
ON A.MyField B.MyField
GROUP BY A.MyField
HAVING Count(B.MyField) [Top How Many Number])

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County

Amy Blankenship wrote:
Hi, all;

I need to be able to use one query to retrieve either the most recent result
or all results, based on a parameter to the query. I don't seem to be able
to find a syntax that will allow me to do something like:

SELECT TOP [Enter how many things] things FROM MyTable ORDER BY thingNum;

Has anyone solved this before?

TIA;

Amy


  #3  
Old July 14th, 2008, 07:05 PM posted to microsoft.public.access.queries
Amy Blankenship
external usenet poster
 
Posts: 539
Default Retrieving variable number of rows


"John Spencer" wrote in message
...
You can't use a parameter to set the Top predicates value.

You can build the query's sql on the fly using VBA or you can use a
ranking query

Select Top N records where N is variable ( a parameter)

One method that can be used is to create a ranking query to assign a
number to the records and then use the ranking to return N records.

'Probably not updatable
SELECT MyTable.*
FROM MyTable INNER JOIN
(
SELECT A.MyField, Count(B.MyField) as TheCount
FROM MyTable as A LEFT JOIN MyTable As B
ON A.MyField B.MyField
GROUP BY A.MyField
) as Ranking
ON MyTable.MyField = Ranking.MyField
WHERE Ranking.TheCount [Top How Many Number]

'Probably updatable
SELECT MyTable.*
FROM MyTable
WHERE MyField in
(SELECT A.MyField
FROM MyTable as A LEFT JOIN MyTable As B
ON A.MyField B.MyField
GROUP BY A.MyField
HAVING Count(B.MyField) [Top How Many Number])


Cool, thanks. The query I was using this way was already a subquery, so I
wound up using IIF([Return All?], (SELECT myID FROM myTABLE), (SELECT
Max(MyID) FROM myTABLE));

:-)


 




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 Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 11:40 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.