A simple solution to get the desired result would be the following:
create table E_Limit (EID int, PID int, Metric int, Limit1 varchar(10), Limit2 varchar(10), Limit3 varchar(10));
go
insert into E_LIMIT values(1, 8 , 20 , '<' , '210' , '<');
insert into E_LIMIT values(1, 8 , 22 , '>' , '89' , '>=');
Go
Select * From
(SELECT EID, PID, CONVERT(VARCHAR,METRIC) + '-'+ LIMIT_RANGE METRIC_LIMIT_RANGE,LIMIT
FROM
(SELECT EID, PID, METRIC, LIMIT1, LIMIT2, LIMIT3
FROM E_LIMIT) UP
UNPIVOT
(LIMIT FOR LIMIT_RANGE IN
(LIMIT1, LIMIT2, LIMIT3)
)AS unpvt) P
Pivot (MAX(LIMIT) FOR METRIC_LIMIT_RANGE IN
(
[20-LIMIT1]
,[20-LIMIT2]
,[20-LIMIT3]
,[22-LIMIT1]
,[22-LIMIT2]
,[22-LIMIT3]
)) PVT
If you need more LIMIT columns or Metric values, you can try to use the information schema along with the values โโof the METRIC column and create a dynamic summary query to get the desired result.
Email me if you want to have a sample code for a dynamic script to generate the required results.
source
share