SQL - 使用 where 子句的一个选择中的不同总和级别
我有 2 张桌子。其原始金额保持不变。第二个表列出了随时间推移针对第一个表中的原始金额应用的部分金额。
数据库表:
***memotable***
ID [primary, unique]
Amount (Orginal Amount)
***transtable***
ID [many IDs in transtable to single ID in memotable]
AmountUsed (amount applied)
ApplyDate (date applied)
我想在单个选择中找到 ID、自上周以来使用的金额(ApplyDate > 2011-04-21)、迄今为止使用的金额。
结果中应该出现的唯一行是自上周以来已使用金额的行(ApplyDate > 2011-04-21)。
我一直在尝试获取迄今为止使用的金额的总和,因为这需要包括 ApplyDate > 之外的 AmountUsed 值。 2011年4月21日。
I have 2 tables. One has the orginal amount that remains static. The second table has a list of partial amounts applied over time against the orginal amount in the first table.
DB Tables:
***memotable***
ID [primary, unique]
Amount (Orginal Amount)
***transtable***
ID [many IDs in transtable to single ID in memotable]
AmountUsed (amount applied)
ApplyDate (date applied)
I would like to find, in a single select, the ID, amount used since last week (ApplyDate > 2011-04-21), amount used to date.
The only rows that should appear in the result is when an amount has been used since last week (ApplyDate > 2011-04-21).
I'm stuck on trying to get the sum for the amount used to date, since that needs to include AmountUsed values that are outside of when ApplyDate > 2011-04-21.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下可以避免子选择:
It is possible to avoid subselects in this case:
由于您想将其限制为自上周以来发生的行,但也想包括迄今为止的总数,我认为最有效的方法是使用子选择......
Since you want to limit it to rows that happened since last week, but also want to include the total to date, I think the most efficient method would be to use sub-selects...