根据另一列的值递增行

时间:2012-03-16 10:21:19

标签: sql sql-server sql-server-2008 row-number

我在下面有一个sql查询,其中dtMontno可以从任何月份开始,并且手动添加Row列,如下所示:

SELECT COUNT(*) as count,
       MONTH(TourTbl.DT_Started) as dtMonthno, 
       DATENAME(YYYY, TourTbl.DT_Started) as dtYear,
       row_number() over (order by DATENAME(YYYY, TourTbl.DT_Started) asc,
                                   MONTH(TourTbl.DT_Started) asc ) as Row 
FROM TourTbl 
INNER JOIN BranchTbl ON TourTbl.BranchID = BranchTbl.BranchID 
INNER JOIN AgencyTbl on AgencyTbl.AgencyID = BranchTbl.AgencyID 
WHERE Cancelled = 0  AND 
      (TourTbl.DT_Started >= '2010/03/15' and 
       TourTbl.DT_Started <= '2012/03/15') AND 
      AgencyTbl.AgencyID in ( 245 ) and 
      BranchRODID > 0
group by datename(M, TourTbl.DT_Started), 
         DATENAME(YYYY, TourTbl.DT_Started), 
         MONTH(TourTbl.DT_Started) 
order by dtYear asc, dtMonthno asc 

现在我的结果是:

  count dtMonthno dtYear    Row
    6      5      2011      1
    8      6      2011      2
    2      7      2011      3
    23     8      2011      4
    126    9      2011      5
    101    10     2011      6
    85     11     2011      7
    92     12     2011      8
    115    1      2012      9
    102    2      2012      10
    48     3      2012      11

有没有办法根据dtMonthno启动Row列,上面例子中的增量1将从5开始到15结束?

由于

3 个答案:

答案 0 :(得分:3)

尝试将Row的派生更改为:

row_number() over (order by YEAR(TourTbl.DT_Started) asc,
                            MONTH(TourTbl.DT_Started) asc ) +
min(YEAR(TourTbl.DT_Started)*12+MONTH(TourTbl.DT_Started)-1) OVER () % 12 as Row 

答案 1 :(得分:1)

您可以添加第一个DT_Started日期的月份:

SELECT COUNT(*) as count,
       MONTH(TourTbl.DT_Started) as dtMonthno, 
       DATENAME(YYYY, TourTbl.DT_Started) as dtYear,
       row_number() over (order by DATENAME(YYYY, TourTbl.DT_Started) asc,
                                   MONTH(TourTbl.DT_Started) asc ) 
                            + substring(min(DATENAME(YYYY, [TourTbl].DT_Started) + right ('0' + str (MONTH([TourTbl].DT_Started), 2), 2)) over (), 5, 2) - 1 as Row
FROM TourTbl 
INNER JOIN BranchTbl ON TourTbl.BranchID = BranchTbl.BranchID 
INNER JOIN AgencyTbl on AgencyTbl.AgencyID = BranchTbl.AgencyID 
WHERE Cancelled = 0  AND 
      (TourTbl.DT_Started >= '2010/03/15' and 
       TourTbl.DT_Started <= '2012/03/15') AND 
      AgencyTbl.AgencyID in ( 245 ) and 
      BranchRODID > 0
group by datename(M, TourTbl.DT_Started), 
         DATENAME(YYYY, TourTbl.DT_Started), 
         MONTH(TourTbl.DT_Started) 
order by dtYear asc, dtMonthno asc 

答案 2 :(得分:0)

我会将日期截断为月份并按这些值分组,然后根据截断日期获取年,月和行数:

SELECT
  COUNT(*) AS count,
  MONTH(GroupMonth) AS dtMonthno,
  DATENAME(YYYY, GroupMonth) AS dtYear,  /* why do you want year as a string? */
  ROW_NUMBER() OVER (ORDER BY GroupMonth) + MONTH(MIN(GroupMonth) OVER ()) - 1 AS Row
FROM (
  SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, TourTbl.DT_Started), 0) AS GroupMonth
  FROM TourTbl 
  INNER JOIN BranchTbl ON TourTbl.BranchID = BranchTbl.BranchID 
  INNER JOIN AgencyTbl on AgencyTbl.AgencyID = BranchTbl.AgencyID 
  WHERE Cancelled = 0  AND 
        (TourTbl.DT_Started >= '2010/03/15' and 
         TourTbl.DT_Started <= '2012/03/15') AND 
        AgencyTbl.AgencyID in ( 245 ) and 
        BranchRODID > 0
) s
GROUP BY GroupMonth