首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问一sql count

2012-04-14 
请教一sql counttype inum1223351428311select count(*) from 表 group by typetype count16211316现在想

请教一sql count
type inum
12
23
35
14
28
311

select count(*) from 表 group by type

type count
16
211
316

现在想要把类型1,2的合并count
type count
1和217
316

该如何写

[解决办法]
select sum(inum) from tt group by (type-1)/2

[解决办法]

SQL code
select type,SUM(num)num from (select case TYPE when 2 then '1'+'和2' when 1 then '1'+'和2' else '3' end type,num from tb)a group by type/*type    num1和2    173    16*/
[解决办法]
SQL code
use tempdb gocreate table tb (type int ,num int)goinsert tb select 1, 2 union allselect 2 ,3 union allselect 3 ,5 union allselect 1 ,4 union allselect 2 ,8 union allselect 3 ,11select type ,sum(num) as num from (select case when type=1 then '1和2' when type=2 then '1和2' else '3' end type, sum(num) as num  from tb group by type)a group by typetype num---- -----------1和2  173    16(2 行受影响) 

热点排行