only get top 3 from the join (1 Viewer)

qwertyjjj

Registered User.
Local time
Yesterday, 19:03
Joined
Aug 8, 2006
Messages
262
How do I only bring back the top 3 from the join in this query?
There will be multiple records in the InvoiceNotes table and many different invoices.

select invoice_no, inv_date, totamnt, projman, invoicenotes.note, invoicenotes.notedate
from invoices
left join invoicenotes on invoicenotes.invno = invoices.invoice_no

The InvoiceNotes tbale has an ID column so I can order by MAX(ID) DESC and then get top if needed
 

Bogzla

Registered User.
Local time
Today, 03:03
Joined
May 30, 2006
Messages
55
try:
Code:
select TOP 3 invoice_no, inv_date, totamnt, projman, invoicenotes.note, invoicenotes.notedate
from invoices
left join invoicenotes on invoicenotes.invno = invoices.invoice_no

should display the first 3 records only. you'll probably want to add an 'ORDER BY' line to the end to make sure you get the 3 you want...

hth,
Bogzla
 

qwertyjjj

Registered User.
Local time
Yesterday, 19:03
Joined
Aug 8, 2006
Messages
262
No, sorry.
That selects the top 3 records of the whole lot.
I want every record in the Invoices table but only the top 3 records for every record in the InvoiceNotes table
 

Bogzla

Registered User.
Local time
Today, 03:03
Joined
May 30, 2006
Messages
55
Sorry, my bad (RTFQ...)

I think you'll need to use a subquery, try something like this:
Code:
WHERE InvoiceNotes.ID IN(SELECT TOP 3 InvoiceNotes.ID FROM InvoiceNotes WHERE invoicenotes.invno = invoices.invoice_no ORDER BY InvoiceNotes.ID DESC;)
the subquery *should* return the top 3 invoiceNotes.IDs for each invoice no..
 

Users who are viewing this thread

Top Bottom