我有一个24小时数据的表,我想根据计数来转动数据并为它们分配排名。
create table #HourlyData_Counts
(Hr_1 int,Hr_2 int ,Hr_3 int,Hr_4 int,Hr_5 int ,Hr_6 int ,Hr_7 int , Hr_8 int ,Hr_9 int , Hr_10 int )
insert into #HourlyData_Counts
values (55,89,78,77,67,99,45,33,23,91)
select * from #HourlyData_Counts
--drop table #HourlyData_Counts
必需的输出
答案 0 :(得分:1)
在这种情况下,你需要使用Unpivot。
select Hours,counts,Ranks=Row_Number()over(order by counts)
from
(
select * from #HourlyData_Counts
) src
UNPIVOT
(
counts For Hours in(Hr_1,Hr_2,Hr_3,Hr_4,Hr_5,Hr_6,Hr_7,Hr_8,Hr_9,Hr_10)
) unpiv;
答案 1 :(得分:0)
您正在寻找unpivot,如下所示:


 select
 [小时],
 [次],
 rank()over(order by [counts] desc)
 from
(
 select * from#HourlyData_Counts
)as src
 unpivot
(
 [计数] [小时]在[Hr_1,Hr_2,Hr_3,Hr_4,Hr_5,Hr_6,Hr_7,Hr_8,Hr_9,Hr_10)
)为upvt
按try_cast排序(替换([Hours],'Hr_ ','')int)



答案 2 :(得分:0)
CREATE TABLE #HourlyData_Counts
(Hr_1 int,Hr_2 int ,Hr_3 int,Hr_4 int,Hr_5 int ,Hr_6 int ,Hr_7 int , Hr_8 int ,Hr_9 int , Hr_10 int )
INSERT INTO #HourlyData_Counts
VALUES (55,89,78,77,67,99,45,33,23,91)
SELECT
[Hours]
, Counts
, RANK() OVER (ORDER BY Counts DESC) Ranks
FROM
(
SELECT Hr_1, Hr_2, Hr_3, Hr_4, Hr_5, Hr_6, Hr_7, Hr_8, Hr_9, Hr_10
FROM #HourlyData_Counts
) P
UNPIVOT
(
Counts FOR [Hours] IN
(Hr_1, Hr_2, Hr_3, Hr_4, Hr_5, Hr_6, Hr_7, Hr_8, Hr_9, Hr_10)
) U
ORDER BY CAST(REPLACE([Hours], 'Hr_', '') AS int)
DROP TABLE #HourlyData_Counts
答案 3 :(得分:0)
IF OBJECT_ID('tempdb..#HourlyData_Counts') IS NOT NULL
DROP TABLE #HourlyData_Counts
create table #HourlyData_Counts
(Hr_1 int,Hr_2 int ,Hr_3 int,Hr_4 int,Hr_5 int ,Hr_6 int ,Hr_7 int , Hr_8 int ,Hr_9 int , Hr_10 int )
insert into #HourlyData_Counts
values (55,89,78,77,67,99,45,33,23,91)
select * from #HourlyData_Counts
SELECT hours,hour1, RANK() OVER (order by hour1 desc) as RANK
FROM
(
SELECT Hr_1, Hr_2, Hr_3, Hr_4, Hr_5, Hr_6, Hr_7, Hr_8, Hr_9, Hr_10
FROM #HourlyData_Counts
) as s
UNPIVOT
(
hour1
FOR hours IN
(
Hr_1,Hr_2,Hr_3,Hr_4,Hr_5,Hr_6,Hr_7,Hr_8,Hr_9,Hr_10
)
)AS pvt
order by hours
答案 4 :(得分:0)
您可以使用UNPIVOT,因此您的查询应该是这样的
SELECT hr,counts, RANK() OVER(ORDER BY counts DESC) FROM
(SELECT * FROM #HourlyData_Counts) p
UNPIVOT
(counts FOR hr IN (Hr_1 ,Hr_2 ,Hr_3 ,Hr_4 ,Hr_5 ,Hr_6 ,Hr_7 , Hr_8 ,Hr_9 , Hr_10 ) )AS unpvt;
RESULT
Hr_6 99 1
Hr_10 91 2
Hr_2 89 3
Hr_3 78 4
Hr_4 77 5
Hr_5 67 6
Hr_1 55 7
Hr_7 45 8
Hr_8 33 9
Hr_9 23 10
答案 5 :(得分:0)
如果列数和列名已修复,则可以使用
;with r(hr, data_count) as (
select 'hr_1', hr_1 from #HourlyData_Counts union all
select 'hr_2', hr_2 from #HourlyData_Counts union all
select 'hr_3', hr_3 from #HourlyData_Counts union all
select 'hr_4', hr_4 from #HourlyData_Counts union all
select 'hr_5', hr_5 from #HourlyData_Counts union all
select 'hr_6', hr_6 from #HourlyData_Counts union all
select 'hr_7', hr_7 from #HourlyData_Counts union all
select 'hr_8', hr_8 from #HourlyData_Counts union all
select 'hr_9', hr_9 from #HourlyData_Counts union all
select 'hr_10', hr_10 from #HourlyData_Counts
)
select hr, data_count, data_rank = ROW_NUMBER() over(order by data_count asc)
from r
order by data_count