一条sql怎么写

黑夜中的一点慰藉 2014-07-17 03:35:44
现在有这个么一张表:
table test
id , name,type,dc这几个属性
数据有下面一条
id name type dc
1 a qw 12
2 b w 9
3 c qw 12
4 d w 9
5 e w 9
6 f w 9
7 g w 11
现在我想通过sql语句查询
合计 dc12 dc9 dc其他
heji type(qw) type(w) heji type(qw) type(w) heji type(qw) type(w)
7 2 1 1 4 1 3 1 1

请问一下这样的sql怎么写??
...全文
503 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 10 楼 chacheen 的回复:
count改成sum select dc , count(*) as heji, sum(decode(type,'qw',1,0)) as type(qw) , sum(decode(type,'w',1,0)) as type(w) from test group by dc
比如,在dc=9的条件下,查看 sum(decode(type,'qw',1,0)) as type(qw) ,
  • 打赏
  • 举报
回复
引用 10 楼 chacheen 的回复:
count改成sum select dc , count(*) as heji, sum(decode(type,'qw',1,0)) as type(qw) , sum(decode(type,'w',1,0)) as type(w) from test group by dc
sum(decode(type,'qw',1,0))这个里面能不能嵌套加个条件那??
  • 打赏
  • 举报
回复
引用 14 楼 oZhiMing12 的回复:
如果用一条SQL是不可能实现的,最好是把结果集查询出来再在程序里面处理!
由于表比较复杂,在程序中写,定义的变量也特别多,处理起来for if特别多,我现在正在尝试用后台java来处理
seqwait 2014-07-18
  • 打赏
  • 举报
回复
如果用一条SQL是不可能实现的,最好是把结果集查询出来再在程序里面处理!
InSpirit1 2014-07-18
  • 打赏
  • 举报
回复
sql会很复杂,为何不select出整个表,然后存入二维数组中再做处理?
vict_ 2014-07-17
  • 打赏
  • 举报
回复
count改成sum select dc , count(*) as heji, sum(decode(type,'qw',1,0)) as type(qw) , sum(decode(type,'w',1,0)) as type(w) from test group by dc
vict_ 2014-07-17
  • 打赏
  • 举报
回复
select dc , count(*) as heji, count(decode(type,'qw',1,0)) as type(qw) , count(decode(type,'w',1,0)) as type(w) from test group by dc
wyx100 2014-07-17
  • 打赏
  • 举报
回复
引用 7 楼 wangwuyilove 的回复:
[quote=引用 6 楼 maihao110 的回复:] mysql测试通过

select 
	distinct dc,
	count(*) heji,
	(select count(*) from test t1 where t1.dc = t.dc and t1.type = 'qw') type(qw),
	(select count(*) from test t1 where t1.dc = t.dc and t1.type = 'w') type(w)
from test t group by dc
我以前就这样搞,但是前面的例子不复杂,如果type下qw下又有集中情况比如ss,aa等等,有要统计一下,有四五层的时候该怎么样那,难道一行数据写一个sql,在搞到一起[/quote]顶
  • 打赏
  • 举报
回复
引用 6 楼 maihao110 的回复:
mysql测试通过

select 
	distinct dc,
	count(*) heji,
	(select count(*) from test t1 where t1.dc = t.dc and t1.type = 'qw') type(qw),
	(select count(*) from test t1 where t1.dc = t.dc and t1.type = 'w') type(w)
from test t group by dc
我以前就这样搞,但是前面的例子不复杂,如果type下qw下又有集中情况比如ss,aa等等,有要统计一下,有四五层的时候该怎么样那,难道一行数据写一个sql,在搞到一起
  • 打赏
  • 举报
回复
mysql测试通过

select 
	distinct dc,
	count(*) heji,
	(select count(*) from test t1 where t1.dc = t.dc and t1.type = 'qw') type(qw),
	(select count(*) from test t1 where t1.dc = t.dc and t1.type = 'w') type(w)
from test t group by dc
  • 打赏
  • 举报
回复
引用 4 楼 a304507016 的回复:
[quote=引用 3 楼 wangwuyilove 的回复:] [quote=引用 2 楼 a304507016 的回复:] 这个数据库应该不支持这样的sql吧 select count(id) ,type,dc from test group by type,dc 查出来结果,用后台程序根据自己的逻辑,利用map封装成你想要的结果,在做页面展示就可以了
能不能嵌套几条sql来处理下,纯后台出里,是不是速度效率方面有点差[/quote] 嵌套sql?不知道怎么写能出来你要的效果。 而且如果sql很复杂,效率也不会太高。 不过可以写两个查询sql,其中一个sql, select id,name,type,case when dc in(12,9) then dc else -999 end from test 这样查出来12,9和其他的结果。再用一个sql select * from (select id,name,type,case when dc in(12,9) then dc else -999 end from test ) 这样就可以不用在程序中判断是不是等于12或者9了,而且通过这样的sql 查出来的结果不会有很多行吧? 后台调用循环肯定超级快。而且我感觉即便是不这样写,后台执行代码也会很快,效率问题可以不用考虑。 [/quote] 谢谢哦
酱油一哥 2014-07-17
  • 打赏
  • 举报
回复
引用 3 楼 wangwuyilove 的回复:
[quote=引用 2 楼 a304507016 的回复:] 这个数据库应该不支持这样的sql吧 select count(id) ,type,dc from test group by type,dc 查出来结果,用后台程序根据自己的逻辑,利用map封装成你想要的结果,在做页面展示就可以了
能不能嵌套几条sql来处理下,纯后台出里,是不是速度效率方面有点差[/quote] 嵌套sql?不知道怎么写能出来你要的效果。 而且如果sql很复杂,效率也不会太高。 不过可以写两个查询sql,其中一个sql, select id,name,type,case when dc in(12,9) then dc else -999 end from test 这样查出来12,9和其他的结果。再用一个sql select * from (select id,name,type,case when dc in(12,9) then dc else -999 end from test ) 这样就可以不用在程序中判断是不是等于12或者9了,而且通过这样的sql 查出来的结果不会有很多行吧? 后台调用循环肯定超级快。而且我感觉即便是不这样写,后台执行代码也会很快,效率问题可以不用考虑。
  • 打赏
  • 举报
回复
引用 2 楼 a304507016 的回复:
这个数据库应该不支持这样的sql吧 select count(id) ,type,dc from test group by type,dc 查出来结果,用后台程序根据自己的逻辑,利用map封装成你想要的结果,在做页面展示就可以了
能不能嵌套几条sql来处理下,纯后台出里,是不是速度效率方面有点差
酱油一哥 2014-07-17
  • 打赏
  • 举报
回复
这个数据库应该不支持这样的sql吧 select count(id) ,type,dc from test group by type,dc 查出来结果,用后台程序根据自己的逻辑,利用map封装成你想要的结果,在做页面展示就可以了
  • 打赏
  • 举报
回复
自己定一下dsssss

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧