Getting percentage of “Count(*)” to the number of all items in “GROUP BY”

前端 未结 3 2020
难免孤独
难免孤独 2020-12-01 01:37

Let\'s say I need to have the ratio of "number of items available from certain category" to "the number of all items". Please consider a

3条回答
  •  孤城傲影
    2020-12-01 01:43

    SELECT Category, COUNT(*) AS Total , (COUNT(*) / (SELECT COUNT(*) FROM Item WHERE Department='Popular')) * 100 AS 'Percentage to all items', 
    FROM Item
    WHERE Department='Popular'
    GROUP BY Category;
    

    I'm not sure of the MySql syntax, but you can use a sub-query as shown.

提交回复
热议问题