• mario17 (8/22/2013)


    Is it possible to assign level (count for how many parts (months) it was broken for each ID),

    303 6/27/13 8/11/13 45

    ||

    \/

    level

    1 303 6/27/13 6/30/13 3

    2 303 7/01/13 7/31/13 31

    3 303 8/01/13 8/11/13 11

    Tx again,

    Jamie thansk for your post. I got you point, then I can do inpivot.

    Best

    mario

    Add an outer select to get a ROW_NUMBER():

    SELECT Level=ROW_NUMBER() OVER (PARTITION BY id ORDER BY fromdd)

    ,id, fromDD, toDD, period, prodID

    FROM (

    SELECT id, fromDD=MIN(d.DD), toDD=MAX(d.DD), period=COUNT(*), prodID

    FROM ttt a

    CROSS APPLY (SELECT n FROM Tally WHERE n BETWEEN 1 AND period+1) c

    CROSS APPLY (SELECT [month]=DATEPART(month, a.fromDD+n-1),DD=CAST(a.fromDD+n-1 AS DATE)) d

    GROUP BY id, prodID, [month]

    ) a


    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