多列汇总,不知道怎么写,大家进来帮我看看啊

jin20000 2010-06-09 08:37:08
有表a,有如下列

id bm lx lx1cs lx2cs lx3cs lx4cs
1 1 lx1 0 0 0 0
2 1 lx1 1 0 0 0
3 2 lx2 0 0 0 0
4 2 lx2 0 1 0 0
5 2 lx3 0 0 0 0
6 2 lx3 0 0 1 0
7 2 lx4 0 0 0 0
8 2 lx4 0 0 0 1
9 2 lx1 1 0 0 0
10 1 lx2 0 0 0 0
11 1 lx3 0 0 1 0
12 1 lx4 0 0 0 1
13 1 lx1 0 0 0 0

想要得到按bm,lx,分组得到对应类型的,如lx是lx1,则看lx1cs值是否为1,为1则累计1条
如下结果

bm lx count
1 lx1 2
1 lx2 0
1 lx3 1
1 lx4 1
2 lx1 1
2 lx2 1
2 lx3 1
2 lx4 1


...全文
110 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jin20000 2010-06-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tangren 的回复:]

SQL code
SQL> --如果lx不固定,可能要使用存储过程来拼接动态SQL来执行
SQL> with a as (
2 select 1 id,1 bm, 'lx1' lx, 0 lx1cs,0 lx2cs, 0 lx3cs, 0 lx4cs from dual union all
3 select 2 id,1 bm, 'lx1' lx, 1 lx1cs,0 ……
[/Quote]
tangren,lx不止0和1两种状态呢
tangren 2010-06-10
  • 打赏
  • 举报
回复
SQL> --如果lx不固定,可能要使用存储过程来拼接动态SQL来执行
SQL> with a as (
2 select 1 id,1 bm, 'lx1' lx, 0 lx1cs,0 lx2cs, 0 lx3cs, 0 lx4cs from dual union all
3 select 2 id,1 bm, 'lx1' lx, 1 lx1cs,0 lx2cs, 0 lx3cs, 0 lx4cs from dual union all
4 select 3 id,2 bm, 'lx2' lx, 0 lx1cs,0 lx2cs, 0 lx3cs, 0 lx4cs from dual union all
5 select 4 id,2 bm, 'lx2' lx, 0 lx1cs,1 lx2cs, 0 lx3cs, 0 lx4cs from dual union all
6 select 5 id,2 bm, 'lx3' lx, 0 lx1cs,0 lx2cs, 0 lx3cs, 0 lx4cs from dual union all
7 select 6 id,2 bm, 'lx3' lx, 0 lx1cs,0 lx2cs, 1 lx3cs, 0 lx4cs from dual union all
8 select 7 id,2 bm, 'lx4' lx, 0 lx1cs,0 lx2cs, 0 lx3cs, 0 lx4cs from dual union all
9 select 8 id,2 bm, 'lx4' lx, 0 lx1cs,0 lx2cs, 0 lx3cs, 1 lx4cs from dual union all
10 select 9 id,2 bm, 'lx1' lx, 1 lx1cs,0 lx2cs, 0 lx3cs, 0 lx4cs from dual union all
11 select 10 id,1 bm, 'lx2' lx, 0 lx1cs,0 lx2cs, 0 lx3cs, 0 lx4cs from dual union all
12 select 11 id,1 bm, 'lx3' lx, 0 lx1cs,0 lx2cs, 1 lx3cs, 0 lx4cs from dual union all
13 select 12 id,1 bm, 'lx4' lx, 0 lx1cs,0 lx2cs, 0 lx3cs, 1 lx4cs from dual union all
14 select 13 id,1 bm, 'lx1' lx, 0 lx1cs,0 lx2cs, 0 lx3cs, 0 lx4cs from dual)
15 SELECT bm,
16 lx,
17 CASE
18 WHEN lx = 'lx1' THEN
19 SUM(lx1cs)
20 WHEN lx = 'lx2' THEN
21 SUM(lx2cs)
22 WHEN lx = 'lx3' THEN
23 SUM(lx3cs)
24 WHEN lx = 'lx4' THEN
25 SUM(lx4cs)
26 END cnt
27 FROM a
28 GROUP BY bm, lx;

BM LX CNT
---------- --- ----------
1 lx1 1
2 lx4 1
2 lx2 1
2 lx1 1
1 lx2 0
2 lx3 1
1 lx4 1
1 lx3 1

已选择8行。

SQL>
minoboy 2010-06-10
  • 打赏
  • 举报
回复
楼上还没有理解题意啊
header230 2010-06-10
  • 打赏
  • 举报
回复
select bm ,lx ,count(*) from table_Name
where lx1cs = 1 and lx = 'lx1'
group by bm,lx


需要那么复杂吗,这个语句没有测试过,基本思路就是这样的。
jessica0403 2010-06-10
  • 打赏
  • 举报
回复
lx里面的值是否有重复?
minoboy 2010-06-09
  • 打赏
  • 举报
回复
select bm,lx,sum(cnt) from
(select
bm, lx,case when lx=lx1 then lx1cs
when lx=lx2 then lx2cs
when lx=lx3 then lx3cs
when lx=lx4 then lx4cs else 0 end cnt from 表A) 表B

group by bm,lx
jin20000 2010-06-09
  • 打赏
  • 举报
回复
难道都这么早就睡了?
来人啦,各位大虾帮帮我啊
jin20000 2010-06-09
  • 打赏
  • 举报
回复
哎,怎么没人呀
jin20000 2010-06-09
  • 打赏
  • 举报
回复
感觉是多行多列转成一列

17,377

社区成员

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

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