紧急求助一条sql语句的写法,高分送上

成都清香白莲 2008-04-12 03:23:57
字段1 字段2
A 00
A 01
B 00
B 01
C 00
C 02
D 00
D 02
E 01
E 02
上面是表的模拟数据,分别统计出字段2为(00,01)、(00,02)、(01,02)三种情况的数据,比如上表对应三种情况的正确结果应该是2、2、1,这个sql语句应该如何写,各位大虾帮帮忙
...全文
156 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
lz要干嘛?
lkj107 2008-04-17
  • 打赏
  • 举报
回复
with a as(select 字段1,字段2 from 表名 ),
b as (select 字段1,字段2 from 表名),
c as (select distinct a.字段1,a.字段2 as "22" ,b.字段2 as "33" ,a.字段2 |','|b.字段2 as "44",1 as "55" from a inner join b on(a.字段1=b.字段 1)),
select count("55") as “C”,"44" as "M"from c group by "44";
得出的结果会有 M C
00,01 2
00,02 2
01,02 1
01,00 2
02,00 2
00,01 1
lkj107 2008-04-17
  • 打赏
  • 举报
回复
with a as(select 字段1,字段2 from 表名 ),b as (select 字段1,字段2 from 表名),c as (select distinct a.字段1,a.字段2 as "22" ,b.字段2 as "33" ,a.字段2|b.字段2 as "44",1 as "55" from a inner join b on(a.字段1=b.字段1)),select count("55"),"44" group by "44";
cson_cson 2008-04-15
  • 打赏
  • 举报
回复
create table att(c1 varchar(10),c2 varchar(10));
insert into att values('A','00') ;
insert into att values('A','01') ;
insert into att values('B','00') ;
insert into att values('B','01') ;
insert into att values('C','00') ;
insert into att values('C','02') ;
insert into att values('D','00') ;
insert into att values('D','02') ;
insert into att values('E','01') ;
insert into att values('E','02') ;
SELECT a.c2||','||b.c2 m,count(*) c FROM ATT a,att b where a.c1 = b.c1 and a.c2<b.c2
group by a.c2||','||b.c2;
drop table att;
/*
M C
--------------------- ----------
00,01 2
00,02 2
01,02 1
*/
HelloWorld_001 2008-04-14
  • 打赏
  • 举报
回复
test(id,name)

Select Type,Count(*) From (
select id, substr(max(sys_connect_by_path(nm,',')),2) As Type from
(SELECT id, Name As nm, MIN(name) over(PARTITION BY Id) minnm
,(row_number() over(ORDER BY id, name)) + (dense_rank() over(ORDER BY id)) no
FROM test)
start with nm=minnm
connect by no-1 = prior no
group by Id )
Group By Type
HelloWorld_001 2008-04-14
  • 打赏
  • 举报
回复
先写下思路,一会再解决
order by 字段1,字段2

再根据结果集合,连接成
col1 col2
a 00,01
b 00,01
...
形式,

最后根据上个结果集
select col2,count(*) from ...
group by col2
成都清香白莲 2008-04-12
  • 打赏
  • 举报
回复
谢谢各位,应该是这个样子,我再试一下
海清 2008-04-12
  • 打赏
  • 举报
回复
参考一下吧:
SELECT COUNT(1),
NT.A2 || XT.A2 AA2
FROM (select MAX(t1.A2) A2,
T1.A1
from x_temp t1
GROUP BY T1.A1) XT,
(select MIN(t2.A2) A2,
T2.A1
from x_temp t2
GROUP BY T2.A1) NT
WHERE XT.A1 = NT.A1
GROUP BY NT.A2 || XT.A2
我感觉比较灵活,但是只适合字段1相同的只能有2个
niujunkai 2008-04-12
  • 打赏
  • 举报
回复
SELECT COUNT(tb.a2) num,
decode(tb.a1,1,'(00,01)',
2,'(00,02)',
3,'(01,02)')
FROM (
SELECT sum(a2) a1,sum(a2) a2
from x_temp
group by a1) tb
GROUP BY a1
hebo2005 2008-04-12
  • 打赏
  • 举报
回复
我写的SQL可以统计出来的
出来是一行
结果
(00,01) (00,02) (01,02)
2 2 1
你如果要分别统计,可以这样
select '(00,01)' name,count(1) num
from
(
select a,WMSYS.WM_CONCAT(b) b
from table
group by a) aa
where aa.b='00,01'
union all
select '(00,02)',count(1)
from
(
select a,WMSYS.WM_CONCAT(b) b
from table
group by a) aa
where aa.b='00,02'
union all
select '(01,02)',count(1)
from
(
select a,WMSYS.WM_CONCAT(b) b
from table
group by a) aa
where aa.b='01,02'

这样出来的结果就是
name num
(00,01) 2
(00,02) 2
(01,02) 1
成都清香白莲 2008-04-12
  • 打赏
  • 举报
回复
可能是我表达问题,我的意思是根据字段1分组,比如前两个A第一组,两个B是第二组,两个C是第三组,两个D是第四组,两个E是第五组,每一组都对应字段2的两个值,只可能是(00,01)或者(00,02)或者(01,02)这三种情况中的一种,我需要统计出这三种情况分别有多少组,比如第一组和第二组就是(00,01),统计数据就是2,第三组和第四组是(00,02),统计数据也是2,第五组是(01,02),统计数据是1,这样说能明白吗,可能说得有点罗唆
niujunkai 2008-04-12
  • 打赏
  • 举报
回复
没明白什么意思 取出来的值怎么出来的啊?
凤影 2008-04-12
  • 打赏
  • 举报
回复
题意不清,严重不清。
hebo2005 2008-04-12
  • 打赏
  • 举报
回复
select sum(decode(aa.b,'00,01',1,0)) "(00,01)",
sum(decode(aa.b,'00,02',1,0)) "(00,02)",
sum(decode(aa.b,'01,02',1,0)) "(01,02)"
from
(
select a,WMSYS.WM_CONCAT(b) b
from table
group by a) aa
成都清香白莲 2008-04-12
  • 打赏
  • 举报
回复
votoon兄弟,你可能没有明白我的意思
votoon 2008-04-12
  • 打赏
  • 举报
回复
select 字段1,count(字段1) from table
group by 字段1
成都清香白莲 2008-04-12
  • 打赏
  • 举报
回复
谁能帮我解决一下

17,090

社区成员

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

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