开发者

SQL - Select Fields of the Max ID

开发者 https://www.devze.com 2022-12-13 02:23 出处:网络
The last InvoiceID, and corresponding fields needs to be selected.The entire sql contains several inner joins.

The last InvoiceID, and corresponding fields needs to be selected. The entire sql contains several inner joins.

SELECT max(InvoiceID), 
       InvoiceEndDate 
  FROM Invoices 
 WHERE TransactionOrderItemID = '000831'

Right now, I am getting the InvoiceID, and have to fetch the InvoiceEnd开发者_运维技巧Date again.

Is there an efficient way of doing this?


SELECT InvoiceID, InvoiceEndDate 
FROM Invoices 
WHERE TransactionOrderItemID='000831'
ORDER BY InvoiceID DESC
LIMIT 1


SELECT InvoiceID, InvoiceEndDate 
FROM Invoices INV
WHERE TransactionOrderItemID='000831'
  AND INV.InvoiceID = (SELECT MAX(SUB.InvoiceID)
    FROM Invoices SUB WHERE SUB.TransactionOrderItemID='000831');


Take a look at Including an Aggregated Column's Related Values which has several ways to accomplish this

0

精彩评论

暂无评论...
验证码 换一张
取 消