SQL中用Group by 分组 假如有个字段ID,值1~10,想分成3组(1,3,7)(2,4,5,6)(8,9,10)
用groupby能分组吗,怎么分假设还有一个字段num,要用groupby统计三个分组的num值的和,怎么写。...
用group by 能分组吗,怎么分
假设还有一个字段 num,要用group by 统计三个分组的num值的和,怎么写。 展开
假设还有一个字段 num,要用group by 统计三个分组的num值的和,怎么写。 展开
1个回答
展开全部
select id,sum(case when id in (1,3,7) then 1 else 0 end),
sum(case when id in (2,4,5,6) then 1 else 0 end),
sum(case when id in (8,9,10) then 1 else 0 end)
from tb group by id
这个意思?
-----补充-----
select num,sum(case when id in (1,3,7) then num else 0 end),
sum(case when num in (2,4,5,6) then num else 0 end),
sum(case when num in (8,9,10) then num else 0 end)
from tb group by num
试验下这个
sum(case when id in (2,4,5,6) then 1 else 0 end),
sum(case when id in (8,9,10) then 1 else 0 end)
from tb group by id
这个意思?
-----补充-----
select num,sum(case when id in (1,3,7) then num else 0 end),
sum(case when num in (2,4,5,6) then num else 0 end),
sum(case when num in (8,9,10) then num else 0 end)
from tb group by num
试验下这个
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询