Saturday, February 25, 2012

Retrieve Last Record SQL Stmt

Is there a way to get the last record from the duplicate records. To give an
example, I have table that has records as follows:
BranchPO
branch PO POLine Amount POsuf
555 147586 10 399.00 2
555 147586 10 .00 1
555 147586 10 27.00 0
I can't use POSuf in my criteria. branch, PO and POline are the only fields
that I can set my query. These being duplicate records, how do I get the
last record which is
branch PO POLine Amount POsuf
555 147586 10 399.00 2
If I use Progress GL language, I can use the following stmt to get the last
record.
Find Last BranchPO where branch = 555 and po = 147586 and poline = 10
no-error.
My result will be the last created record
branch PO POLine Amount
555 147586 10 399.00
Please help.
Thank you so much
Does this table have a key? In SQL tables are unordered and there is no
reliable way to retrieve rows in the order in which they were inserted
unless you persist that information as data in the table. If the last
inserted row is important to you then your best bet is to recreate the data
or to add a key in the correct order and then use that to drive your query.
If the insertion order is not so important and you just need to remove
duplicates then you can GROUP BY branch, po, poline.
David Portas
SQL Server MVP
|||Thank you so very much for your suggestion.
Insertion order is not important b'cos I'm dumping the data into SQL server
from the ERP system.
I will try by Removing the duplicates with GROUP BY branch, po, poline. I
feel positive that this works.
Thank you
"David Portas" wrote:

> Does this table have a key? In SQL tables are unordered and there is no
> reliable way to retrieve rows in the order in which they were inserted
> unless you persist that information as data in the table. If the last
> inserted row is important to you then your best bet is to recreate the data
> or to add a key in the correct order and then use that to drive your query.
> If the insertion order is not so important and you just need to remove
> duplicates then you can GROUP BY branch, po, poline.
> --
> David Portas
> SQL Server MVP
> --
>
>

No comments:

Post a Comment