Home Forums SQL Server 2008 T-SQL (SS2K8) TSQL to join two tables to calculate the total based on the date range RE: TSQL to join two tables to calculate the total based on the date range

  • Hi All

    I need your help Im not much good in Tsql.

    I have 2 tables Rates and Records, I need to calculate the total cost for each country picking the rates based on the from and to dates on the RATE table.

    RATES table

    country fromdate todate costpersec

    =======================================================================

    USA 2012-10-10 00:00:00.000 2012-10-14 23:59:59.000 0.01

    USA 2012-10-15 00:00:00.000 NULL 0.02

    UK 2012-10-13 00:00:00.000 NULL 0.02

    RECORDS table

    Country date duration(sec)

    ===========================================

    USA 2012-10-14 10:00:00.000 10

    USA 2012-10-15 20:00:00.000 10

    UK 2012-10-13 20:00:00.000 10

    I want to calculate the TotalCost between '2012-10-10 00:00:00.000' and '2012-10-16 00:00:00.000 '

    So that picks up 0.01 rate between 10th and 14th and form 15th it should pick 0.02 and calculate the total.

    Country TotalCost

    ===================

    USA 0.3

    UK 0.2

    Thanks a lot in advance.