troubleshooting Question

Problem with DATEDIFF and computed columns

Avatar of David D
David DFlag for Norway asked on
Microsoft SQL ServerSQL
9 Comments2 Solutions220 ViewsLast Modified:
Hi

I have the following code on MS SQL Server 2012:

ALTER VIEW [dbo].[ExtendedOrderHeaderView]
AS
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
GO

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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros