如果需要帮助,请使用SQL
我正在尝试进行行计数,以便在reporting services中使用它。下面的代码意味着行的计数将一次又一次地增加到40,但有时计数不会增加如果需要帮助,请使用SQL,sql,ssms,case-when,Sql,Ssms,Case When,我正在尝试进行行计数,以便在reporting services中使用它。下面的代码意味着行的计数将一次又一次地增加到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
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案例中的佼佼者,因此任何帮助都是非常感谢的。下面是完整的代码和一些屏幕截图
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
它似乎位于完全相同的位置O.O…在您的查询中,您正在使用SUBSTRINGForsamling.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
令人惊叹的我尝试了您的两个查询,并在旧查询中更改为“”,我可以确认这两种方法都有效。