Subject SQL question - getting "last" record
Author Kok_BG
Hi group,
Can someone help me to optimize this query:

select first 1 skip 0

(tsd."Price" - (tsd."Price"* tsd."Discount"/100))

"tSupplyDET" tsd left join "vSupplyGRP" tsg left join "tOperationType" top
on tsg."OpType"= top."Id"
on tsd."GRPId"=tsg."Id"
where tsd."ArticleId" = 31 and
(top."Pay"='Y' or top."Profit"='Y')

order by tsg."DocumentDate" desc, tsd."Id" desc

As you see, i load all records, sort it by date and fetch only the first one, which seems very unfriendly for the server.

Actually I need only some fields ("Price" and "Discount") from the "last" record, based on the criteria shown in the query.

Thanks a lot.