Question

I have query which outputs the following,

RegDate     Month      countrycode  affiliate sk_skin  AC    GameWin
1/1/2011    1/1/2011   DE           0         a        274   32458.2558
1/1/2011    1/1/2011   UK           0         b        4     386.6925
1/1/2011    1/1/2011   FI           1         a        320   12875.0189
1/1/2011    1/1/2011   SK           1         b        85    5752.8503
1/1/2011    1/1/2011   FI           0         a        520   65339.3142
1/1/2011    1/1/2011   FI           1         a        295   29163.1045
1/1/2011    2/1/2011   DE           0         a        79    16028.3849
1/1/2011    3/1/2011   DE           0         b        4     525.252
1/1/2011    3/1/2011   DE           1         a        36    7718.6327
1/1/2011    3/1/2011   DE           1         b        24    5715.1279
1/1/2011    4/1/2011   FI           0         a        196   27357.4072
1/1/2011    4/1/2011   FI           1         a        146   20178.9315

I would like to have a Month row number, in this case 1/1/2011 will be 1, 2/1/2011 will be 2, 3/1/2011 will be 3, 4/1/2011 will be 4.

This query will be running through a loop so the month could start at different points,

I would like the following output;

RegDate   Month      countrycode  affiliate  sk_skin  AC   GameWin      Month
1/1/2011  1/1/2011   DE           0          a        274  32458.2558   1
1/1/2011  1/1/2011   UK           0          b        4    386.6925     1
1/1/2011  1/1/2011   FI           1          a        320  12875.0189   1
1/1/2011  1/1/2011   SK           1          b        85   5752.8503    1
1/1/2011  1/1/2011   FI           0          a        520  65339.3142   1
1/1/2011  1/1/2011   FI           1          a        295  29163.1045   1
1/1/2011  2/1/2011   DE           0          a        79   16028.3849   2
1/1/2011  3/1/2011   DE           0          b        4    525.252      3
1/1/2011  3/1/2011   DE           1          a        36   7718.6327    3
1/1/2011  3/1/2011   DE           1          b        24   5715.1279    3
1/1/2011  4/1/2011   FI           0          a        196  27357.4072   4
1/1/2011  4/1/2011   FI           1          a        146  20178.9315   4

I have tried to use the following,

(RANK() OVER (ORDER BY DATEADD(m,DATEDIFF(m,0,date),0))-1) as rank

The format of the date is date (no time) using

DATEADD(m,DATEDIFF(m,0,datetime),0)

This rank gives me random Rank number (with a sort of pattern), however since this query will be added to a loop and iterated over the number generated needs to be, 1 for lowest month, 2 for second lowest ect...

I hope i have explained myself properly.

Was it helpful?

Solution

Change your rank to be DENSE_RANK based on DATEPART

DENSE_RANK() OVER ( ORDER BY DATEPART(m, MyDate) )

when you need to span multiple years just add another datepart

DENSE_RANK() OVER ( ORDER BY DATEPART(YYYY, MyDate), DATEPART(m, MyDate))

Example

DECLARE @table TABLE ( MyDate DATE )

INSERT INTO @table
    VALUES  ( '02/01/2012' )
,           ( '02/01/2012' )
,           ( '02/01/2012' )
,           ( '03/01/2012' )
,           ( '03/01/2012' )
,           ( '03/01/2012' )
,           ( '04/01/2012' )

SELECT *
       ,DENSE_RANK() OVER ( ORDER BY DATEPART(m, MyDate) )
    FROM @table

Results

Month          Rank
2012-02-01     1
2012-02-01     1
2012-02-01     1
2012-03-01     2
2012-03-01     2
2012-03-01     2
2012-04-01     3

OTHER TIPS

Use DENSE_RANK() instead of RANK(), it will produce desired numbering.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top