我想在where子句中包含MAX日期范围的选择。我正在使用它必须把它带进来,但它错误地说"Column 'dbo.PURCHLINE.PURCHID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
但它不会让我分组,抛出其他错误。也许我正在接近这个错误。基本上,我需要在此查询的一组列中获取最新日期:
Select
PURCHLINE.PURCHID as 'PO'
,SUBSTRING(Cast(Max(VENDPACKINGSLIPJOUR.DELIVERYDATE) as varChar), 0, 12) as 'DeliveryDate'
,PURCHLINE.QTYORDERED as 'QtyOrdered'
FROM dbo.PURCHLINE
INNER JOIN dbo.INVENTTABLE
ON dbo.PURCHLINE.ITEMID = dbo.INVENTTABLE.ITEMID
INNER JOIN dbo.INVENTITEMGROUP
ON dbo.INVENTTABLE.ITEMGROUPID = dbo.INVENTITEMGROUP.ITEMGROUPID
INNER JOIN dbo.INVENTPOSTING
ON dbo.INVENTITEMGROUP.ITEMGROUPID = ITEMRELATION
INNER JOIN dbo.PURCHTABLE
ON dbo.PURCHLINE.PURCHID = dbo.PURCHTABLE.PURCHID
INNER JOIN dbo.LEDGERTABLE
ON LEDGERACCOUNTID = ACCOUNTNUM
INNER JOIN VENDPACKINGSLIPJOUR
on VENDPACKINGSLIPJOUR.PURCHID = PURCHLINE.PURCHID
WHERE
dbo.PURCHLINE.PURCHSTATUS = 3 --open Order
AND dbo.PURCHLINE.PURCHASETYPE = 3 --Purchase Order
AND dbo.PURCHLINE.DIMENSION IN (@department)
AND INVENTACCOUNTTYPE = 0 --Purchase Packing Slip
AND PURCHLINE.DIMENSION2_ = @division
HAVING Max(VENDPACKINGSLIPJOUR.DELIVERYDATE) between @start and @end
答案 0 :(得分:2)
您有一个聚合,因此您需要对nonaggregate字段进行分组:
GROUP BY
PURCHLINE.PURCHID
,PURCHLINE.QTYORDERED
这在HAVING
子句之前。