473,379 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Select Top different number of records

99 64KB
I have a table:
Expand|Select|Wrap|Line Numbers
  1. ChapID   QID    (other fields)
  2. 1        1
  3. 1        2
  4. 1        3
  5. 1        4
  6. 1        5
  7. 2        1
  8. 2        2
  9. 2        3
  10. 3        1
  11. 3        2
  12. --       --
I am able to select Top 2 records for each of ChapID using query
Expand|Select|Wrap|Line Numbers
  1. SELECT t.ChapID, t.QID, FROM MyTable AS t
  2. WHERE (((t.QID) In (SELECT TOP 2 QID FROM MyTable WHERE ChapID = t.ChapID )))
  3. ORDER BY t.ChapID, t.QID;
I am also able to change the Top 2 with any number uing "QueryDef" method.
Now the requirement is that I should be able to use a text box on a form against each ChapID to get a number and my query should return that number of top records (different for each ChapID) for each Chap ID.
One of my ideas include to add another table linked to this one with ChapID including a field NoOfRecs, like:
Expand|Select|Wrap|Line Numbers
  1. ChapID     NoOfRecs
  2. 1           3
  3. 2           2
  4. 3           1
and a query should pick this number to show top x number of records.
Any help please? asap.
Feb 20 '18 #1
6 1174
gnawoncents
214 100+
When you say:
I should be able to use a text box on a form against each ChapID to get a number and my query should return that number of top records (different for each ChapID) for each Chap ID.
...are you saying that the user will input/select the ChapID?

Also, is there any reason the NoOfRecs couldn't go on the same original table? That would make it a little simpler. Either way, you could modify your query using a dlookup to find the number of records based on the selected ChapID.

I get the feeling though that there is something here I'm missing. Could you explain in a little more detail how it works from selecting the chapter to displaying the results?
Feb 20 '18 #2
mshakeelattari
99 64KB
Thank you for your quick response.

The requirement is to get x number of QIDs for each ChapID.
For example:
for ChapID=1, I need Three QIDs (in MyTable there are five QIDs against ChapID=1); for ChapID=2 (Total QIDs for ChaptID=2 are three), two QIDs are required,
for ChapID=3, only one out of two QIDs is needed in a query or on a report.

The number of QIDs required from each ChapID may be taken from a Textbox on a Form shown against each chapID in a list or it may be pre-entered into the original or different Table.

It is actually Different number of questions required from different chapters.

Thanking in anticipation.
Feb 20 '18 #3
gnawoncents
214 100+
So, just to make sure I understand, the user selects a ChapID and you want the query to return the top X records from MyTable where X = the NoOfRecs that correspond to ChapID, regardless of QID.

If so, and you are already using QueryDef to update the "Top X" records, all you need to do is replace X with a Dlookup to find the number of records you want to return. If you haven't already built all this, there are other options that might be easier to implement. Otherwise, this should just plug into your existing code.
Expand|Select|Wrap|Line Numbers
  1. DLookup("[NoOfRecs]", "YourNewTable", "[ChapID] = " & Me.YourFormField)
  2.  
YourNewTable = the table that stores NoOfRecs for each ChapID
YourFormField = the name of the field on your form where you enter the chapter number (the "Me." assumes of course that you are running the code from that same form)
Feb 20 '18 #4
Rabbit
12,516 Expert Mod 8TB
Is QID always sequential starting from 1 for any chapter? If so, you can just filter on that using a less than or equal to the number of recs
Feb 20 '18 #5
mshakeelattari
99 64KB
thank you @gnawoncents . you said:
Expand|Select|Wrap|Line Numbers
  1. , there are other options that might be easier to implement.

Please just point out those ones. Thank you.
Feb 20 '18 #6
gnawoncents
214 100+
@Rabbit already pointed out one option, which will work if, as already stated, the QID is "always sequential starting from 1 for any chapter."

In order to find the best solution, however, we need some more information on how the results will be used.
- Will the results be displayed immediately to a form/report? If so, you could just create/modify the form's/report's filter.
- Will the results be used to do further work and not necessarily displayed immediately? If so, you are better off creating and modifying a query or a recordset for more temporary use.

Again, if you can give us a better picture of how you intend to use the results, we can do more to guide you to a good answer.
Feb 22 '18 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Martin Feuersteiner | last post by:
Dear Group I wonder whether you can give me a syntax example for a SQL Statement. Lets assume I've a table containing three columns ContactID (Primary Key), Firstname and Lastname. I would...
2
by: MLH | last post by:
I feel pretty lucky on my last SQL question. I think I'll try one more... If there is a table (tblCorrespondence) with a field in it named and I am extracting a dynaset of records having an ...
1
by: philip260 | last post by:
Im trying to run a query that will pull distinct data not for the entire select query but for a single column. Here is my sql statement below. Why is it not working? It is probably something...
4
by: allenpramod | last post by:
i am working with ms access. i got a problem with checkboxes. i create a checkbox in the form named chkbox with database fields.i am using continuous form so database fields are displayed with...
1
by: Dmitry Kulinich | last post by:
how to select 200 first records from table? -- Thank you, De Cool, EPE
0
by: =?Utf-8?B?UHVjY2E=?= | last post by:
I Have the following in my own library project and I call it from my C# application twice each time to get Unix group account information and user account information. But it would return...
1
by: indhuma | last post by:
Hi, I want to select all the records against the key word 'thoughts' I wrote the followin code. but i'm getting error!. can you help me? $sqlquery="select * from news where...
2
omerbutt
by: omerbutt | last post by:
hi there i have to select 7 columns from table1, 1 column from table2, and 1 column from tables3 and show the result but i am not getting it right the main thing that i am trying to achieve is that i...
1
missshaikh
by: missshaikh | last post by:
SQL Query to select top 10 records except for the top 10 records I just need aSQL Server Query that can select the next 10 records after the first top 10 records.
6
by: diannosd | last post by:
Hello, i'm using Access 2010 I am now managing a database which is being used or importing text files. The problem or question I have is, Can a spec be programmed dynamically to deal with...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.