Hi,

I'm having trouble writing a stored procedure in MS SQL SERVER. I have a table with 3 columns - REFERENCE, ID and AMOUNT.

What I'm trying to do is SUM the AMOUNT based on different ID's for each REFERENCE. So it would be something like

SUM(TBL.AMOUNT) WHERE ID IN '020'. '021', '022' AND REFERENCE = '001'

SUM(TBL.AMOUNT) WHERE ID IN '023', '024'' '025' AND REFERENCE = '001'

SUM(TBL.AMOUNT) WHERE ID IN '020'. '021', '022' AND REFERENCE = '002'

SUM(TBL.AMOUNT) WHERE ID IN '023', '024'' '025' AND REFERENCE = '002'

and so on (although there are many more combinations of id's and references)

Initially I went for a cursor to iterate through all the references in the table and do all the sums based on each of the id's but that didnt work, possibly becasue i was using a cursor, it just ran and ran. So I was hoping to find firstly a way that worked and secondly a more set based way of doing it.

Can anyone help?

Many thanks