简体   繁体   中英

SQL - Limit all Except TOP 6

the SQL I got at the moment and it is working :

SELECT Count(CATEGORY),  COUNT(INCIDENT_ID), CATEGORY,

ROUND(Count(CATEGORY) * 100 / (SELECT Count(*) 
                              FROM   incident_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   incident_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(INCIDENT_ID) DESC
limit 6

This query results in the top 6 Categorys and their number of Incidents with the Percentage.

Now I want to generate a second query, which shows me all Category and their Total Number + Percentage , !EXCEPT! the 6 Category I retrieved in the first query.

So if I have let's say 20 Categorys, I now want number 7-20 Displayed, under one name "Other" and the total Number Sumed up of all Categories (7-20) + their Percentage.

The result should look like this for example:

Category|      Number of Inicdents |    Percentage
Other              200                      20%

Other should Contain all categories, except the from the first query..

Is this Possible?

Would appreciate any help.

------------EDIT-------------

SELECT Count(CATEGORY),  COUNT(INCIDENT_ID), CATEGORY,

ROUND(Count(CATEGORY) * 100 / (SELECT Count(*) 
                          FROM   incident_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   incident_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(INCIDENT_ID)DESC
     limit 6, 1295852105

Cheers

As you don't know the last record so use just a large number as below code

SELECT Count(CATEGORY),  COUNT(INCIDENT_ID), CATEGORY,

ROUND(Count(CATEGORY) * 100 / (SELECT Count(*) 
                              FROM   incident_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   incident_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(INCIDENT_ID) DESC
limit 6, 18446744073709551615;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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