Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server制作组_Sql_Sql Server - Fatal编程技术网

SQL Server制作组

SQL Server制作组,sql,sql-server,Sql,Sql Server,我有一个问题: select maand, KENMART, minprio, _Id, rij from Tabelx 使用此输出: maand KENMART minprio _Id Rij ---------------------------------------- 201701 15959074 29 2921957 1 201702 15959074 29 2921957 2 201703 15959074

我有一个问题:

select maand, KENMART, minprio, _Id, rij 
from Tabelx
使用此输出:

maand   KENMART     minprio _Id     Rij
----------------------------------------
201701  15959074    29      2921957 1   
201702  15959074    29      2921957 2  
201703  15959074    29      2921957 3  
201704  15959074    29      2921957 4  
201705  15959074    29      2921957 5  
201706  15959074    29      2921957 6  
201707  15959074    29      2921955 1  
201708  15959074    29      2921955 2  
201709  15959074    19      2921949 1  
201710  15959074    19      2921949 2  
201711  15959074    19      2921949 3   
201712  15959074    29      2921953 1  
201801  15959074    29      2921951 1  
201802  15959074    19      2921947 1  
201803  15959074    19      2921947 2  
201804  15959074    29      2921951 2  
201805  15959074    29      2921951 3  
201806  15959074    29      2921951 4  
201807  15959074    29      2921951 5  
201808  15959074    29      2921951 6  
201809  15959074    29      2921951 7  
我想创建组,以便输出如下所示

maand   KENMART     minprio _Id     Rij Group     
-----------------------------------------------
201701  15959074    29      2921957 1   A   
201702  15959074    29      2921957 2   A    
201703  15959074    29      2921957 3   A    
201704  15959074    29      2921957 4   A    
201705  15959074    29      2921957 5   A    
201706  15959074    29      2921957 6   A    
201707  15959074    29      2921955 1   B   
201708  15959074    29      2921955 2   B    
201709  15959074    19      2921949 1   C    
201710  15959074    19      2921949 2   C    
201711  15959074    19      2921949 3   C   
201712  15959074    29      2921953 1   D    
201801  15959074    29      2921951 1   E    
201802  15959074    19      2921947 1   F     
201803  15959074    19      2921947 2   F    
201804  15959074    29      2921951 2   G   
201805  15959074    29      2921951 3   G    
201806  15959074    29      2921951 4   G    
201807  15959074    29      2921951 5   G    
201808  15959074    29      2921951 6   G    
201809  15959074    29      2921951 7   G    
问题在于这个群体

201801 15959074 29 2921951 1 E
它与组G具有相同的ID,但必须是不同的组,因为月份不对齐

我希望问题很清楚,你们能帮我解决


提前感谢

我很确定这是一个缺口和孤岛问题。一种方法使用
lag()
和累积和:

select t.*,
       sum(case when _id = prev_id then 0 else 1 end) over (partition by kenmart order by maand) as grp
from (select t.*,
             lag(_id) over (partition by kenmart order by maand) as prev_id
      from tabelx t
     ) t;

这将分组构造为数字,而不是字母。

这是代码及其工作原理。我使用数字语句进行测试,例如A是“1”B是“2”,等等

select *,(
(case
 when KENMART = 15959074 and id=2921957 then 1 
 when KENMART = 15959074 and id=2921955 then 2
 when KENMART = 15959074 and id=2921949 then 3
 when KENMART = 15959074 and id=2921953 then 4
 when KENMART = 15959074 and id=2921947 then 5
 when KENMART = 15959074 and id=2921951 then 6

end) ) as [Group]
 from tabelx

你能解释一下定义组的逻辑吗?我看不到名为“月”的专栏,所以你的解释没有帮助。除了Gordon写的,样本数据最好用+。请将您的问题包括在内,您当前的尝试和您想要的结果。欲知详情,,