大家好,我在尝试从表中获取数据时遇到了一些问题,这是我在执行的选择:
SELECT
HOS.Area_Code,
HOS.Year,
HOS.OrderNumber,
MAX(HOS.Id) as 'Id_Order',
HOS.status,
MAX(HOS.date_status) as 'date_status',
HO.Rut,
HO.Name,
HO.Amount
FROM SDF_Historic_Order_Status as HOS inner join HeaderOrder as HO
ON
HOS.Area_Code = HO.Area_Code AND
HOS.Year = HO.Year AND
HOS.OrderNumber = HO.OrderNumber
WHERE
(HOS.Area_Code= 1) AND
(HOS.Year= 2017) AND
(HOS.OrderNumber= 7) AND
(HOS.date_status < CONVERT(date, DATEADD(day, -2, GETDATE())))
GROUP BY
HOS.Area_Code, HOS.Year, HOS.OrderNumber, HOS.status, HO.Rut, HO.Name, HO.Amount
它返回3行:
因此,即时通讯正在根据该订单的当前状态获取最大ID原因。如您所见,最大ID的状态为2,是否有办法改善我的选择范围,使之仅获得该行?
我不能设置“最大状态”,因为它将返回状态5,当前状态为2。
答案 0 :(得分:0)
如果只需要一行,则可以使用top
和order by
:
select top (1) . . .
from . . .
. . .
order by id_order desc;