我正在尝试进行行计数,以便可以在报表服务中使用它。下面的代码是指行最多可重复计数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方面的专家,因此能提供任何帮助。完整的代码和下面的一些屏幕截图。
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 but every 50 rows instead
似乎在O.O的完全相同的地方...
答案 0 :(得分:0)
在查询中,您正在使用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