Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > IBM DB2 > Re: Knut's func...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 2 Topic 8850 of 9520
Post > Topic >>

Re: Knut's function "elelments"

by Lennart <Erik.Lennart.Jonsson@[EMAIL PROTECTED] > May 3, 2008 at 08:22 AM

On May 3, 12:17 am, "lenygold via DBMonster.com" <u41482@[EMAIL PROTECTED]
> 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
 




 2 Posts in Topic:
Knut's function "elelments"
"lenygold via DBMons  2008-05-02 22:17:14 
Re: Knut's function "elelments"
Lennart <Erik.Lennart.  2008-05-03 08:22:26 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Dec 2 20:19:17 CST 2008.