Comparing figures based on relative date

  • Agree in this scenario it's for a BI application and I doubt it can handle self joins, throw in the fact it's been dimensionally modelled and it adds it a fair bit more complexity. I literally took it back to bare bones in order to try and break it down a bit then build it back up.

    I'm too spoiled now with ETL tools and not close enough to the real action anymore ๐Ÿ˜€

    Again thanks for the responses much appreciated.

  • For best performance,

    Cluster the table by

    LAST_DAY_OF_MONTH

    and not the dopey identity column. If you still need the identity (??), you can leave it as the nonclustered pk.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • ChrisM@Work (2/11/2013)


    Something like this?

    DROP TABLE #mytable

    CREATE TABLE #mytable

    (P_KEY INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,

    LAST_DAY_OF_MONTH DATETIME,

    Value int,

    PreviousYearValue INT)

    INSERT INTO #mytable

    VALUES('20130131',20,NULL),('20120131',5,NULL),('20130331',40,NULL),('20120331',2,NULL)

    SELECT

    ty.*,

    ly.*

    FROM #mytable ty

    LEFT JOIN #mytable ly

    ON ly.LAST_DAY_OF_MONTH = DATEADD(yy,-1,ty.LAST_DAY_OF_MONTH)

    This may be an alternative:

    CREATE TABLE #mytable

    (P_KEY INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,

    LAST_DAY_OF_MONTH DATETIME,

    Value int,

    PreviousYearValue INT)

    INSERT INTO #mytable

    VALUES('20130131',20,NULL),('20120131',5,NULL),('20130331',40,NULL),('20120331',2,NULL)

    SELECT P_Key=MIN(P_Key), LAST_DAY_OF_MONTH, Value=SUM(Value), PreviousYearValue=SUM(PreviousYearValue)

    FROM (

    SELECT P_Key, LAST_DAY_OF_MONTH, Value, PreviousYearValue

    FROM #mytable

    UNION ALL

    SELECT P_Key, DATEADD(year, 1, LAST_DAY_OF_MONTH), NULL, Value

    FROM #mytable) a

    GROUP BY LAST_DAY_OF_MONTH

    HAVING SUM(Value) IS NOT NULL

    DROP TABLE #mytable


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • ScottPletcher (2/11/2013)


    For best performance,

    Cluster the table by

    LAST_DAY_OF_MONTH

    and not the dopey identity column. If you still need the identity (??), you can leave it as the nonclustered pk.

    +1

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Agree in this scenario it's for a BI application and I doubt it can handle self joins

    Maybe I am reading you wrong but I take it you are writing the logic in the application and performance is your concern? Have the application call a sql server stored procedure where the user can supply the date range for the normal dates (not the ones one year ago).

    ----------------------------------------------------

Viewing 5 posts - 16 through 19 (of 19 total)

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