需要帮助的情况 SQL

Case when, help required SQL

我正在尝试进行行计数,以便在报告服务中使用它。下面的代码意味着行一次又一次地计数到 40,但计数有时不会。

CASE WHEN (ROW_NUMBER()OVER (PARTITION BY SUBSTRING(Forsamling.forsamling, 
1,1) ORDER BY Forsamling.forsamling) % 40) = 0 THEN 40 ELSE (ROW_NUMBER() 
OVER(PARTITION BY SUBSTRING(Forsamling.forsamling, 1, 1) ORDER BY 
Forsamling.forsamling) % 40) END AS SubGroupNo

我在 SQL CASE WHEN 方面不是最擅长的,因此感谢您的帮助。下面是完整的代码和一些屏幕截图。

 SELECT Forsamling.Forsamling, Forsamling.ForsamlingsNamn, Forsamling.Kommun, kommun.Kommun, kommun.KommunNamn, kommun.Lan, Lan.LansNamn, Lan.LansSuffix, Pastorat.Pastorat, Lan.Lan, Forsamling.DatumIn, Lan.DatumIn, Pastorat.DatumIn, Forsamling.DatumAvr, Lan.DatumAvr, Pastorat.DatumAvr, kommun.DatumIn, kommun.DatumAvr,
     CASE WHEN (ROW_NUMBER()OVER (PARTITION BY SUBSTRING(Forsamling.forsamling, 1, 1)
     ORDER BY kommun.Lan, Forsamling.Kommun, Forsamling.Forsamling) % 40) = 0 THEN 40 ELSE (ROW_NUMBER() OVER (PARTITION BY SUBSTRING(Forsamling.forsamling, 1, 1)
     ORDER BY kommun.Lan, Forsamling.Kommun, Forsamling.Forsamling) % 40) END AS SubGroupNo
 
FROM  
     (Select Forsamling, ForsamlingsNamn, kommun, DatumIn, DatumAvr, Lan
     FrOM A_Forsamling F
     Where (DatumAvr IS NULL OR DatumAvr > '2018-01-01') AND DatumIn <= '2018-01-01'
     )AS Forsamling Cross Apply
     (Select KommunNamn, kommun, DatumIn, DatumAvr, Lan
     FrOM A_Kommun K
     Where (DatumAvr IS NULL OR DatumAvr > '2018-01-01') AND DatumIn <= '2018-01-01' And Forsamling.Lan = K.Lan ANd Forsamling.Kommun = k.Kommun
     )AS Kommun Cross Apply
     (Select forsamling, kommun, pastorat, DatumIn, DatumAvr, Lan
     FrOM A_Pastorat P
     Where (DatumAvr IS NULL OR DatumAvr > '2018-01-01') AND DatumIn <= '2018-01-01' And Forsamling.Lan = p.Lan ANd Forsamling.Kommun = p.Kommun AND forsamling.Forsamling = p.Forsamling
     )AS Pastorat Cross Apply
     (Select  pastorat, DatumIn, DatumAvr, Lan, LansNamn, LansSuffix
     FrOM A_Lan L
     Where (DatumAvr IS NULL OR DatumAvr > '2018-01-01') AND DatumIn <= '2018-01-01' And Kommun.Lan = L.Lan
     )AS Lan
     
ORDER BY kommun.Lan, Forsamling.Kommun, Forsamling.Forsamling

Where the error occurs

Where the error occurs but every 50 rows instead

它似乎在同一个地方 O.O ...

在您的查询中,您正在使用 SUBSTRING(Forsamling.forsamling, 1, 1) 进行分区。将其更改为 '' 将解决问题。

此外,您可以通过将其更改为

来获得预期的结果
SubGroupNo=(ROW_NUMBER()OVER(PARTITION BY '' ORDER BY kommun.Lan, Forsamling.Kommun, Forsamling.Forsamling)  - 1) % 40 + 1

完整查询:

SELECT Forsamling.Forsamling, Forsamling.ForsamlingsNamn, Forsamling.Kommun, kommun.Kommun, kommun.KommunNamn, kommun.Lan, 
     Lan.LansNamn, Lan.LansSuffix, Pastorat.Pastorat, Lan.Lan, Forsamling.DatumIn, Lan.DatumIn, Pastorat.DatumIn, 
     Forsamling.DatumAvr, Lan.DatumAvr, Pastorat.DatumAvr, kommun.DatumIn, kommun.DatumAvr,
     SubGroupNo=(ROW_NUMBER()OVER(PARTITION BY '' ORDER BY kommun.Lan, Forsamling.Kommun, Forsamling.Forsamling)  - 1) % 40 + 1

FROM  
     (Select Forsamling, ForsamlingsNamn, kommun, DatumIn, DatumAvr, Lan
     FrOM A_Forsamling F
     Where (DatumAvr IS NULL OR DatumAvr > '2018-01-01') AND DatumIn <= '2018-01-01'
     )AS Forsamling Cross Apply
     (Select KommunNamn, kommun, DatumIn, DatumAvr, Lan
     FrOM A_Kommun K
     Where (DatumAvr IS NULL OR DatumAvr > '2018-01-01') AND DatumIn <= '2018-01-01' And Forsamling.Lan = K.Lan ANd Forsamling.Kommun = k.Kommun
     )AS Kommun Cross Apply
     (Select forsamling, kommun, pastorat, DatumIn, DatumAvr, Lan
     FrOM A_Pastorat P
     Where (DatumAvr IS NULL OR DatumAvr > '2018-01-01') AND DatumIn <= '2018-01-01' And Forsamling.Lan = p.Lan ANd Forsamling.Kommun = p.Kommun AND forsamling.Forsamling = p.Forsamling
     )AS Pastorat Cross Apply
     (Select  pastorat, DatumIn, DatumAvr, Lan, LansNamn, LansSuffix
     FrOM A_Lan L
     Where (DatumAvr IS NULL OR DatumAvr > '2018-01-01') AND DatumIn <= '2018-01-01' And Kommun.Lan = L.Lan
     )AS Lan

ORDER BY kommun.Lan, Forsamling.Kommun, Forsamling.Forsamling