简体   繁体   English

根据另一列的值递增行

[英]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 : 我在下面有一个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 

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? 是否有任何方法可以根据dtMonthno启动Row列,并且上例中的增量1将从5开始到15结束?

Thanks 谢谢

Try changing the derivation of Row to: 尝试将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 

You can add month of first DT_Started date: 您可以添加第一个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 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一列的值组合行 - Combine row depending on their value of another column 根据同一行另一列中设置的值更新列 - Updating a column depending on the value set in another column in the same row 将1列作为两个单独的值求和,具体取决于行中的另一个值 - Summing 1 Column As Two Separate Values Depending on Another Value in the Row 增加/减少表中的计数值取决于使用 postgresql 中的触发器插入/删除另一个表的特定列值 - Increment/decrement count value in a table depending on insert/delete of a specific column value of another table using triggers in postgresql SQL脚本自动递增,具体取决于列值 - SQL Script to Auto Increment Depending on Column Value 基于另一列增加值 - Increment value based on another column SQL Server:根据上一行增加行值 - SQL Server: Increment row value depending on previous row SQL:根据另一列的值,在一列上保留一个具有最大值的行 - SQL : Keep ONE row with max value on a column depending on value of another column SQL 如何根据另一个值自动增加一个值 - SQL how to auto increment a value depending on another value OracleSQL:自动增量取决于另一列(弱实体建模) - OracleSQL: Auto Increment depending on another column (weak entity modelling)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM