如果需要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

发生错误的

发生错误,但每50行出现一次

它似乎就在同一地点

在查询中,您使用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

最新更新