gy
2 楼
B*g
3 楼
问题:
1. Fibonacci Series 已经存在了还是要用sql 产生?
2. single query可以用几层select?
明天上午搞一个oracle的,然后看能不能换成t-sql
【在 y****w 的大作中提到】
: http://beyondrelational.com/blogs/tc/archive/2009/12/27/tsql-challenge-20-about-finding-repeating-digits-on-on-fibonacci-sequence-numbers.aspx
1. Fibonacci Series 已经存在了还是要用sql 产生?
2. single query可以用几层select?
明天上午搞一个oracle的,然后看能不能换成t-sql
【在 y****w 的大作中提到】
: http://beyondrelational.com/blogs/tc/archive/2009/12/27/tsql-challenge-20-about-finding-repeating-digits-on-on-fibonacci-sequence-numbers.aspx
y*w
6 楼
5个,用的是字符串函数。
后来改了下用数字,两层cte,7次(sql server则多一次,替换values)出现select,在我破x41上快了一倍,大约0.07s.
我贴个db2的,替换下函数就可以在sql server上跑。
你把oracle的做法贴一下吧,我不会oracle,今天下午试了好像oracle的cte差点,但是connect by不错。
with fibo(d1,d2,n) as
(
values (cast(1 as bigint), cast(1 as bigint),1)
union all
select d2, d1+d2, n+1 from fibo where n+1 < 92
),
f1(fibo,step,digit,cnt)as
(
select d2, d2/10, mod(d2,10),
case when mod(mod(d2,100),11) = 0 then 1 else 0 end from fibo
union all
select fibo, step/10, mod(step,10),
case when
【在 B*****g 的大作中提到】
: 有几个select?
后来改了下用数字,两层cte,7次(sql server则多一次,替换values)出现select,在我破x41上快了一倍,大约0.07s.
我贴个db2的,替换下函数就可以在sql server上跑。
你把oracle的做法贴一下吧,我不会oracle,今天下午试了好像oracle的cte差点,但是connect by不错。
with fibo(d1,d2,n) as
(
values (cast(1 as bigint), cast(1 as bigint),1)
union all
select d2, d1+d2, n+1 from fibo where n+1 < 92
),
f1(fibo,step,digit,cnt)as
(
select d2, d2/10, mod(d2,10),
case when mod(mod(d2,100),11) = 0 then 1 else 0 end from fibo
union all
select fibo, step/10, mod(step,10),
case when
【在 B*****g 的大作中提到】
: 有几个select?
t*9
7 楼
这种算法一般在application 层面实现吧,为什么一定要在db实现?
B*g
8 楼
Oracle 10g+, 那个expected output不对呀
WITH t1 AS (
SELECT f, l-1 l
FROM DUAL
CONNECT BY LEVEL <= 10
MODEL RETURN ALL ROWS
PARTITION BY (LEVEL l)
DIMENSION BY (0 d)
MEASURES (0 f)
RULES
ITERATE (92)
(f[iteration_number] = DECODE (iteration_number, 0, 1, 1, 0, f[iteration_num
ber - 2]) + NVL (f[iteration_number - 1], 0))),
t2 AS (
SELECT f, SUM(SIGN(INSTR(f, l, 1 ,2))) c, ROW_NUMBER() OVER (PARTITION BY SU
M(SIGN(INSTR(f, l, 1 ,2))) ORDER BY f) r
FROM t1
GROUP BY f)
SELECT f, c
FROM t2
WHERE r <= 5
AN
【在 y****w 的大作中提到】
: 5个,用的是字符串函数。
: 后来改了下用数字,两层cte,7次(sql server则多一次,替换values)出现select,在我破x41上快了一倍,大约0.07s.
: 我贴个db2的,替换下函数就可以在sql server上跑。
: 你把oracle的做法贴一下吧,我不会oracle,今天下午试了好像oracle的cte差点,但是connect by不错。
: with fibo(d1,d2,n) as
: (
: values (cast(1 as bigint), cast(1 as bigint),1)
: union all
: select d2, d1+d2, n+1 from fibo where n+1 < 92
: ),
WITH t1 AS (
SELECT f, l-1 l
FROM DUAL
CONNECT BY LEVEL <= 10
MODEL RETURN ALL ROWS
PARTITION BY (LEVEL l)
DIMENSION BY (0 d)
MEASURES (0 f)
RULES
ITERATE (92)
(f[iteration_number] = DECODE (iteration_number, 0, 1, 1, 0, f[iteration_num
ber - 2]) + NVL (f[iteration_number - 1], 0))),
t2 AS (
SELECT f, SUM(SIGN(INSTR(f, l, 1 ,2))) c, ROW_NUMBER() OVER (PARTITION BY SU
M(SIGN(INSTR(f, l, 1 ,2))) ORDER BY f) r
FROM t1
GROUP BY f)
SELECT f, c
FROM t2
WHERE r <= 5
AN
【在 y****w 的大作中提到】
: 5个,用的是字符串函数。
: 后来改了下用数字,两层cte,7次(sql server则多一次,替换values)出现select,在我破x41上快了一倍,大约0.07s.
: 我贴个db2的,替换下函数就可以在sql server上跑。
: 你把oracle的做法贴一下吧,我不会oracle,今天下午试了好像oracle的cte差点,但是connect by不错。
: with fibo(d1,d2,n) as
: (
: values (cast(1 as bigint), cast(1 as bigint),1)
: union all
: select d2, d1+d2, n+1 from fibo where n+1 < 92
: ),
y*w
10 楼
学习了,
btw,你是指最后那一个值? 如果一个数字有两个连续出现,按这次的规则只计数一次。
昨天在台破机器这样按数值算快一倍,今天在主力机器的sql/db2,aix的db2测的结果都
差于substr的方案。
【在 B*****g 的大作中提到】
: Oracle 10g+, 那个expected output不对呀
: WITH t1 AS (
: SELECT f, l-1 l
: FROM DUAL
: CONNECT BY LEVEL <= 10
: MODEL RETURN ALL ROWS
: PARTITION BY (LEVEL l)
: DIMENSION BY (0 d)
: MEASURES (0 f)
: RULES
btw,你是指最后那一个值? 如果一个数字有两个连续出现,按这次的规则只计数一次。
昨天在台破机器这样按数值算快一倍,今天在主力机器的sql/db2,aix的db2测的结果都
差于substr的方案。
【在 B*****g 的大作中提到】
: Oracle 10g+, 那个expected output不对呀
: WITH t1 AS (
: SELECT f, l-1 l
: FROM DUAL
: CONNECT BY LEVEL <= 10
: MODEL RETURN ALL ROWS
: PARTITION BY (LEVEL l)
: DIMENSION BY (0 d)
: MEASURES (0 f)
: RULES
B*g
11 楼
看错了,重复数要连着。改一下,明天上班才能测试
WITH t1 AS (
SELECT f, TO_CHAR(l-1)||TO_CHAR(l-1) l
FROM DUAL
CONNECT BY LEVEL <= 10
MODEL RETURN ALL ROWS
PARTITION BY (LEVEL l)
DIMENSION BY (0 d)
MEASURES (0 f)
RULES
ITERATE (92)
(f[iteration_number] = DECODE (iteration_number, 0, 1, 1, 0, f[iteration_num
ber - 2]) + NVL (f[iteration_number - 1], 0))),
t2 AS (
SELECT f, SUM(SIGN(INSTR(f, l))) c, ROW_NUMBER() OVER (PARTITION BY SUM(SIGN
(INSTR(f, l))) ORDER BY f) r
FROM t1
GROUP BY f)
SELECT f, c
FROM t2
WHERE r <= 5
A
【在 y****w 的大作中提到】
: 学习了,
: btw,你是指最后那一个值? 如果一个数字有两个连续出现,按这次的规则只计数一次。
: 昨天在台破机器这样按数值算快一倍,今天在主力机器的sql/db2,aix的db2测的结果都
: 差于substr的方案。
WITH t1 AS (
SELECT f, TO_CHAR(l-1)||TO_CHAR(l-1) l
FROM DUAL
CONNECT BY LEVEL <= 10
MODEL RETURN ALL ROWS
PARTITION BY (LEVEL l)
DIMENSION BY (0 d)
MEASURES (0 f)
RULES
ITERATE (92)
(f[iteration_number] = DECODE (iteration_number, 0, 1, 1, 0, f[iteration_num
ber - 2]) + NVL (f[iteration_number - 1], 0))),
t2 AS (
SELECT f, SUM(SIGN(INSTR(f, l))) c, ROW_NUMBER() OVER (PARTITION BY SUM(SIGN
(INSTR(f, l))) ORDER BY f) r
FROM t1
GROUP BY f)
SELECT f, c
FROM t2
WHERE r <= 5
A
【在 y****w 的大作中提到】
: 学习了,
: btw,你是指最后那一个值? 如果一个数字有两个连续出现,按这次的规则只计数一次。
: 昨天在台破机器这样按数值算快一倍,今天在主力机器的sql/db2,aix的db2测的结果都
: 差于substr的方案。
gy
13 楼
都是牛银呀
相关阅读
哪位先进用过MYSQL里面的LOCK TABLEHelp! How to deal with SYNONYME in MySQL[转载] Re: No reason to stick with IEhow to hot back mysql?pull-down menu in Access formsSQL 怎样和empty string 做比较阿In SQLPLUS, how to repeat typed commandaccess db最多能有多少record呢?谁能介绍一下spatial database?[转载] 有人8月份参加KDD会议(在Seattle)A question about recursive queryApache/MySQL怎末优化一个stored procedure ?Please recommend Access book Thanks.Re: In SQLPLUS, how to repeat typed commHELP!!! PL/SQL write to a filemysql -- could not use the databaseHow to improve performance of Database?请问MYSQL JDBC如何获得插入记录的编号请问个从windows通过jdbc连server的问题