如何合并一个表中的两个选择,其中一个具有 GROUP BY,另一个具有 DISTINCT
这两个单独工作
SELECT PONumber,
count(RequestNumber) as "InNumOfRequests",
sum(Amount) as "SumofAmounts"
FROM tableView
GROUP BY PONumber
SELECT DISTINCT PONumber,
(10 * ceiling((DATEDIFF(day,POApprovedDate,GETDATE()))/10))
AS "BINofDaysSincePOApproved"
FROM tableView
我想最终得到:
PONumber | InNumOfRequests | SumofAmounts | BINofDaysSincePOApproved
PO1 | 2 | 100 | 180
PO2 | 1 | 50 | 179
tableView 看起来像:
RequestNumber | PONumber | Amount | POApproved Date
1 | PO1 | 100.00 | 2010-01-01
2 | PO1 | 100.00 | 2010-01-01
3 | PO2 | 50.00 | 2010-01-02
请注意,PO1 实际上是 2 个请求的 PO,因此 POApproved 数据和金额对于这 2 个请求是相同的。
这看起来很简单,但从我正在使用的书(SQL 语言)中我无法弄清楚。
帮助:(
亚历克斯
Both of these work individually
SELECT PONumber,
count(RequestNumber) as "InNumOfRequests",
sum(Amount) as "SumofAmounts"
FROM tableView
GROUP BY PONumber
SELECT DISTINCT PONumber,
(10 * ceiling((DATEDIFF(day,POApprovedDate,GETDATE()))/10))
AS "BINofDaysSincePOApproved"
FROM tableView
And I want to end up with:
PONumber | InNumOfRequests | SumofAmounts | BINofDaysSincePOApproved
PO1 | 2 | 100 | 180
PO2 | 1 | 50 | 179
tableView looks like:
RequestNumber | PONumber | Amount | POApproved Date
1 | PO1 | 100.00 | 2010-01-01
2 | PO1 | 100.00 | 2010-01-01
3 | PO2 | 50.00 | 2010-01-02
note that PO1 is actually the PO for 2 requests and so the POApproved Data and Amount is the same for those 2 requests.
It seems easy but from the book I'm using (The Language of SQL) i can't figure it out.
Help :(
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果日期始终等于每个 PONumber,则可以放在第一个选择中。 MIN 函数 (MIN(POApprovedDate)) 是 group by,then 应该在字段中使用聚合函数的原因。
If the date is ALWAYS equal to each PONumber, can be put in the first select. The MIN function (MIN(POApprovedDate)) is why a group by, then should use aggregate functions in the fields.