Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jparker
Posting Yak Master
118 Posts |
Posted - 2005-04-01 : 09:02:37
|
| I have three lines which belong to the same jobName | Job Id | Value | Type-----------------------------Jparker | 00001 | 100.99 | IJparker | 00001 | 50.00 | IJparker | 00001 | 75.80 | CMonkey | 00002 | 60.00 | IMonkey | 00002 | 20.00 | CWhat I want to do is have a sum of the three values to Jparker so that i returns 75.19 and Monkey returns 40.The logic is that I denotes invoice and C denotes credit note so in the first example 100.99 + 50.00 - 75.80How do I do this on the fly in sql i.e. make all Type where it is C a negative number and then sum that group? |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2005-04-01 : 09:09:00
|
| I think something likeSELECT [Name], sum (case when type = I then value when type = C then (-1 * value) else 0 end) as VALUEFROM yourtableGROUP BY [Name]Best not to use Name as a column name btw-------Moo. :) |
 |
|
|
jparker
Posting Yak Master
118 Posts |
Posted - 2005-04-01 : 09:30:01
|
| Cheers dude spot on |
 |
|
|
|
|
|