如何合并一个表中的两个选择,其中一个具有 GROUP BY,另一个具有 DISTINCT

发布于 2024-09-26 03:28:22 字数 992 浏览 4 评论 0原文

这两个单独工作

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画骨成沙 2024-10-03 03:28:22
SELECT *
   FROM (
         SELECT PONumber, count(RequestNumber) as "InNumOfRequests", sum(Amount) as "SumofAmounts"
           FROM tableView
          GROUP BY PONumber
        ) a,
        (
         SELECT DISTINCT PONumber, (10 * ceiling((DATEDIFF(day, POApprovedDate, GETDATE()))/10)) AS "BINofDaysSincePOApproved"
           FROM tableView
        ) b
 WHERE a.PONumber = b.PONumber
SELECT *
   FROM (
         SELECT PONumber, count(RequestNumber) as "InNumOfRequests", sum(Amount) as "SumofAmounts"
           FROM tableView
          GROUP BY PONumber
        ) a,
        (
         SELECT DISTINCT PONumber, (10 * ceiling((DATEDIFF(day, POApprovedDate, GETDATE()))/10)) AS "BINofDaysSincePOApproved"
           FROM tableView
        ) b
 WHERE a.PONumber = b.PONumber
逆光飞翔i 2024-10-03 03:28:22

如果日期始终等于每个 PONumber,则可以放在第一个选择中。 MIN 函数 (MIN(POApprovedDate)) 是 group by,then 应该在字段中使用聚合函数的原因。

SELECT  PONumber, 
        count(RequestNumber) as "InNumOfRequests", 
        sum(Amount) as "SumofAmounts",
        (10 * ceiling((DATEDIFF(day,MIN(POApprovedDate),GETDATE()))/10)) 
         AS "BINofDaysSincePOApproved" 
FROM tableView
GROUP BY PONumber 

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.

SELECT  PONumber, 
        count(RequestNumber) as "InNumOfRequests", 
        sum(Amount) as "SumofAmounts",
        (10 * ceiling((DATEDIFF(day,MIN(POApprovedDate),GETDATE()))/10)) 
         AS "BINofDaysSincePOApproved" 
FROM tableView
GROUP BY PONumber 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文