Link to home
Start Free TrialLog in
Avatar of reynaldio
reynaldioFlag for Indonesia

asked on

Help Me with a query

Hi experts,

I have 2 tables

INVOICE TABLE
-----------------------------
InvoiceID
CustomerID
InvoiceDate
InvoiceAmt


PAYMENT TABLE
----------------------
PaymentID
PaymentDate
InvoiceID
AmountPaid



1 invoice may have more than one payment on the same date. I need to have a query that will list last payment of certain invoice. Can you help me with the query? Should i use stored proc to achieve this, or can you use just a simple query. I use firebird rdbms.

Thanks in advance.
Avatar of aflockhart
aflockhart
Flag of United Kingdom of Great Britain and Northern Ireland image

Are the payment ID's generated in order ? I assume they are, because if not, you can't tell which of two invoices on the same date is the later one.

Assuming the IDs are in increasing order it will be something like this  assuming you are looking for the latest payment on invoice 999 - and I don't know firebird, but this is standard SQL:

select * from payment
where paymentID=(select max paymentID from payment where invoiceid=999)







ASKER CERTIFIED SOLUTION
Avatar of neo00000110
neo00000110
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yadaya
yadaya

SELECT FIRST 1 * FROM PAYMENT WHERE InvoiceID=1234567 ORDER BY PaymentDate

Open in new window

try the attached.

or you can alternatively try if PaymentID is cronological and sequencial

....where B.PaymentId = (select max(paymentID) from Payment where InvoiceID = B.InvoiceID)

select	A.*,
	B.*	
from Invoice A
inner join Payment B on A.InvoiceID = b.InvoiceID
where B.PaymentDate = (select max(paymentDate) from Payment where InvoiceID = B.InvoiceID)

Open in new window

Avatar of reynaldio

ASKER

Thank you all for your superfast comments

@aflockhart: i cannot use only payment id or paymentdate. I need to use max(paymentdate) and max(paymentid), since it is possible that user make payment but set the payment date before the actual latest payment.

@ralmada & @yadaya: i also cannot use your query, since it is possible that invoice has 2 payments on the same date.

@neo00000…: i think i can try that query. i will just have to replace "top" to "first"
i've come up with this query.

select  a.invoiceid, b.paymentid
from invoice a
inner join (select first 1 paymentid, invoiceid from payment order by paymentdate desc, paymentid desc) b
on a.invoiceid=b.invoiceid


But i dont think this will work, since "select first 1 paymentid, invoiceid" might only return 1 record and might not be the record for the current invoice. Any idea?
is this possible?


select  a.invoiceid, b.paymentid
from invoice a
inner join (select first 1 paymentid
from payment where invoice.invoiceid=payment.invoiceid
order by paymentdate desc, paymentid desc) b
on 1=1
How about this ( added the line in bold to your previous query)

select  a.invoiceid, b.paymentid
from invoice a
inner join (select first 1 paymentid, invoiceid from payment
where invoiceid=a.invoiceid
order by paymentdate desc, paymentid desc) b
on a.invoiceid=b.invoiceid

>>@ralmada & @yadaya: i also cannot use your query, since it is possible that invoice has 2 payments on the same date.<<

That's why I told you to replace PaymentDate with PaymentID

select	A.*,
	B.*	
from Invoice A
inner join Payment B on A.InvoiceID = b.InvoiceID
where B.PaymentID = (select max(paymentID) from Payment where InvoiceID = B.InvoiceID)

Open in new window

@aflockhart: The query didn't work. "where invoiceid=a.invoiceid", table a is unknown in derived table. Any other idea?
@ralmada: i also cannot use just paymentid. since it is possible that user make payment but set the payment date before the actual latest payment.
Looking back at previous comments - did you try the suggestion from neo0000 ?

If you only need the details for one invoice at a time, you might be able to build a stored procedure based on their suggestion, where you supply a parameter with the invoice nunber that you want.  I don;t know enough about your database software to know what it supports or how you would set it up.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks