Distinct from custom table  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Distinct from custom table

Postby 2can2 » Wed Jul 08, 2015 9:30 pm

Hi,
I have added some code to the end of usp_Jiwa_invoices_invoice in V70115 and want to Select the TOP 1 record From DB_Notes for a Note Type = 'hard coded id'. I am battling to get only One record of a Specific Note Type (say GenPackNote) where there could be more than 1.
Code as below - can you show me the correct syntax. I think I need the Top1 in a separate Select ???
SELECT #TempTable2.*, DB_Main.Phone AS DrPhone, SO_Lines.Discount AS Discount,
tbLineDBNotes_GenPack.NoteText as GenPackNotes
FROM #TempTable2
INNER JOIN SO_Lines ON #TempTable2.InvoiceLineID = SO_Lines.InvoiceLineID
INNER JOIN DB_Main ON #TempTable2.AccountNo = DB_Main.AccountNo
--- HAVE to do this in case no 'GenPackerNotes' records and then can pass NULL!!
LEFT OUTER JOIN
(SELECT TOP 1 DB_Notes.DebtorID, DB_Notes.NoteText
FROM DB_Notes
WHERE (DB_Notes.NoteTypeID = '20bd5f02-810d-4c6f-9322-15ae6ab54090')) AS tbLineDBNotes_GenPack
ON DB_Main.DebtorID = tbLineDBNotes_GenPack.DebtorID
Thanks.
2can2
Frequent Contributor
Frequent Contributor
 
Posts: 171
Joined: Tue Feb 26, 2008 10:24 am
Topics Solved: 25

Re: Distinct from custom table  Topic is solved

Postby Mike.Sheen » Wed Jul 15, 2015 7:17 pm

2can2 wrote:want to Select the TOP 1 record From DB_Notes for a Note Type = 'hard coded id'. I am battling to get only One record of a Specific Note Type (say GenPackNote) where there could be more than 1.


A subquery is probably what you are looking for:

Code: Select all
SELECT SO_Main.InvoiceNo, DB_Main.Name, (SELECT TOP 1 DB_Notes.NoteText FROM DB_Notes WHERE DB_Notes.DebtorID = DB_Main.DebtorID AND DB_Notes.NoteTypeID = '32F2495E-FA0D-49D0-8A49-40615BFB53D7')
FROM SO_Main
JOIN DB_Main ON DB_Main.DebtorID = SO_Main.DebtorID


Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2446
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 757

Re: Distinct from custom table

Postby 2can2 » Thu Jul 23, 2015 11:08 am

Thanks Mike. Always a simple solution.
Cheers
2can2
Frequent Contributor
Frequent Contributor
 
Posts: 171
Joined: Tue Feb 26, 2008 10:24 am
Topics Solved: 25


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 1 guest