How to combine top and count in ms access sql so that every record other than the 1st is retrieved?

How to combine top and count in ms access sql so that every record other than the 1st is retrieved?



How to combine top and count in ms access sql so that every record other than the 1st is retrieved? i.e. exclude the 1st record, but do not know the number of records that exists.



I imagine the sql would be something like:


SELECT TOP

(SELECT COUNT(*)
FROM Company LEFT JOIN CompanySecretary ON Company.[Company Number] = CompanySecretary.[Company Number]
WHERE Company.[Company Number]=[Forms]![Company]![Company Number]
- 1)

Company.[Company Number], CompanySecretary.[Name]
FROM Company LEFT JOIN CompanySecretary ON Company.[Company Number] = CompanySecretary.[Company Number]
WHERE Company.[Company Number]=[Forms]![Company]![Company Number]
ORDER BY Company.[Company Number] ASC, CompanySecretary.[Name] ASC;



E.g.


Company.[Company Number] | CompanySecretary.[Name]
---------------------------------------------------
002 | Mark
002 | Paul
002 | William



The expected result would be:


Company.[Company Number] | CompanySecretary.[Name]
---------------------------------------------------
002 | Paul
002 | William



Thank you so much.
Grateful.





I guess ASC - not AESC
– JohnyL
Aug 27 at 5:30


ASC


AESC





Off-topic: change your name to something meaningful :)
– JohnyL
Aug 27 at 5:35





A small data sample and the expected result would help. See also meta.stackoverflow.com/questions/333952/…
– Koen
Aug 27 at 5:43





Welcome aboard, Do you have to use TOP and COUNT? like an exercise in JET SQL course, or you really need the right answer whatever it is like in real life?
– Sion.D.P
Aug 27 at 8:50


TOP


COUNT





Please provide sample data and desired results. The question doesn't make sense for a couple of reasons. First AESC is quite ambiguous -- ASC or DESC. Second, you are ordering by the column used in the WHERE, so it has a constant value. SQL result sets (and tables) have no "first" row, unless a column specifies the ordering. If the values are all equal, then no column specifies the ordering.
– Gordon Linoff
Aug 27 at 11:04


AESC


ASC


DESC


WHERE




1 Answer
1



Assuming no 2 secretaries in same company with same name


SELECT TOP 1 Company.[Company Number], CompanySecretary.[Name]
FROM Company LEFT JOIN CompanySecretary
ON Company.[Company Number] = CompanySecretary.[Company Number]
WHERE Company.[Company Number]=[Forms]![Company]![Company Number]
ORDER BY Company.[Company Number] ASC, CompanySecretary.[Name] ASC



These are the pair of values to exclude from the result set



Lets create this as qTop1 in Access queries


qTop1



Now the desired query will be


SELECT Company.[Company Number], CompanySecretary.[Name]
FROM Company LEFT JOIN CompanySecretary
ON Company.[Company Number] = CompanySecretary.[Company Number]
WHERE Company.[Company Number]=[Forms]![Company]![Company Number]
AND NOT EXISTS (SELECT 1 FROM qTop1
WHERE qTop1.[Company Number] = Company.[Company Number]
AND qTop1.Name = CompanySecretary.[Name])
ORDER BY Company.[Company Number] ASC, CompanySecretary.[Name] ASC






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế