| Author |
Topic |
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2007-10-25 : 07:19:43
|
| Hi;We received a Payment from a customer on '10/10/2007 10:30:00'. i am trying to calculate the commission we would receive from that payment. the commission rate can be edited. so i have to find what the commission rate was when that payment was received.I have a CommisionAudit table that tracks changes in commission rate with the following values.ID | Commission Change | UpdatedOn----------------------------------------------1 | Change from 20->25 | 03/07/2007 09:00:00----------------------------------------------2 | Change from 25->35 | 10/09/2007 17:00:00----------------------------------------------3 | Change from 35->20 | 01/10/2007 16:00:00----------------------------------------------4 | Change from 20->26 | 11/10/2007 10:00:00----------------------------------------------with this payment, as the commission rate had been changed on 01/10/2007 it would obviously be 20%(ID 3). But I need to write sql to cover all eventualities i.e. Before the first and after the last. any help would be most welcome. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-25 : 07:43:26
|
Care to post any sample data and expected output? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2007-10-25 : 08:23:33
|
| Thanks for the reply:SampleDatePayment Date 1: '02/07/2007 10:00:00'Payment Date 2: '04/10/2007 10:00:00'Table – ContractAuditID| RateChangeDescription| ChangeDate---------------------------------------------------------------------------------------------711| Change Fee Rate: 22.00 -> 26.00| 2007-07-03 11:29:38.8571037| Change Fee Rate: 26.00 -> 35.50| 2007-10-01 15:10:34.9901043| Change Fee Rate: 35.50 -> 37.50| 2007-10-02 09:42:12.8371057| Change Fee Rate: 37.50 -> 35.00| 2007-10-08 10:02:16.010Expected Output : 22.00so as can be seen if we use payment1 commission rate would be 22.00, but if we use payment2 the commission rate would be 37.50. hope this makes it clearer |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-10-25 : 08:34:23
|
| I had a similar problem, effective dates with no expiration dates, and in SQL 2005 I did thiswith cte_Service AS(select 'aRank' = RANK() OVER (PARTITION BY skill order by effectivedate) ,skill,effectivedate,servicethresholdfrom results..ctlskillthresholds) SELECT a.skill,a.effectivedate,ISNULL(b.effectivedate,'12/31/2999'),a.servicethresholdFROM cte_service aLEFT join cte_service bon a.aRank <= b.arank+1and a.skill = b.skilland a.effectivedate < b.effectivedateWHERE a.skill = 841Hope this helpsJim |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-25 : 08:35:58
|
SELECT TOP 1 * FROM ContractAuditWHERE ChangeDate >= @SomeDateORDER BY ChangeDate E 12°55'05.25"N 56°04'39.16" |
 |
|
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2007-10-25 : 08:41:32
|
| i am also using sql 2005, but i cant quite follow your post jim. |
 |
|
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2007-10-25 : 09:00:12
|
| thanks for the input peso. i had similar sql, but with this sql if the payment date is after the very last date, it returns no rows, where i would then need it to go back to the last row in the table. |
 |
|
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2007-10-25 : 12:05:12
|
| Just wondering if this what i am after is possible, or would it be easier to created a separate table with a start & end date of when a com date was active. |
 |
|
|
KenW
Constraint Violating Yak Guru
391 Posts |
Posted - 2007-10-25 : 14:00:24
|
How about SELECT Max(ChangeDate) FROM ContractAuditWHERE PaymentDate >= ChangeDate |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-10-25 : 14:37:32
|
| slihp,the cte_service in my query is a common table expression which you can look up in Books On Line, as well as the rank function. What my query does is make an expiration date for each effective date, no when I query my date can be between an effective date and an expiration date. The real trouble you'll have is getting the rate out of your ratedescription field. Are the rates actually in their own field?Jim |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-10-25 : 15:01:51
|
| You can use this to query aginst your table with the rates in it ,or better yet, use it to create a better tables, with effectivedate and expiration datedeclare @table table (ID int, RateChangeDescription varchar(50),ChangeDate datetime)INSERT INTO @tableSELECT 711 ,' Change Fee Rate: 22.00 -> 26.00','2007-07-03 11:29:38.857' unionSELECT 1037 ,' Change Fee Rate: 26.00 -> 35.50','2007-10-01 15:10:34.990' unionSELECT 1043 ,' Change Fee Rate: 35.50 -> 37.50','2007-10-02 09:42:12.837' unionSELECT 1057 ,' Change Fee Rate: 37.50 -> 35.00','2007-10-08 10:02:16.010' ;with cte_rateas( Select Changedate,[aRank] = RANK() OVER(ORDER BY changedate asc) FROM @table) SELECT a.changedate,ISNULL(b.changedate,'12/31/2099')FROM cte_rate aLEFT JOIN cte_rate bON a.arank = b.arank-1 |
 |
|
|
slihp
Yak Posting Veteran
61 Posts |
Posted - 2007-10-26 : 04:47:03
|
| thanks allot Jim for the detailed reply. yes you are right, the real problem is getting the value. The values are not in another table, this table is the only one that has a historical view of the rate changes. I will persevere |
 |
|
|
|