簡體   English   中英

根據另一列的值遞增行

[英]Increment row depending on value of another column

我在下面有一個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結束?

謝謝

嘗試將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 

您可以添加第一個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 

我會將日期截斷為幾個月並按這些值分組,然后根據截斷日期獲取年,月和行數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM