简体   繁体   中英

Increment row depending on value of another column

I have a sql query below, where dtMontno could start from any month and am adding Row column manually as below :

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 

now my result is :

  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

Is there any way to start the Row column depending on the dtMonthno and increment by one in the example above would start from 5 and end in 15?

Thanks

Try changing the derivation of Row to:

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 

You can add month of first DT_Started date:

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 

I would truncate the dates to months and group by those values, then obtain years, months and row numbers based on the truncated dates:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM