按类别获取每个类别的计数结果

时间:2011-10-18 07:03:17

标签: sql sql-server

我有一张这样的表:

Categories
=================================
 Category |   subcategory   | Text 
---------------------------------
 film       English          The Lost World
 film       Hindi            sholai
 music      English          Enrique
 Music      Hindi            A.R Rahman

我想用计数结果来表达结果。 结果应该是这样的

======================
  Category/    | count
 Sub Cate
 ----------------------
  film           2   
  music          2    
  english        2    
  Hindi          2   

我想得到像下面的结果

请帮助我实现这个

谢谢和问候

世斌

1 个答案:

答案 0 :(得分:2)

SELECT Category AS 'Category/SubCategory', COUNT(*) AS 'Count'
FROM  Categories
GROUP BY Category

UNION

SELECT SubCategory AS 'Category/SubCategory', COUNT(*) AS 'Count'
FROM  Categories
GROUP BY SubCategory