473,396 Members | 2,057 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,396 software developers and data experts.

Sum result of a Select

In the database i have a table with this information:

key_id =1
key_desc =43+34+22+12

I want sum the values in key_desc. Something like:

SELECT key_desc FROM table

But the result of the select was "111" and not "43+34+22+12".

Is this posible?

Jun 21 '07 #1
6 3375
On Jun 21, 12:44 pm, Apaxe <apaxe2000Milh...@gmail.comwrote:
In the database i have a table with this information:

key_id =1
key_desc =43+34+22+12

I want sum the values in key_desc. Something like:

SELECT key_desc FROM table

But the result of the select was "111" and not "43+34+22+12".

Is this posible?
Are you saying that the key_desc is a char() field with the value
"43+34+22+12" in it? If that's the case then this IS possible, but
the code is gonna make your head hurt a little bit :) Let me know and
I'll be happy to work with you on it.

Joshua

Jun 21 '07 #2
Apaxe (ap**************@gmail.com) writes:
In the database i have a table with this information:

key_id =1
key_desc =43+34+22+12

I want sum the values in key_desc. Something like:

SELECT key_desc FROM table

But the result of the select was "111" and not "43+34+22+12".

Is this posible?
I will have to confess that I understand about zero of your post. It would
certainly help if you could clarify what you are up to. I would suggest that
you post:

o CREATE TABLE statement for your table.
o INSERT statements with sample data.
o The desired result given the sample.
o A short narrative that explains the business problem you are trying
to address.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 21 '07 #3
On Jun 21, 8:05 pm, "jlaust...@gmail.com" <jlaust...@gmail.comwrote:
On Jun 21, 12:44 pm,Apaxe<apaxe2000Milh...@gmail.comwrote:
In the database i have a table with this information:
key_id =1
key_desc =43+34+22+12
I want sum the values in key_desc. Something like:
SELECT key_desc FROM table
But the result of the select was "111" and not "43+34+22+12".
Is this posible?

Are you saying that the key_desc is a char() field with the value
"43+34+22+12" in it? If that's the case then this IS possible, but
the code is gonna make your head hurt a little bit :) Let me know and
I'll be happy to work with you on it.

Joshua
Hello, Joshua.

Yes, key_desc is a char() field. And i need to extract de sum of the
string. In this case, the value "111".

Thank you.

Jul 3 '07 #4
On Jul 2, 8:11 pm, Apaxe2000 <Apaxe2...@gmail.comwrote:
Yes, key_desc is a char() field. And i need to extract de sum of the
string. In this case, the value "111".
Simply create a temporary table (name it, for example "TheCalculator")
with only ONE record in it (Whatever...) and do something like:

SELECT (123+4)*2 FROM TheCalculator

That'll give you the result of any calculation

Piero

Jul 6 '07 #5

Apaxe :
In the database i have a table with this information:

key_id =1
key_desc =43+34+22+12

I want sum the values in key_desc. Something like:

SELECT key_desc FROM table

But the result of the select was "111" and not "43+34+22+12".

Is this posible?

1. Assuming you have a table and insertions like following ones
(using T-SQL syntax):
CREATE TABLE Test(key_id int, key_desc varchar(100))
INSERT INTO Test SELECT 1, '43+34+22+12' UNION SELECT 2,'34+56+78'

2. Assuming you have a following sequenve view (again using T-SQL)
CREATE VIEW [dbo].[Sequence](seq)
AS
SELECT hundred * 100 + ten * 10 + unit + 1
FROM (SELECT 0 UNION SELECT 1 UNION
SELECT 2 UNION SELECT 3 UNION
SELECT 4 UNION SELECT 5 UNION
SELECT 6 UNION SELECT 7 UNION
SELECT 8 UNION SELECT 9) AS Units(unit)
CROSS JOIN
(SELECT 0 UNION SELECT 1 UNION
SELECT 2 UNION SELECT 3 UNION
SELECT 4 UNION SELECT 5 UNION
SELECT 6 UNION SELECT 7 UNION
SELECT 8 UNION SELECT 9) AS Tens(ten)
CROSS JOIN
(SELECT 0 UNION SELECT 1 UNION
SELECT 2 UNION SELECT 3 UNION
SELECT 4 UNION SELECT 5 UNION
SELECT 6 UNION SELECT 7 UNION
SELECT 8 UNION SELECT 9) AS Hundreds(hundred)

3. Assuming you have only '+' operation, you can use a select like
following
SELECT Parsed.key_id,
SUM(Parsed.key_vls) sums
FROM (SELECT T.key_id,
CAST (SUBSTRING ('+' + T.key_desc + '+', MAX(S1.seq +
1), (S2.seq - MAX(S1.seq + 1))) AS INTEGER)
FROM Test AS T,
Sequence AS S1,
Sequence AS S2
WHERE SUBSTRING ('+' + T.key_desc + '+', S1.seq, 1) = '+'
AND SUBSTRING ('+' + T.key_desc + '+', S2.seq, 1) = '+'
AND S1.seq < S2.seq
AND S2.seq <= LEN(T.key_desc) + 2
GROUP BY T.key_id, T.key_desc, S2.seq
) AS Parsed(key_id, key_vls)
GROUP BY Parsed.key_id
If you have more operators in your string you have to add to this
query additional checks.
Idea is got from the book "JOE CELKO'S SQL PROGRAMMING STYLE".

Hope this helps,
Andriy

Jul 10 '07 #6
On Jun 21, 8:44 pm, Apaxe <apaxe2000Milh...@gmail.comwrote:
In the database i have a table with this information:

key_id =1
key_desc =43+34+22+12

I want sum the values in key_desc. Something like:

SELECT key_desc FROM table

But the result of the select was "111" and not "43+34+22+12".

Is this posible?
This looks similar to one of your other problems see
http://groups.google.com/group/micro...4b73956cc1b7f8

How come you did not clean the data before it got into the database?

Assuming max 4 values (columns) to sum

CREATE TABLE Test(key_id int, key_desc varchar(100))
INSERT INTO Test SELECT 1, '43+34+22+12' UNION SELECT 2,'34+56+78'

select
convert(int,isnull(PARSENAME(replace(key_desc,'+', '.'),4),0))
+convert(int,isnull(PARSENAME(replace(key_desc,'+' ,'.'),3),0))
+convert(int,isnull(PARSENAME(replace(key_desc,'+' ,'.'),2),0))
+convert(int,isnull(PARSENAME(replace(key_desc,'+' ,'.'),1),0))
from Test

or else you can do:

declare c1 cursor for select convert(varchar(9),key_id),key_desc from
Test
open c1
declare @id int, @idc varchar(9), @key_desc varchar(100)
select key_id, 0 'total' into #t1 from Test where 1=2
fetch c1 into @idc, @key_desc
while @@FETCH_STATUS = 0
begin
exec ( 'insert into #t1 select key_id, '+@key_desc+' from Test where
key_id='+@idc)
fetch c1 into @idc, @key_desc
end
close c1
select * from #t1
drop table #t1
deallocate c1
drop TABLE Test

Jul 10 '07 #7

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

Similar topics

2
by: PYCTAM | last post by:
Hi, I have data stored as in below sample : -------------------------------+---------------------------------+-------------- DateBegin | DateEnd | Rate...
3
by: uthuras | last post by:
hi all, One of my customer having warehouse database running on DB2. Recently when they execute the following query at db2 command line the exepected result take ages before return result ...
0
by: Florian | last post by:
Hi Uthuras, it would be helpful to see the access path. However sometimes it makes sense to rewrite a "not in" to gain better access paths: I tried the following - hopefully similar -...
1
by: Alex | last post by:
Hello I need to select records from table1 where column1 is a part of column2 in table2, but with this query I can't obtain the desired result: SELECT * from table1 a, table2 b where b.column2...
2
by: Wei Wang | last post by:
Hi, I want to do a select in dynamic command, something like: TRIGGER FUNCTION DECLARE table_name_suffix text; temp_result RECORD; temp_result2 RECORD;
6
by: Bob Stearns | last post by:
I am getting unwanted duplicate rows in my result set, so I added the DISTINCT keyword to my outermost SELECT. My working query then returned the following message: DB2 SQL error: SQLCODE: -214,...
2
by: studentfromhell | last post by:
These mesages appeared... what should I do? script is below the error messages... Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in...
1
by: Akhenaten | last post by:
Any suggestion as to why my fetch_array is bad? Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jkokko/public_html/ask/test.php on line 11...
2
by: Prakashakarapu | last post by:
Hi, I am writing a query to get data from the SQL server 2005, i am having a record in the database but i am getting null records can any one help me regarding this i am posting the query below.....
5
by: agarwalsrushti | last post by:
Hi, Im just stuck up with a small query. In my database ive a table called specialization which is dependent on qualification. In specialization i ve 3 fields: QualificationId,...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.