我需要从子查询中指定的列返回信息。
在下面的子查询中,我试图从PaymentDate列返回信息。
当我在查询中尝试选择Order.PaymentDate时,Invalid object name
收到错误Order.PaymentDate
。我试图将此指定为Payment.PaymentDate但我得到相同的错误。
关于如何解决这个问题的任何想法?
(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
答案 0 :(得分:0)
如果你想在外部查询中使用order.payment日期,你需要group by
它,但我怀疑这是你想要的。您想要一个聚合函数,例如max(order.payment)
还是min(order.payment)
?您不能在同一查询中混合聚合函数和非聚合函数。
如果你想要的是,正如你在另一个答案的评论中所说的那样,要验证你的日期范围标准是否已应用,请返回max()和min()值范围,例如:
(SELECT ID, SUM(amount) AS purchase,
min(order.paymentdate) as min_payment,
max(order.paymentdate) as max_payment
FROM Order
WHERE Order.PaymentDate BETWEEN '2012-09-01' AND '2012-09-04'
AND Order.amount >=0
GROUP BY ID)Payment
答案 1 :(得分:0)
由于您获得了日期范围的聚合,因此子查询的任何返回行中都没有任何特定日期。您始终可以在外部查询中以仲裁方式声明日期范围,以显示聚合汇总的日期范围。
SELECT Payment.ID, Payment.purchase, '2012-09-01' AS 'startdate', '2012-09-04' AS 'enddate'
FROM (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) AS Payment