简体   繁体   English

具有子查询的多个GROUP BY

[英]Multiple GROUP BY with Subqueries

I am trying to create a query that will provide me two separate COUNTs in two separate columns. 我正在尝试创建一个查询,该查询将在两个单独的列中为我提供两个单独的COUNT。 A count for applications and a count for approved applications. 申请计数和批准申请计数。 I want these counts displayed for each month within a span of time. 我希望在一段时间内每个月显示这些计数。 Like so: 像这样:

date      applications  approvals
January   20            3    
February  25            1
March     20            4
April     10            2

When using the subqueries, I grouped the main query using "GROUP BY MONTH(APPL_CREATE_DT)". 使用子查询时,我使用“按月份分组的组(APPL_CREATE_DT)”对主查询进行了分组。 The problem with this though is that it only correctly grouped the first column while the next column contained the same count for each month. 但是,这样做的问题在于,它仅正确地将第一列分组,而下一列包含每个月相同的计数。 If I add the same GROUP BY to the other subquery section, then I get a 1242 error. 如果将相同的GROUP BY添加到其他子查询部分,则会收到1242错误。

Any ideas on how to work this out would be helpful. 关于如何解决此问题的任何想法都将有所帮助。 I've seen other questions like this, but they weren't so dependent on other clauses for a solution. 我已经看到其他类似的问题,但是它们并不是很依赖其他子句来提供解决方案。

SELECT
COUNT(DISTINCT APPL_ID) AS Applications,
(SELECT COUNT(DISTINCT APPL_ID)
FROM APPL_APP
JOIN APPU_APP_USER ON APPL_ID = APPU_APPL_ID
LEFT JOIN APPD_APP_DECISION ON APPD_APPL_ID = APPL_ID
 WHERE APPD_STATUS = 'LENDER_APPROVED'
  AND APPU_CUR_STATE = 'TX'
  AND APPL_CREATE_DT >= '2013-01-01 00:00:00'
  AND APPL_CREATE_DT <= '2014-01-31 23:59:59'
GROUP BY MONTH(APPL_CREATE_DT)) AS Approvals
FROM APPU_APP_USER
JOIN APPL_APP ON APPL_ID = APPU_APPL_ID
 AND APPL_CREATE_DT >= '2013-01-01 00:00:00'
 AND APPL_CREATE_DT <= '2014-01-31 23:59:59'
 AND APPU_CUR_STATE = 'TX'
GROUP BY MONTH(APPL_CREATE_DT);

To make things simple, I've deleted some where conditions. 为简单起见,我删除了一些where条件。

As you mentioned 如你所说

I had looked up doing something like MONTH(outerTable.APPL_CREATE_DATE) = MONTH(inner_table.APPL_CREATE_DATE) 我抬起头来做类似MONTH(outerTable.APPL_CREATE_DATE)= MONTH(inner_table.APPL_CREATE_DATE)的事情

You're right. 你是对的。

but I don't know where to place it to make it work 但我不知道将它放置在哪里

I hope following query would help you. 希望以下查询对您有所帮助。

SELECT
    COUNT(DISTINCT APPL_ID) AS Applications,
    (
        SELECT COUNT(DISTINCT APPL_ID)
        FROM APPL_APP INNER JOIN APPU_APP_USER ON APPL_ID = APPU_APPL_ID
          LEFT JOIN APPD_APP_DECISION ON APPD_APPL_ID = APPL_ID
        WHERE APPD_STATUS = 'LENDER_APPROVED'
          AND MONTH(APPL_CREATE_DT) = MONTH(t1.APPL_CREATE_DT)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^= This condition added
        GROUP BY MONTH(APPL_CREATE_DT)
    ) AS Approvals
FROM APPL_APP t1 INNER JOIN APPU_APP_USER ON APPL_ID = APPU_APPL_ID
              ^^===== Alias added
GROUP BY MONTH(APPL_CREATE_DT);

Please note that MySQL's correlated sub-query has extreamly poor performance in my experience. 请注意,根据我的经验,MySQL的相关子查询的性能极差。 Please consider converting your query to with just sub-query if possible. 如果可能的话,请考虑将查询转换为仅包含子查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM