1

我正在编写一个如下所示的存储过程:

SELECT Replace(c_invoice, Char(13) + Char(10), ''),
       c_uom1,
       c_uom2
FROM   @invoice_TABLE AS invoice
       INNER JOIN @uom1_TABLE AS uom1
               ON invoice.ID = uom1.ID
       INNER JOIN @uom2_TABLE AS uom2
               ON uom1.ID = uom2.ID
WHERE  uom1.ID = 4

但我希望 WHERE 子句使用最高的 uom1.ID 号码 - 有人可以告诉我如何做到这一点吗?

4

3 回答 3

1

而不是WHERE你可以使用TOP WITH TIES... ORDER BY

SELECT TOP (1)  WITH TIES replace(c_invoice, char(13) + char(10), ''),
               c_uom1,
               c_uom2
FROM   @invoice_TABLE AS invoice
       INNER JOIN @uom1_TABLE AS uom1
         ON invoice.ID = uom1.ID
       INNER JOIN @uom2_TABLE AS uom2
         ON uom1.ID = uom2.ID
ORDER  BY uom1.ID DESC 
于 2013-09-24T07:58:34.997 回答
0

尝试这个,

SELECT replace(c_invoice,char(13) + char(10), ''), c_uom1, c_uom2 FROM @invoice_TABLE AS invoice
    INNER JOIN (select MAX(ID) AS ID FROM @uom1_TABLE) as uom1 ON invoice.ID = uom1.ID
    INNER JOIN @uom2_TABLE as uom2 ON uom1.ID = uom2.ID
于 2013-09-24T08:00:57.477 回答
0
SELECT replace(c_invoice,char(13) + char(10), ''), c_uom1, c_uom2 
    FROM @invoice_TABLE AS invoice
    INNER JOIN @uom1_TABLE as uom1 ON invoice.ID = uom1.ID
    INNER JOIN @uom2_TABLE as uom2 ON uom1.ID = uom2.ID
    WHERE uom1.ID = (SELECT MAX(ID) FROM @uom1_TABLE)
于 2013-09-24T07:59:49.980 回答