Dates

  • Something like this?

    Select 'CURRENT' as "Year", sum(orders) from dbo.Orders where OrderDate between @DateStart and @DateEnd

    UNION ALL

    Select 'LAST' as "Year", sum(orders) from dbo.Orders where OrderDate between

    DATEADD(yyyy, -1, @DateStart) and

    DATEADD(yyyy, -1, @DateEnd)

  • Or, if you need your results on the same row, then...

    select sum(case when OrderDate between @DateStart and @DateEnd then Orders else null end) as ThisYearOrders,

    sum(case when OrderDate between DATEADD(yyyy, -1, @DateStart) and DATEADD(yyyy, -1, @DateEnd) then Orders else null end) as LastYearOrders

    from dbo.Orders

    where (OrderDate between @DateStart and @DateEnd)

    or (OrderDate between DATEADD(yyyy, -1, @DateStart) and DATEADD(yyyy, -1, @DateEnd))

     

Viewing 2 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply