MS SQL Server finding top 4 peak readings in a table and display as columns -


my data set

date_time   load 7/24/17 12:00    3987 7/24/17 1:00 3748 7/24/17 2:00 3608 7/24/17 3:00 3526 7/24/17 4:00 3493 7/24/17 5:00 3545 7/24/17 6:00 3683 7/24/17 7:00 3827 7/24/17 8:00 3942 7/24/17 9:00 3956 7/24/17 10:00    3985 7/24/17 11:00    4000 7/24/17 12:00 pm    3917 7/24/17 1:00 pm 3834 7/24/17 2:00 pm 3901 7/24/17 3:00 pm 4132 7/24/17 4:00 pm 4388 7/24/17 5:00 pm 4497 7/24/17 6:00 pm 4675 7/24/17 7:00 pm 4713 7/24/17 8:00 pm 4743 7/24/17 9:00 pm 4704 7/24/17 10:00 pm    4540 7/24/17 11:00 pm    4227 

what need--desired output

date_time   load          peak-1    peak-2  peak-3  peak-4 7/24/17 12:00    3987                 7/24/17 1:00 3748                 7/24/17 2:00 3608                 7/24/17 3:00 3526                 7/24/17 4:00 3493                 7/24/17 5:00 3545                 7/24/17 6:00 3683                 7/24/17 7:00 3827                 7/24/17 8:00 3942                 7/24/17 9:00 3956                 7/24/17 10:00    3985                 7/24/17 11:00    4000                 7/24/17 12:00 pm    3917                 7/24/17 1:00 pm 3834                 7/24/17 2:00 pm 3901                 7/24/17 3:00 pm 4132                 7/24/17 4:00 pm 4388                 7/24/17 5:00 pm 4497                 7/24/17 6:00 pm 4675                1 7/24/17 7:00 pm 4713        1        7/24/17 8:00 pm 4743    1            7/24/17 9:00 pm 4704            1    7/24/17 10:00 pm    4540                 7/24/17 11:00 pm    4227 

here wrote

this way messy select date_time, system_load dbo.loads e1 e1.date_time >= dateadd(day, -1, convert(date, getdate())) , e1.date_time< dateadd(day, +0, convert(date, getdate ()))-- previous day) , (4-3) = (select count(distinct(e2.system_load)) dbo.loads e2 e2.date_time >= dateadd(day, -1, convert(date, getdate())) , e2.date_time< dateadd(day, +0, convert(date, getdate ()))-- previous day) , e2.system_load > e1.system_load) union select date_time, system_load dbo.loads e1 e1.date_time >= dateadd(day, -1, convert(date, getdate())) , e1.date_time< dateadd(day, +0, convert(date, getdate ()))-- previous day) , (4-2) = (select count(distinct(e2.system_load)) dbo.loads e2 e2.date_time >= dateadd(day, -1, convert(date, getdate())) , e2.date_time< dateadd(day, +0, convert(date, getdate ()))-- previous day) , e2.system_load > e1.system_load) union select date_time, system_load dbo.loads e1 e1.date_time >= dateadd(day, -1, convert(date, getdate())) , e1.date_time< dateadd(day, +0, convert(date, getdate ()))-- previous day) , (4-1) = (select count(distinct(e2.system_load)) dbo.loads e2 e2.date_time >= dateadd(day, -1, convert(date, getdate())) , e2.date_time< dateadd(day, +0, convert(date, getdate ()))-- previous day) , e2.system_load > e1.system_load) union select date_time, system_load dbo.loads e1 e1.date_time >= dateadd(day, -1, convert(date, getdate())) , e1.date_time< dateadd(day, +0, convert(date, getdate ()))-- previous day) , (4-4) = (select count(distinct(e2.system_load)) dbo.loads e2 e2.date_time >= dateadd(day, -1, convert(date, getdate())) , e2.date_time< dateadd(day, +0, convert(date, getdate ()))-- previous day) , e2.system_load > e1.system_load) order system_load desc

this query give column counting highest load 1, next 2 etc

i suggest wrap sub query, , use 4 instances of case when load_rank = xxx 1 end put 1 each column (xxx number 1 4) - exercise have go. we'll here

select   date_time, load, row_number() over(partition convert(date, date_time) order load desc) load_rank    table order date_time 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -