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

wafer_w 2003-07-03 02:55:39
表结构如下:
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是已知确定的,怎么写????

还有,要求选出的结果按照总条数(或其它)排序

---
有朋友提出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;
---
请问用标准SQL能做么?或者在informix下面该怎么做?
(最好是用标准sql能够完成)
...全文
49 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tchatcha 2003-07-11
  • 打赏
  • 举报
回复
up
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
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
wenlq 2003-07-03
  • 打赏
  • 举报
回复
select id,count(*) abc,sum(decode(col1,0,1,0)) c0,sum(decode(col1,1,1,0)) c1,sum(decode(col1,2,1,0)) c2 from your_table group by id order by abc desc;
这条语句在informix 也可以正确执行。

标准sql 也可以,其实只要不用decode 就行了,做个存储过程类似decode;
drop procedure decod;
create procedure decod( col int ,val int ) returning int ;

if col = val then
return 1;
else
return 0;
end if
end procedure ;

select id,count(*) abc,sum(decod(col1,0)) c0,
sum(decod(col1,1)) c1,
sum(decod(col1,2)) c2 from tab group by id order by 2 desc;

1,195

社区成员

发帖
与我相关
我的任务
社区描述
其他数据库开发 Informix
社区管理员
  • Informix社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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