Avatar of David D
David D
Flag for Norway

asked on 

Problem with DATEDIFF and computed columns


I have the following code on MS SQL Server 2012:

ALTER VIEW [dbo].[ExtendedOrderHeaderView]
SELECT        H.*
	,dbo.PreviousOrderDate(H.custID) as [PreviousOrderDate] 
	,RealOrderCount = (SELECT count(o.orderno) FROM OrderHeaders O WHERE (O.custid=H.CustID) and (OrderType=0))
	,RealLastOrderDate = (SELECT max(o.createdat) from OrderHeaders O WHERE (o.custid=H.custid) and (OrderType=0))
	,DaysSincePreviousOrder = DATEDIFF(DAY,h.[CreatedAt],[PreviousOrderDate])

FROM            dbo.OrderHeaders as H

Open in new window

It gives an Invalid column name 'PreviousOrderDate' on the DATEDIFF when trying to compile. It accepts the table field, but not the computed field name
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
David D

8/22/2022 - Mon