这是我的起始查询,效果很好。
问题: 我想在select中添加一些将MAX(c.Date)返回为[LastCostDate]作为结果的一部分。但显然,只是添加它本身需要我GROUP BY,但我不想。还有另一种方法可以实现这个目标吗?
SELECT
o.WorkOrder as [Work Order], o.ServiceSite as [Site], o.Description as [Description], o.LeadTechnician as [PM], r.Name as [PM Name], r.RevEmail as [PM Email], o.Notes as [Notes]
FROM
SMWorkOrder o
LEFT JOIN SMWorkCompleted c
ON o.WorkOrder = c.WorkOrder
AND c.Date >= DATEADD(WEEK,-2,CAST(GETDATE() AS DATE)) AND c.Date <= GETDATE()
AND c.SMCo = 1
LEFT JOIN HQRV r
ON o.LeadTechnician = r.Reviewer
LEFT JOIN SMInvoiceList l
ON o.SMCo = l.SMCo
AND o.WorkOrder = l.WorkOrder
WHERE
c.WorkOrder is NULL
AND o.SMCo = 1 AND o.WOStatus not in (1,2)
AND o.EnteredDateTime <= DATEADD(WEEK,-2,CAST(GETDATE() AS DATE))
答案 0 :(得分:1)
MAX (c.Date) OVER( ... )
将为OVER
中的条件返回max c.date(可以为空)