| Author |
Topic |
|
rkumar28
Starting Member
49 Posts |
Posted - 2005-09-28 : 13:24:02
|
| Hi,I am new to sql server. I have a situation where I have to subtract the values from one column. There is a function "SUM" to do the Addition. Is there a function to do the subtraction.e.g.: I have a field called COL1....and I have to subtract the values in this column grouping it by MONTH.I will really appreciate any advice in this regards.ThanksRaj |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
rkumar28
Starting Member
49 Posts |
Posted - 2005-09-28 : 13:45:46
|
| I want to subtract them from the values of same column. Let say there is a table A. It has two columns called Month(date) and Pol1(Integer).These are the records in the table...Month Pol1January 10000January 30000February 10000February 20000There can be many records in a month. I have shown only two records above.I am trying to subtract the values of column Pol1 grouping it by Month.I want to get the result something like: January 20000 .....(I got this by subtrating 30000 with 10000 for January month)February 10000 .....(I got this by subtrating 20000 with 10000 for January month)Is there a funtion to subtract like we have Sum function.ThanksRaj |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-09-28 : 13:56:01
|
| Remember that A-B is equal to A + (-B) which is equal to A + (-1 * B) which is equal to the sum of A and -1*B ....Does that clue help ? |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
rkumar28
Starting Member
49 Posts |
Posted - 2005-09-28 : 14:30:18
|
| Hi,I am trying to generate a report for the end users. I have a Insurance policies and these policies has Annutization Amount. These amounts varies for each policies in a month.E.g.: There is a policy 123456 and its anutization amount changes couple of times in month.....Below I have given a sample for the month of January.....for one policyDATE…………….POLICY…….AMOUNT01-02-2005………123456………..20001-05-2005………123456……….. 50 The amount changes01-15-2005………123456………..200 The amount changes again01-20-2005……….123456……….300 The amount changes againBasically end user wants to see the result of 300 -200 -50 -200= -150 for the month of January. The subtraction has to be done from the latest date to an earlier date for a particular policy for a month.My problem is I am not able to figure out how to do the subtraction within one column.ThanksRaj |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-09-28 : 14:41:03
|
quote: Originally posted by jsmith8858 Remember that A-B is equal to A + (-B) which is equal to A + (-1 * B) which is equal to the sum of A and -1*B ....Does that clue help ?
In other words, some values need to make negative, sum left positive, and then simply use SUM() ....In your case, leave the value with the highest date per policy per month positive, multiply the rest by -1, and then add them all up:SELECT Policy, Year(Date), Month(Date), SUM(case when Date= LastDate then 1 else -1 end * Amount) as AmountFROM( SELECT Policy, Date, (select Max(Date) from YourTable B where A.Policy = B.Policy and Year(A.Date) = Year(B.Date) and Month(A.Date) = Month(A.Date) ) as LastDate, Amount FROM YourTable A) tmpGROUP BY Policy, Year(Date), Month(Date) |
 |
|
|
rkumar28
Starting Member
49 Posts |
Posted - 2005-09-28 : 17:37:09
|
| Thanks a bunch. This logic worked....Raj |
 |
|
|
|