Sql server 2012 如何在此select查询中返回最小日期
以下是查询:Sql server 2012 如何在此select查询中返回最小日期,sql-server-2012,Sql Server 2012,以下是查询: -- Get purchase order ETA for first purchase order whose quantity completes a (running total) sum (within the interval between today's date and some future date) adequate to satisfy current demand select min(t2.docduedate) as ETA from po
-- Get purchase order ETA for first purchase order whose quantity completes a (running total) sum (within the interval between today's date and some future date) adequate to satisfy current demand
select min(t2.docduedate) as ETA
from por1 t1
inner join opor t2 on t1.docentry = t2.docentry
join opor t3 on t2.docentry = t3.docentry
and t3.docduedate <= t2.docduedate
where t1.itemcode = 'NSL1705-S'
and t2.docduedate > getdate()
group by t1.itemcode
-- , t1.dscription
, t2.docduedate
having (select sum(t6.quantity)
from por1 t6
inner join opor t7 on t6.docentry = t7.docentry
where t7.docduedate <= t2.docduedate
and t7.docduedate > getdate()
and t6.itemcode = 'NSL1705-S') >= 141
我希望它只返回包含最小日期的行:
ETA
2016-02-17 00:00:00.0
我错过了什么?“mint2.docduedate”显然没有按我希望的方式工作
注意:这实际上是作为更大查询的一部分的子查询,因此我不能使用任何不能在子查询中使用的内容来解决此问题
由于500个内部服务器错误而进行更新,此操作有效:
select min(t2.docduedate) as ETA
from por1 t1
inner join opor t2 on t1.docentry = t2.docentry
join opor t3 on t2.docentry = t3.docentry
and t3.docduedate <= t2.docduedate
where t1.itemcode = 'NSL1705-S'
and t2.docduedate > getdate()
and (select sum(t6.quantity)
from por1 t6
inner join opor t7 on t6.docentry = t7.docentry
where t7.docduedate <= t2.docduedate
and t7.docduedate > getdate()
and t6.itemcode = 'NSL1705-S') >= 141
如果你去掉GROUPBY子句,结果会怎样?@500 InternalServerError,谢谢你的理智检查。仅仅通过将having条件移动到where子句来摆脱组就解决了问题。如果你想把你的建议变成一个答案,我会接受的。谢谢
select min(t2.docduedate) as ETA
from por1 t1
inner join opor t2 on t1.docentry = t2.docentry
join opor t3 on t2.docentry = t3.docentry
and t3.docduedate <= t2.docduedate
where t1.itemcode = 'NSL1705-S'
and t2.docduedate > getdate()
and (select sum(t6.quantity)
from por1 t6
inner join opor t7 on t6.docentry = t7.docentry
where t7.docduedate <= t2.docduedate
and t7.docduedate > getdate()
and t6.itemcode = 'NSL1705-S') >= 141