Hi @Manasa M
Depending on your needs, I wrote two pieces of code in case when and pivot, you can give them a try.
create table source(Location char(6),Date datetime,Hours varchar(20),
Interval datetime,Value varchar(20));
insert into source values
('UK','2022/1/11','H00','0:15','1.2'),
('UK','2022/1/11','H00','0:30','2.2'),
('UK','2022/1/11','H00','0:45','3.3'),
('UK','2022/1/11','H01','1:00','4.4'),
('UK','2022/1/11','H01','1:15','1.2'),
('UK','2022/1/11','H01','1:30','2.2'),
('UK','2022/1/11','H01','1:45','3.3'),
('UK','2022/1/11','H02','2:00','4.4'),
('UK','2022/1/11','H02','2:15','1.2'),
('UK','2022/1/11','H02','2:30','2.2'),
('UK','2022/1/11','H02','2:45','3.3'),
('UK','2022/1/11','H03','3:00','4.4'),
('UK','2022/1/11','H03','3:15','1.2'),
('UK','2022/1/11','H03','3:30','2.2'),
('UK','2022/1/11','H03','3:45','3.3'),
('UK','2022/1/11','H04','4:00','4.4'),
('UK','2022/1/11','H04','4:15','1.2'),
('UK','2022/1/11','H04','4:30','2.2'),
('UK','2022/1/11','H04','4:45','3.3'),
('UK','2022/1/11','H05','5:00','4.4'),
('UK','2022/1/11','H05','5:15','1.2'),
('UK','2022/1/11','H05','5:30','2.2'),
('UK','2022/1/11','H05','5:45','3.3'),
('UK','2022/1/11','H06','6:00','4.4'),
('UK','2022/1/11','H06','6:15','1.2'),
('UK','2022/1/11','H06','6:30','2.2'),
('UK','2022/1/11','H06','6:45','3.3'),
('UK','2022/1/11','H07','7:00','4.4'),
('UK','2022/1/11','H07','7:15','1.2'),
('UK','2022/1/11','H07','7:30','2.2'),
('UK','2022/1/11','H07','7:45','3.3'),
('UK','2022/1/11','H08','8:00','4.4'),
('UK','2022/1/11','H08','8:15','1.3742');
Case when:
;with CTE as(
select *,case when datepart(mi,Interval) = 0 then '1:00'
when datepart(mi,Interval) = 15 then '0:15'
when datepart(mi,Interval) = 30 then '0:30'
when datepart(mi,Interval) = 45 then '0:45' end as q,
case when datepart(mi,Interval) = 0
then left(Hours,2) + convert(varchar(10),(convert (int,right(Hours,1)) - 1))
else Hours end as p from source)
select Location,Date,q as Interval,
max(case when p = 'H00' then Value end) as H00,
max(case when p = 'H01' then Value end) as H01,
max(case when p = 'H02' then Value end) as H02,
max(case when p = 'H03' then Value end) as H03,
max(case when p = 'H04' then Value end) as H04,
max(case when p = 'H05' then Value end) as H05,
max(case when p = 'H06' then Value end) as H06
from CTE group by Location,Date,q order by q;
Pivot:
;with T1 as(
select *,case when datepart(mi,Interval) = 0 then '1:00'
when datepart(mi,Interval) = 15 then '0:15'
when datepart(mi,Interval) = 30 then '0:30'
when datepart(mi,Interval) = 45 then '0:45' end as q,
case when datepart(mi,Interval) = 0
then left(Hours,2) + convert(varchar(10),(convert (int,right(Hours,1)) - 1))
else Hours end as p from source
),T2 as(
select Location,Date,q as Interval,p,Value from T1)
select * from T2 pivot(max(Value) for p in (H00,H01,H02,H03,H04,H05,H06)) as t;
Best regards,
Percy Tang
If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.