我一直试图弄清楚如何只选择最近的日期值
SELECT TOP (100) PERCENT
dbo.StockStatusHistory.StockID AS Expr1,
dbo.Stock.StockNo, dbo.CardFile.CardName AS SalesPerson,
dbo.Stock.Description, dbo.Stock.Retail,
dbo.Stock.SaleOrderDate, dbo.Stock.WarrantyStartDate, dbo.Stock.SaleNotes,
dbo.Stock.ManuMonth, dbo.Stock.ManuYear, dbo.Stock.InspCertDate,
dbo.Stock.StockStatus, dbo.StockStatusHistory.NewStockStatus,
dbo.StockStatusHistory.ChangeDate AS Expr2,
dbo.StockStatusHistory.ID, dbo.CardFile.CardId,
dbo.Stock.SalesPersonId
FROM
dbo.Stock
INNER JOIN
dbo.CardFile ON dbo.Stock.SalesPersonId = dbo.CardFile.CardId
INNER JOIN
dbo.StockStatusHistory ON dbo.Stock.StockId = dbo.StockStatusHistory.StockID
WHERE
(dbo.Stock.StockStatus = 30) AND (dbo.Stock.WarrantyStartDate >= { fn CURDATE() })
AND (dbo.StockStatusHistory.NewStockStatus = 30)
OR (dbo.Stock.StockStatus = 30) AND (dbo.Stock.WarrantyStartDate IS NULL)
AND (dbo.StockStatusHistory.NewStockStatus = 30)
我尝试使用MAX(dbo.StockStatusHistory.StockID)
,但我想出了错误。
希望我的代码有意义。显然是SQL查询的新手。
提前致谢 安迪
答案 0 :(得分:0)
我并不完全明白“最近的日期值”是什么意思,但我认为您希望基于某个日期字段的最新条目。
如果没有手头的架构,很难说,但最后可以创建ORDER BY MyDate DESC
,并在开头创建SELECT TOP 1 dbo.xxx
。