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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting at a sum

Status
Not open for further replies.

sagn

Programmer
Jun 7, 2001
166
US
I have a table that looks like this:

col1 col2 col3 col4
a 1 n1 3
a 1 n2 4
a 2 n1 5
b 1 n1 6
b 1 n2 7
b 2 n1 8
b 2 n2 9

I would like to insert rows that will be the sum of
col4, but for the same value of col1 and col2
but different values of col3.

The rows to be inserted would look like

a 1 n1n2 7
a 2 n1n2 5
b 1 n1n2 13
b 2 n1n2 17

I have tried joing the table to itself
and using group by but I don;t know how to get the sum
I want out of it since I have to use sum(col4)
but I really want the sum of t1.col4 and t2.col4.

This is a sample problem. The real problem will have more
than two values of col3 for each col1,col2 to base the sum on. Other wise I could do it simply
using t1.col4 + t2.col4


Thanks


ds


 
Never Mind


I'm currently banging my head on the
keyboard for being so dense!


insert into table
select
col1,
col2,
'n1n2',
sum(col4)

from table
group by col1,col2

will do the trick!

ds


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top