473,395 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Need Help with query

One column table:

ALL_SUM
--------------------------------------------------------------------
73237155+73240240+73243230+73249335

73237155+73240240+73246345

73237155+73240240+73246345+73249335
.................................................. ..................

I need to sum every 2 digits prior '+' sign plus last 2 digits:

For row 1: 55 + 40 + 30 + 35
For row 2: 55 + 40 + 45
For row 3: 55 + 40 + 45 + 35
Any idea how to do this?
This is realy urgent.
Thank's in advance.

--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....m-db2/200804/1

Apr 5 '08 #1
3 1908
On Apr 5, 3:28 am, "lenygold via DBMonster.com" <u41482@uwewrote:
One column table:

ALL_SUM
--------------------------------------------------------------------
73237155+73240240+73243230+73249335

73237155+73240240+73246345

73237155+73240240+73246345+73249335
.................................................. .................

I need to sum every 2 digits prior '+' sign plus last 2 digits:

For row 1: 55 + 40 + 30 + 35
For row 2: 55 + 40 + 45
For row 3: 55 + 40 + 45 + 35
Any idea how to do this?
This is realy urgent.
Thank's in advance.
See Knut's post for a function that will be of help here:

http://tinyurl.com/3hp487.

CREATE FUNCTION elements ( string varchar(100) )
RETURNS TABLE ( ordinal INTEGER, index INTEGER )
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
CONTAINS SQL
RETURN
WITH t(ordinal, index) AS
( VALUES ( 0, 0 )
UNION ALL
SELECT ordinal+1, COALESCE(NULLIF(
LOCATE('+', string, index+1), 0), LENGTH(string)
+1)
FROM t
-- to prevent a warning condition for infinite recursion
WHERE ordinal < 500 AND
LOCATE('+', string, index+1) <0 )
SELECT ordinal, index
FROM t

You will have to modify it slightly to fit your problem, Given that
the solution is easy:

db2 "create table T (all_sum varchar(50) not null primary key)"
db2 "insert into T values ('73237155+73240240+73243230+73249335')"
db2 "insert into T values ('73237155+73240240+73246345')"

db2 "select all_sum, sum(int(substr(all_sum, index-2 ,2))) from T,
TABLE (elements( T.all_sum )) x where index 0 group by all_sum"

ALL_SUM 2
-------------------------------------------------- -----------
73237155+73240240+73243230+73249335 125
73237155+73240240+73246345 95

2 record(s) selected.

Note that the sum is not correct since the last part of all_sum is not
taken into concideration, but you get the idea.

A word of caution, if there are duplicate all_sum (I added the primary
key to ensure that there is not) you will get strange results

HTH
/Lennart
Apr 5 '08 #2
On Apr 5, 5:54 am, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
On Apr 5, 3:28 am, "lenygold via DBMonster.com" <u41482@uwewrote:
One column table:
ALL_SUM
--------------------------------------------------------------------
73237155+73240240+73243230+73249335
73237155+73240240+73246345
73237155+73240240+73246345+73249335
.................................................. .................
I need to sum every 2 digits prior '+' sign plus last 2 digits:
For row 1: 55 + 40 + 30 + 35
For row 2: 55 + 40 + 45
For row 3: 55 + 40 + 45 + 35
Any idea how to do this?
This is realy urgent.
Thank's in advance.

See Knut's post for a function that will be of help here:

http://tinyurl.com/3hp487.

CREATE FUNCTION elements ( string varchar(100) )
RETURNS TABLE ( ordinal INTEGER, index INTEGER )
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
CONTAINS SQL
RETURN
WITH t(ordinal, index) AS
( VALUES ( 0, 0 )
UNION ALL
SELECT ordinal+1, COALESCE(NULLIF(
LOCATE('+', string, index+1), 0), LENGTH(string)
+1)
FROM t
-- to prevent a warning condition for infinite recursion
WHERE ordinal < 500 AND
LOCATE('+', string, index+1) <0 )
SELECT ordinal, index
FROM t

You will have to modify it slightly to fit your problem, Given that
the solution is easy:

db2 "create table T (all_sum varchar(50) not null primary key)"
db2 "insert into T values ('73237155+73240240+73243230+73249335')"
db2 "insert into T values ('73237155+73240240+73246345')"

db2 "select all_sum, sum(int(substr(all_sum, index-2 ,2))) from T,
TABLE (elements( T.all_sum )) x where index 0 group by all_sum"

ALL_SUM 2
-------------------------------------------------- -----------
73237155+73240240+73243230+73249335 125
73237155+73240240+73246345 95

2 record(s) selected.

Note that the sum is not correct since the last part of all_sum is not
taken into concideration, but you get the idea.

A word of caution, if there are duplicate all_sum (I added the primary
key to ensure that there is not) you will get strange results

HTH
/Lennart
On second thought, you can use the function as is by adding a '+' at
the end of all_sum as in:

db2 "select all_sum, sum(int(substr(all_sum, index-2 ,2))) from T,
TABLE (elements( rtrim(T.all_sum) || '+')) x where ordinal 0 group
by all_sum"

ALL_SUM 2
-------------------------------------------------- -----------
73237155+73240240+73243230+73249335 160
73237155+73240240+73246345 140
73237155+73240240+73246345+73249335 175

/L
Apr 5 '08 #3
Thank You Lennart. It is working perfect.

Lennart wrote:
One column table:
[quoted text clipped - 65 lines]
>HTH
/Lennart

On second thought, you can use the function as is by adding a '+' at
the end of all_sum as in:

db2 "select all_sum, sum(int(substr(all_sum, index-2 ,2))) from T,
TABLE (elements( rtrim(T.all_sum) || '+')) x where ordinal 0 group
by all_sum"

ALL_SUM 2
-------------------------------------------------- -----------
73237155+73240240+73243230+73249335 160
73237155+73240240+73246345 140
73237155+73240240+73246345+73249335 175

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

Apr 5 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
6
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18....
3
by: pw | last post by:
Hi, I am having a mental block trying to figure out how to code this. Two tables: "tblQuestions" (fields = quesnum, questype, question) "tblAnswers" (fields = clientnum, quesnum, questype,...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
10
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only...
7
by: Rnykster | last post by:
I know a little about Access and have made several single table databases. Been struggling for about a month to do a multiple table database with no success. Help! There are two tables. First...
3
by: pbd22 | last post by:
Hi. I need some help with structuring my query strings. I have a form with a search bar and some links. Each link is a search type (such as "community"). The HREF for the link's anchor looks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.