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

  • Sean, for what I understood, he might need to use the original query and filter the rows with a where clause.

    DECLARE @FromDate datetime = '20121013', @ToDate datetime = '20121015'

    SELECT rc.Country, SUM(r.CostPerSec*rc.Duration)

    FROM #Rates r

    JOIN #Records rc ON rc.Date >= r.FromDate

    AND rc.Date <= isnull(r.ToDate, rc.Date)

    AND r.Country = rc.Country

    WHERE rc.Date >= @FromDate

    AND rc.Date <=@ToDate

    GROUP BY rc.Country

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2