对数据进行 UNPIVIOT 和 RANK

UNPIVIOT AND RANK the data

我有一个包含 24 小时数据的 table,我想旋转数据并根据计数为它们分配排名。

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

需要输出

在这种情况下,您将需要使用 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;

你正在寻找逆轴,像这样:

select
    [Hours],
    [counts],
    rank() over (order by [counts] desc)
from 
(
    select * from #HourlyData_Counts
) as 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)
) as upvt
order by try_cast(replace([Hours], 'Hr_', '') as int)
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
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 

您可以为此使用 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;

结果

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

如果列数和列名是固定的,那么可以使用

;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