SQL超级世界难题!

[复制链接]
查看11 | 回复9 | 2014-2-18 16:42:02 | 显示全部楼层 |阅读模式
一张表T有百万+条记录,有A,B,C三个字段,有个存储函数有需求要写下面三个select 语句。

select count(distinct A) , count (distinct B)from T whereC= c
select count(disintct B) , count (distinct C) from T where A = a
select count(disintct A) , count (distinct C) from T where B = b
这样的话要查三次百万记录的表,

, 显然是不可能的,时间太长

于是小弟想啊想:
select count(distinct A) , count (distinct B)from
(select * from T where (A=aorB=bor C=c) )whereC= c

select count(distinct B) , count (distinct C)from
(select * from T where (A=aorB=bor C=c) )whereA= a

select count(distinct A) , count (distinct C)from
(select * from T where (A=aorB=bor C=c) )whereB= b

有谁有办法把 上面三个SQL 的subquery整合的,做到只查一次百万记录的表?

小弟本来想用cursor,可是这不知道cursor怎么能写出 count(distinct A) 这样的功能,而且语句还不能太复杂,因为时间很重要,好郁闷啊! 谁有能力解开这个世界难题?
回复

使用道具 举报

千问 | 2014-2-18 16:42:02 | 显示全部楼层
你可以用结构函数看看
row_count() over()
可能快一点.
回复

使用道具 举报

千问 | 2014-2-18 16:42:02 | 显示全部楼层
老大,详细点吧?
回复

使用道具 举报

千问 | 2014-2-18 16:42:02 | 显示全部楼层
最初由 firemoth 发布
[B]一张表T有百万+条记录,有A,B,C三个字段,有个存储函数有需求要写下面三个select 语句。

select count(distinct A) , count (distinct B)from T whereC= c
select count(disintct B) , count (distinct C) from T where A = a
select count(disintct A) , count (distinct C) from T where B = b
这样的话要查三次百万记录的表,

, 显然是不可能的,时间太长

于是小弟想啊想:
select count(distinct A) , count (distinct B)from
(select * from T where (A=aorB=bor C=c) )whereC= c

select count(distinct B) , count (distinct C)from
(select * from T where (A=aorB=bor C=c) )whereA= a

select count(distinct A) , count (distinct C)from
(select * from T where (A=aorB=bor C=c) )whereB= b

有谁有办法把 上面三个SQL 的subquery整合的,做到只查一次百万记录的表?

小弟本来想用cursor,可是这不知道cursor怎么能写出 count(distinct A) 这样的功能,而且语句还不能太复杂,因为时间很重要,好郁闷啊! 谁有能力解开这个世界难题? [/B]


這也算難題,這個論壇裡就有相應的,自己找找吧,提示一下,用case就可以了!
回复

使用道具 举报

千问 | 2014-2-18 16:42:02 | 显示全部楼层
你看清楚题目了吗?讲的比唱得还好听。 要求只用一次查询做完三次count阿!case你个头
回复

使用道具 举报

千问 | 2014-2-18 16:42:02 | 显示全部楼层
最初由 firemoth 发布
[B]你看清楚题目了吗?讲的比唱得还好听。 要求只用一次查询做完三次count阿!case你个头 [/B]

某個條件成立=1,否則=0,再進行count,這樣應該知道了吧!
回复

使用道具 举报

千问 | 2014-2-18 16:42:02 | 显示全部楼层
最初由 firemoth 发布
[B]你看清楚题目了吗?讲的比唱得还好听。 要求只用一次查询做完三次count阿!case你个头 [/B]

create table aaa
(
a number,
b number,
c number
)
insert into aaa values (1,2,3);
insert into aaa values (3,2,1);
insert into aaa values (2,2,2);
commit;
SQL> select * from aaa;
A
B
C
---------- ---------- ----------
1
2
3
3
2
1
2
2
2
SQL> select sum(decode(a, 1, 1, 0)),
2 sum(decode(b, 0, 1, 0)),
3 sum(decode(c, 3, 1, 0))
4from aaa
5/
SQL> select sum(decode(a, 1, 1, 0)),
2 sum(decode(b, 0, 1, 0)),
3 sum(decode(c, 3, 1, 0))
4from aaa
5/
SQL> select sum(decode(a, 1, 1, 0)) a,
2 sum(decode(b, 0, 1, 0)) b,
3 sum(decode(c, 3, 1, 0)) c
4from aaa
5/
A
B
C
---------- ---------- ----------
1
0
1
回复

使用道具 举报

千问 | 2014-2-18 16:42:02 | 显示全部楼层
这样能算出distinct吗?
select count(distinct case when a=a then b else null)........................这样都罗列出来但是感觉不如
select count(distinct a) over(partition by c)..........................这样效率高
回复

使用道具 举报

千问 | 2014-2-18 16:42:02 | 显示全部楼层
最初由 oracledba 发布
[B]这样能算出distinct吗?
select count(distinct case when a=a then b else null)........................这样都罗列出来但是感觉不如
select count(distinct a) over(partition by c)..........................这样效率高 [/B]

不好意思,帥哥,太大意了,現修改如下:
SQL> select * from aaa;
A
B
C
---------- ---------- ----------
1
2
3
3
2
1
2
2
2
1
2
3
1
2
3
SQL> select sum(distinct decode(a, 1, 1, 0)) a,
2 sum(distinct decode(b, 0, 1, 0)) b,
3 sum(distinct decode(c, 3, 1, 0)) c
4from aaa
5/
A
B
C
---------- ---------- ----------
1
0
1
當然,用分析函數效果應該會高一點![/COLOR]
回复

使用道具 举报

千问 | 2014-2-18 16:42:02 | 显示全部楼层
最初由 HuiYi_love 发布
[B]
不好意思,帥哥,太大意了,現修改如下:
SQL> select * from aaa;
A
B
C
---------- ---------- ----------
1
2
3
3
2
1
2
2
2
1
2
3
1
2
3
SQL> select sum(distinct decode(a, 1, 1, 0)) a,
2 sum(distinct decode(b, 0, 1, 0)) b,
3 sum(distinct decode(c, 3, 1, 0)) c
4from aaa
5/
A
B
C
---------- ---------- ----------
1
0
1
當然,用分析函數效果應該會高一點![/COLOR] [/B]

好像更不对了,这样成了判断a,b,c是不是有一个值存在了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行