简体   繁体   English

如何根据其他列的值将列的值增加 1 否则不会增加该值?

[英]How to increase the value of a column by 1 based on the value of other column else doesn't increment the value?

I have a table with column (difference).我有一个带列的表(差异)。

difference  tableNumber

     10    
     22    
     03    
     45    
     32    
     120   
     65    
     34    
     85    
     90    
     130   
     140   
     65    
     21    
     01    
     45    
     150   
     55    
     5000  
     87    
     43    

The first rows tableNumber will start with '1'.第一行 tableNumber 将以“1”开头。

difference  tableNumber

     10          1
     22    
     03    
     45    
     32    
     120   
     65    
     34    
     85    
     90    
     130   
     140   
     65    
     21    
     01    
     45    
     150   
     55    
     5000  
     87    
     43    

Now, (from second row onwards) if the 'difference' < 100 then insert the 'tableNumber' of previous rows' value现在,(从第二行开始)如果 'difference' < 100 然后插入前一行的值的 'tableNumber'

else if the 'difference' > 100 then increment the value of 'tableNumber' of previous row by 1 and insert it in the current row.否则,如果 'difference' > 100 则将前一行的 'tableNumber' 的值增加 1 并将其插入当前行。

Edit: Sample table: (tableNumber is the expected output)编辑:示例表:(tableNumber 是预期的输出)

So, this will be the expected output:所以,这将是预期的输出:

 difference tableNumber 

     10          1         
     22          1      
     03          1      
     45          1      
     32          1      
     120         2      
     65          2      
     34          2      
     85          2      
     90          2      
     130         3      
     140         4      
     65          4      
     21          4      
     01          4      
     45          4      
     150         5      
     55          5      
     5000        6      
     87          6      
     43          6      

SQL tables represent unordered sets. SQL 表表示无序集。 What you want to do seems to depend on the ordering of the rows.您想要做什么似乎取决于行的顺序。 I will assume that you have a column that specifies the ordering.我将假设您有一个指定排序的列。 Let's call it ord .我们称之为ord

I think the following basically does what you want:我认为以下基本上可以满足您的需求:

select difference,
       1 + sum(case when difference < 100 then 0 else 1 end) over (order by ord) as tablenumber
from t;

This does not take into account the first "1" in the table.这不考虑表中的第一个“1”。 However, that seems superfluous to the problem you really want to solve (identifying adjacent groups where the values are less than 100).但是,这对于您真正想要解决的问题(识别值小于 100 的相邻组)来说似乎是多余的。

If that is important, you can replace the 1 + with something like (select difference from t order by ord fetch first row only) + -- or whatever expression is appropriate for your database.如果这很重要,您可以将1 +替换为类似(select difference from t order by ord fetch first row only) + -- 或任何适合您的数据库的表达式。

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

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