SQL - 不正确使用Union和Order BY

时间:2016-03-18 10:32:32

标签: mysql sql

SELECT Count(STATUS),
   STATUS,
   ROUND(Count(STATUS) * 100 /
           (SELECT Count(*)
            FROM call_view
            WHERE (create_month = Month(Now() - INTERVAL 1 MONTH))
              AND (create_year = Year(Now() - INTERVAL 1 MONTH))
              AND customer_company_name = "Company" ), 1) AS Percentage
FROM call_view
WHERE (create_month = Month(Now() - INTERVAL 1 MONTH))
AND (create_year = Year(Now() - INTERVAL 1 MONTH))
AND customer_company_name = "Company"
GROUP BY STATUS
ORDER BY COUNT(CALL_ID) DESC LIMIT 6
UNION
SELECT Sum(category_count),
   'Other',
   sum(percentage)
FROM
( SELECT Count(CATEGORY) AS category_count,
       COUNT(CALL_ID),
       CATEGORY,
       ROUND(Count(CATEGORY) * 100 /
               (SELECT Count(*)
                FROM call_view
                WHERE (create_month = Month(Now() - INTERVAL 1 MONTH))
                  AND (create_year = Year(Now() - INTERVAL 1 MONTH))
                  AND customer_company_name = "Company" ), 1) 
AS Percentage FROM call_view
WHERE (create_month = Month(Now() - INTERVAL 1 MONTH))
  AND (create_year = Year(Now() - INTERVAL 1 MONTH))
  AND customer_company_name = "Company"
GROUP BY CATEGORY
ORDER BY COUNT(CALL_ID) DESC LIMIT 6,
                                  1525125215) AS original_query

我收到错误"错误使用UNION和ORDER BY"。

我正在使用此查询来获取工具,我需要它才能工作。

我知道有一个解决方案,您可以在两个查询的开头和结尾使用Parathesis(),但我无法将其用于我的工具。

这就是我为此查询需要其他解决方案的原因,而不是使用()解决方案

还有其他解决方案吗?

感谢任何帮助......从2天起就有这个问题......

干杯

1 个答案:

答案 0 :(得分:0)

试试这个:

(
    SELECT Count(STATUS),  
    STATUS, 
    ROUND(Count(STATUS) * 100 / (SELECT Count(*) 
                                  FROM   call_view 
                                  WHERE 
    ( create_month = Month(Now() - 
                    INTERVAL 1 month) ) 
     AND ( create_year = Year( 
         Now() - INTERVAL 1 month) ) 
     AND customer_company_name = "Company"
       ), 1) AS Percentage
    FROM   call_view 
    WHERE  ( create_month = Month(Now() - INTERVAL 1 month) ) 
    AND ( create_year = Year(Now() - INTERVAL 1 month) ) 
    AND customer_company_name = "Company"
    GROUP BY STATUS
    ORDER BY COUNT(CALL_ID) DESC
    limit 6
)
union
(
    Select Sum(category_count), 'Other', sum(percentage) 
    from (
    SELECT Count(CATEGORY) as category_count,  COUNT(CALL_ID), CATEGORY,

    ROUND(Count(CATEGORY) * 100 / (SELECT Count(*) 
                              FROM   call_view 
                              WHERE 
      ( create_month = Month(Now() - 
                INTERVAL 1 month) ) 
    AND ( create_year = Year( 
      Now() - INTERVAL 1 month) ) 
    AND customer_company_name ="Company"
    ), 1) AS Percentage
    FROM   call_view 
    WHERE  ( create_month = Month(Now() - INTERVAL 1 month) ) 
    AND ( create_year = Year(Now() - INTERVAL 1 month) ) 
       AND customer_company_name = "Company"
       GROUP BY CATEGORY
      ORDER BY COUNT(CALL_ID) DESC
    limit 6, 1525125215) as original_query
)