Count with Year,Month,Week

  • I am trying to count all devices and arrange them on a weekly basis. so

    So the output should be the counts in the following order 2014-07,2014-06,2014-05 .....2014-01 ,2013-52,2013-51

    Table has WeekNumber column as Int, Month Number as Int and Year Number as int.

    My Query shows the output as 2013-01,2013-02....2013-52 , 2014-01 ,2014-02....2014-07

    With CTE1 as

    (Select Count(*) CountDevices,Year_Number,Month_Number,Week_Number

    From Prod

    Group By Year_Number ,Month_Number,Week_Number

    )

    Select CountDevices,Cast(Year_Number as Varchar(10)) + '-'+ Cast(Week_Number as Varchar(10)) from CTE1

    Order By Cast(Year_Number as Varchar(10)) ,Cast(Week_Number as Varchar(10))

    Any Ideas to get the desired output

  • You could post DDL, sample data and expected results to get better answers. I'm sure you've been told how to do that.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

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

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