SELECT OrderNumber, FIRST(ShippingName) as ShippingName FROM Orders
GROUP BY OrderNumber
这会出错,我该怎么做?
答案 0 :(得分:4)
SQL Server不支持FIRST
功能。
也许你想要
SELECT OrderNumber, MIN(ShippingName) as ShippingName
FROM Orders
GROUP BY OrderNumber
但是,可能不是。相反,我怀疑你的意思是...对于OrderNumber
的所有行中的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
但我对你的要求和你的数据库结构做了一些假设。
答案 1 :(得分:0)
你可能意思是:
SELECT TOP 1 OrderNumber, ShippingName
FROM Orders
ORDER BY OrderNumber DESC