SELECT OrderNumber, FIRST(ShippingName) as ShippingName FROM Orders
GROUP BY OrderNumber
这给出了一个错误,我该怎么做?
SELECT OrderNumber, FIRST(ShippingName) as ShippingName FROM Orders
GROUP BY OrderNumber
这给出了一个错误,我该怎么做?
FIRST
SQL Server 不支持该函数。
也许你想要
SELECT OrderNumber, MIN(ShippingName) as ShippingName
FROM Orders
GROUP BY OrderNumber
但是,可能不会。相反,我怀疑您的意思是...对于每个OrderNumber
, 在所有带有 that 的行中,根据某些顺序OrderNumber
选择第一行的 。ShippingName
SELECT OrderNumber,
(
SELECT TOP 1 ShippingName
FROM Orders AS OrdersInner
WHERE OrdersInner.OrderNumber = Orders.OrderNumber
ORDER BY OrdersInner.OrderDate, OrdersInner.OrderId
) as ShippingName
FROM Orders
GROUP BY OrderNumber
但我正在对您的要求和数据库结构做出一些假设。
你可能的意思是:
SELECT TOP 1 OrderNumber, ShippingName
FROM Orders
ORDER BY OrderNumber DESC