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

前端 未结 3 2013
难免孤独
难免孤独 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:58

    This should do it:

    SELECT I.category AS category, COUNT(*) AS items, COUNT(*) / T.total * 100 AS percent
    FROM Item as I,
         (SELECT COUNT(*) AS total FROM Item WHERE Department='Popular') AS T
    WHERE Department='Popular'
    GROUP BY category;
    

提交回复
热议问题