• dwain.c (2/6/2014)


    vip.blade (2/6/2014)


    Wow, thats amazing, thanks alot! I never thought of using a CASE-Statement in a SUM() function. That helped me very much!

    Is there also a way to use the FIRST_VALUE(order_date) window function this way (to show the first order_date after the 2012-01-15 and another column, which shows the first order_date without a date restriction for every customer)?

    Best regards!

    That does sound possible to me. I'd need some DDL and consumable sample data if you want a tested bit of code for it though.

    [This is one of the very few times I've ever posted a solution without DDL and sample data to test it with]

    I use the [AdventureWorks2012].[Sales].[SalesOrderHeader] table for my tests:

    SELECT [CustomerID]

    ,SalesOrderID

    ,[OrderDate]

    ,[TotalDue]

    ,SUM(CASE WHEN CAST([OrderDate] AS DATE) > '2008-02-20' THEN [TotalDue] ELSE 0 END) OVER (PARTITION BY CustomerID)

    FROM [AdventureWorks2012].[Sales].[SalesOrderHeader]

    Do you have this test database or should I provide you some more information?

    Best regards and thanks alot!