#*#*#这样的sql语句怎么写??在线揭贴100分*******************

wafer_w 2003-07-03 02:16:47
表结构如下:
ID COL1
1 0
2 1
2 1
2 2
1 1
1 2
3 2
4 1

求一个查询语句,得到的记录为
id 总条数 COL1值为0的记录数 COL1值为1的记录数 COL1值为2的记录数
1 3 1 1 1
2.....

其中,col1的取值范围0-2是已知确定的,怎么写????

4
...全文
99 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
CrazyFor 2003-07-07
  • 打赏
  • 举报
回复
selct id
,count(*) 总条数
,sum(case when col1=0 then 1 else 0 end) COL1值为0的记录数
,sum(case when col1=1 then 1 else 0 end) COL1值为1的记录数
,sum(case when col1=2 then 1 else 0 end) COL1值为2的记录数
from 表 group by id
RobinHZ 2003-07-04
  • 打赏
  • 举报
回复
有informix版,把你的问题转到那里去吧。
sccyzhi 2003-07-03
  • 打赏
  • 举报
回复
select id,count(*),sum(decode(col1,0,1,0)) c0,sum(decode(col1,1,1,0)) c1,sum(decode(col,2,1,0)) c2 from your_table group by id order by 1
zjcxc 2003-07-03
  • 打赏
  • 举报
回复
select id,count(*) as 总条数
,sum(case col1 when 0 then 1 else 0 end) as col1为0的条数
,sum(case col1 when 1 then 1 else 0 end) as col1为1的条数
,sum(case col1 when 2 then 1 else 0 end) as col1为2的条数
from 表
group by id
beckhambobo 2003-07-03
  • 打赏
  • 举报
回复
select * from (select id,count(*) ct,sum(decode(col1,0,1,0)) c0,sum(decode(col1,1,1,0)) c1,sum(decode(col,2,1,0)) c2 from table_name group by id) order by ct
smallcrocodile 2003-07-03
  • 打赏
  • 举报
回复
支持
wafer_w 2003-07-03
  • 打赏
  • 举报
回复
decode是ORACLE专有的吧?能不能用标准的SQL完成上述功能呢?
wafer_w 2003-07-03
  • 打赏
  • 举报
回复
sum(decode(col1,0,1,0)) 这句话怎么理解,我对sql不是很熟,能说清楚些吗??
痞子酷 2003-07-03
  • 打赏
  • 举报
回复
select id,count(*),sum(decode(col1,0,1,0)) c0,sum(decode(col1,1,1,0)) c1,sum(decode(col,2,1,0)) c2 from your_table group by id;

decode 相当于swict +case
BlueskyWide 2003-07-03
  • 打赏
  • 举报
回复
你所在的贴子位置为"Oracle开发",
给个排序:
select id,count(*) abc,sum(decode(col1,0,1,0)) c0,sum(decode(col1,1,1,0)) c1,sum(decode(col,2,1,0)) c2 from your_table group by id order by abc desc;
wafer_w 2003-07-03
  • 打赏
  • 举报
回复
decode是什么意思??我用的是informix数据库,不是oracle,能用标准sql实现吗?
wafer_w 2003-07-03
  • 打赏
  • 举报
回复
还有,要求选出的结果按照总条数(或其它)排序
BlueskyWide 2003-07-03
  • 打赏
  • 举报
回复
select id,count(*),sum(decode(col1,0,1,0)) c0,sum(decode(col1,1,1,0)) c1,sum(decode(col,2,1,0)) c2 from your_table group by id;

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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