回覆列表
-
1 # 變餅檔
-
2 # 使用者6847486145697
select sno,type ,count(type)from table group by sno,type
這是豎的,橫的下面
select sno,sum(case when type="a" then 1 else 0 end) as t_a,sum(case when type="b" then 1 else 0 end) as t_b,sum(case when type="c" then 1 else 0 end) as t_c from table group by sno
語句1:
SELECT TypeID, COUNT(*) AS [count] FROM GoodsInfo GROUP BY TypeID
得到結果
解析結果:GoodsInfo表有 4條記錄, 分別統計每個TypeID 有多少; 例子中說明 TypeID=1 的有2個,TypeID=2 的有1個,TypeID=3 有1個
語句2:
SELECT COUNT(*) AS [count] FROM (
SELECT TypeID FROM GoodsInfo GROUP BY TypeID
) AS t
得到結果
解析結果:GoodsInfo 表中的記錄,只有3種不同的TypeID