Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conditional sum over several fields, without if?

Status
Not open for further replies.

kittyyo

Programmer
Oct 17, 2001
89
AT
Hi all!

I'm trying to sum up values from several columns, that way:

id | val1 | val2 | val3 | type1 | type2 | type3 |

val* are double columns, type* are integer columns.

I'd like to select the sum of all val_i columns where the value of the type_i column = 4, so for this expample row

id | val1 | val2 | val3 | type1 | type2 | type3
-----------------------------------------------
1 | 0.1 | 0.2 | 0.3 | 1 | 4 | 4

the resulting sum of val* columns would be 0.5, and the result set would be

id | sum
--------
1 | 0.5

I know this is a table structure problem (I didnt design the table), and please consider that I'm using Firebird and therefore do not have a function like IF().

Any clue how I could select this?

Thanks a lot,
Anne
 
select (case t1 when 4 then d1 else 0 end) +
(case t2 when 4 then d2 else 0 end) +
(case t3 when 4 then d3 else 0 end) as max1
from t1
where dx = valx and tx = typex

adjust according to your table name and output expectations.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Thank you all for the discussion! As I found out, Firebird does support CASE. No, I didn't find it in any documentation either, I tried it and it worked (FB 1.5).

The only firebird documentation I know is on the IBPhoenix developer CD... which one has to pay for :-(

Thanks again,
Anne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top