27,579
社区成员
发帖
与我相关
我的任务
分享
if object_id('s') is not null
drop table s
go
create table s(id int,score int)
INSERT INTO s
SELECT 1, 150
UNION ALL SELECT 2, 133
UNION ALL SELECT 3, 123
UNION ALL SELECT 4, 80
UNION ALL SELECT 5, 90
UNION ALL SELECT 6, 60
UNION ALL SELECT 7, 34
UNION ALL SELECT 8, 21
UNION ALL SELECT 9, 102
go
declare @table_name nvarchar(100);
declare @sql nvarchar(max);
declare @start int;
declare @end int;
declare @interval int;
select @start = 0, --开始的分数
@end = 150, --结束的分数
@interval = 20,--这里的间隔改为20
@sql = '',
@table_name = 's'; --这里的表的名称是s
;with t
as
(
select @start as start_score,@start + @interval as end_score
union all
select start_score + @interval,
case when end_score + @interval > @end
then @end
else end_score + @interval
end
from t
where start_score + @interval < @end
),
tt
as
(
select start_score,
end_score,
cast(end_score as varchar) +'-'+cast(start_score as varchar) as score_range
from t
)
select @sql = @sql +
',count(case when score >'+cast(start_score as varchar) +
' and score <= '+cast(end_score as varchar) +
' then 1 else null end) as ['+score_range +']'
from tt
set @sql = 'select '+stuff(@sql,1,1,'') + ' from '+@table_name
--select @sql
exec(@sql)
/*
20-0 40-20 60-40 80-60 100-80 120-100 140-120 150-140
0 2 1 1 1 1 2 1
*/
if object_id('s') is not null
drop table s
go
create table s(id int,score int)
INSERT INTO s
SELECT 1, 150
UNION ALL SELECT 2, 133
UNION ALL SELECT 3, 123
UNION ALL SELECT 4, 80
UNION ALL SELECT 5, 90
UNION ALL SELECT 6, 60
UNION ALL SELECT 7, 34
UNION ALL SELECT 8, 21
UNION ALL SELECT 9, 102
go
declare @table_name nvarchar(100);
declare @sql nvarchar(max);
declare @start int;
declare @end int;
declare @interval int;
select @start = 0, --开始的分数
@end = 150, --结束的分数
@interval = 10,--间隔
@sql = '',
@table_name = 's'; --这里的表的名称是s
;with t
as
(
select @start as start_score,@start + @interval as end_score
union all
select start_score + @interval,
case when end_score + @interval > @end
then @end
else end_score + @interval
end
from t
where start_score + @interval < @end
),
tt
as
(
select start_score,
end_score,
cast(end_score as varchar) +'-'+cast(start_score as varchar) as score_range
from t
)
select @sql = @sql +
',count(case when score >'+cast(start_score as varchar) +
' and score <= '+cast(end_score as varchar) +
' then 1 else null end) as ['+score_range +']'
from tt
set @sql = 'select '+stuff(@sql,1,1,'') + ' from '+@table_name
--select @sql
exec(@sql)
/*
10-0 20-10 30-20 40-30 50-40 60-50 70-60 80-70 90-80 100-90 110-100 120-110 130-120 140-130 150-140
0 0 1 1 0 1 0 1 1 0 1 0 1 1 1
*/
DECLARE @SpaceScore INT=10 --分数间隔
DECLARE @EndScore INT=150
IF OBJECT_ID('tempdb..#ScoreOut','U') IS NOT NULL DROP TABLE #ScoreOut
CREATE TABLE #ScoreOut
(
Id INT IDENTITY(1,1)
,StartScore INT
,EndScore INT
)
;WITH CTE(Score)
AS (
SELECT 0 AS Score
UNION ALL
SELECT Score+@SpaceScore
FROM CTE
WHERE Score<@EndScore-@SpaceScore
)
INSERT INTO #ScoreOut(StartScore,EndScore)
SELECT Score,Score+@SpaceScore
FROM CTE
IF OBJECT_ID('tempdb..#Score','U') IS NOT NULL DROP TABLE #Score
CREATE TABLE #Score
(
sno INT
,Score INT
)
INSERT INTO #Score
SELECT 1, 150
UNION ALL SELECT 2, 133
UNION ALL SELECT 3, 123
UNION ALL SELECT 4, 80
UNION ALL SELECT 5, 90
UNION ALL SELECT 6, 60
UNION ALL SELECT 7, 34
UNION ALL SELECT 8, 21
UNION ALL SELECT 9, 102
SELECT CAST(B.StartScore AS VARCHAR)+'-'+CAST(B.EndScore AS VARCHAR) AS ScoreSpace
,COUNT(*) AS Cnt
FROM #Score AS A
JOIN #ScoreOut AS B ON A.Score BETWEEN B.StartScore AND B.EndScore
GROUP BY CAST(B.StartScore AS VARCHAR)+'-'+CAST(B.EndScore AS VARCHAR)
/*
ScoreSpace Cnt
100-110 1
120-130 1
130-140 1
140-150 1
20-30 1
30-40 1
50-60 1
60-70 1
70-80 1
80-90 2
90-100 1
*/
select score/5 as fs,count(*) fc
from t
group by score/5