我正在编写一个简单的查询来获取具有MAX ADDRESS_SEQ_NUM的VENDOR_ID,以便我得到以下输出:
VENDOR_ID ADDRESS_SEQ_NUM
76109A 81
问题是,当我编写以下SQL时,我回到每个VENDOR_ID并且它自己的最大ADDRESS_SEQ_NUM,而我只需要整个表中的VENDOR_ID和max ADDRESS_SEQ_NUM。
SELECT VENDOR_ID, MAX(ADDRESS_SEQ_NUM)
FROM PS_VNDR_ADDR_SCROL
GROUP BY VENDOR_ID
ORDER BY
ADDRESS_SEQ_NUM下降。使用TOP 1
仅获取第一行,即具有最高ADDRESS_SEQ_NUM值的行。
SELECT TOP 1 VENDOR_ID, ADDRESS_SEQ_NUM
FROM PS_VNDR_ADDR_SCROL
ORDER BY ADDRESS_SEQ_NUM DESC;
你必须做一个subselect
SELECT VENDOR_ID, ADDRESS_SEQ_NUM
FROM PS_VNDR_ADDR_SCROL
WHERE ADDRESS_SEQ_NUM = (SELECT MAX(ADDRESS_SEQ_NUM)
FROM PS_VNDR_ADDR_SCROL)