Percentage Chnage - partition by date?

  • Hi All,

    I have a quick question. My code below shows the data I'm pulling from my temp table

    select

    [year],[date],[dayofweek],[daynum],

    cast(count(CASE WHEN area = 'City & County' THEN 1 END) AS float) 'City&Countyvols'

    from @vols

    group by [date],[year],[dayofweek],[daynum]

    It displays the data like this...

    Year date dayofweek daynum City&Countyvols

    2012 07/12/2012 Friday 5 541

    2011 09/12/2012 Friday 5 321

    How or is it possible to have a column that calculates the percentage change between the dates?

  • justfillingtime (12/17/2012)


    Hi All,

    I have a quick question. My code below shows the data I'm pulling from my temp table

    select

    [year],[date],[dayofweek],[daynum],

    cast(count(CASE WHEN area = 'City & County' THEN 1 END) AS float) 'City&Countyvols'

    from @vols

    group by [date],[year],[dayofweek],[daynum]

    It displays the data like this...

    Year date dayofweek daynum City&Countyvols

    2012 07/12/2012 Friday 5 541

    2011 09/12/2012 Friday 5 321

    How or is it possible to have a column that calculates the percentage change between the dates?

    Yes it is possible. You use a cte and join to itself to get the previous row total as part of the calculation.

    If you want help with the code you are going to have to provide some details to work with. You can find the best practices for posting questions by following the first link in my signature. It explains what to post and what the best format is.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

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

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