Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > DB2

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-05-2008, 05:51 AM
lenygold via DBMonster.com
 
Posts: n/a
Default Knut's function "elelments"

Need help with Knut's function for summing values in rows
here is my example:
WITH T1 (C1) AS
(VALUES ('1'),
('1.1'),
('1.2.1.2'),
('1.10.1'),
('1.10.1.2.2'),
('1.22.99.1'),
('1.2'),
('1.3.3.7.4'),
('1.3.2.7'),
('1.4.1')),
T2(ALL_SUM) AS
(SELECT REPLACE(C1,'.','+') FROM T1)
select all_sum, sum(int(substr(all_sum, index-1,1))) FROM T2,
TABLE (elements(RTRIM(T2.all_sum)|| '+')) x
where ordinal > 0
group by all_sum
ORDER BY 1;

output:

ALL_SUM 2
---------- -----------
1 1
1+1 2
1+10+1 2
1+10+1+2+2 6
1+2 3
1+2+1+2 6
1+22+99+1 13
1+3+2+7 13
1+3+3+7+4 18
1+4+1 6

10 record(s) selected.
why it is summing wrong?

--
Message posted via http://www.dbmonster.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-05-2008, 05:51 AM
Lennart
 
Posts: n/a
Default Re: Knut's function "elelments"

On May 3, 12:17 am, "lenygold via DBMonster.com" <u41482@uwe> wrote:
> Need help with Knut's function for summing values in rows
> here is my example:
> WITH T1 (C1) AS
> (VALUES ('1'),
> ('1.1'),
> ('1.2.1.2'),
> ('1.10.1'),
> ('1.10.1.2.2'),
> ('1.22.99.1'),
> ('1.2'),
> ('1.3.3.7.4'),
> ('1.3.2.7'),
> ('1.4.1')),
> T2(ALL_SUM) AS
> (SELECT REPLACE(C1,'.','+') FROM T1)
> select all_sum, sum(int(substr(all_sum, index-1,1))) FROM T2,
> TABLE (elements(RTRIM(T2.all_sum)|| '+')) x
> where ordinal > 0
> group by all_sum
> ORDER BY 1;
>
> output:
>
> ALL_SUM 2
> ---------- -----------
> 1 1
> 1+1 2
> 1+10+1 2
> 1+10+1+2+2 6
> 1+2 3
> 1+2+1+2 6
> 1+22+99+1 13
> 1+3+2+7 13
> 1+3+3+7+4 18
> 1+4+1 6
>
> 10 record(s) selected.
> why it is summing wrong?


For one thing, you can't do:

int(substr(all_sum, index-1,1))

because there are numbers with more than 1 digit.

/Lennart

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 08:38 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62