I need to return information from a column that is specified in a subquery.
In the subquery below, I am trying to return info from the PaymentDate column.
When I try to Select Order.PaymentDate in my query I get an error Invalid object name for the Order.PaymentDate. I tried to specify this as Payment.PaymentDate but I get the same error.
Any idea on how I can fix this?
(SELECT ID, SUM(amount) AS purchase FROM Order
WHERE Order.PaymentDate BETWEEN '2012-09-01' AND '2012-09-04'
AND Order.amount >=0
GROUP BY ID)Payment
If you want the order.payment date available in an outer query, you’ll need to
group byit, but I doubt that’s what you want. Do you want an aggregate function, likemax(order.payment)ormin(order.payment)? You can’t mix aggregate and non-aggregate functions in the same query.If what you want, as you say in a comment to another answer, is to validate your date range criteria was applied, return the max() and min() range of values, like: