Hi @Chuks ,
Welcome to Microsoft Q&A!
For this type of problem we recommend that you post CREATE TABLE statements for your tables together with INSERT statements with sample data.
Please also refer below:
drop table if exists table1
create table table1
(FE_KEY varchar(20),
ID int)
insert into table1 values
('6-K-1201',512623),
('6-K-1410',512624),
('6-K-1411',512625),
('6-K-1430',512785),
('6-K-1201',512623),
('6-K-1410',512624),
('6-K-1411',512625),
('6-K-1430',512785),
('6-K-1450',512792)
create table CVDATA
(FE_ID int,
CV_CODE varchar(20),
CV_VALUE int,
CV_UOM varchar(20))
insert into CVDATA values
(512623, 'EST_ISENTROPIC_POWER', 445, 'KW'),
(512624, 'EST_ISENTROPIC_POWER', 47000 , 'KW'),
(512625, 'EST_ISENTROPIC_POWER', 30000, 'KW'),
(512785, 'EST_ISENTROPIC_POWER', 70456, 'KW'),
(512623, 'MAX_TEMP_COMPRESSION', 200 , 'DegC'),
(512624, 'MAX_TEMP_COMPRESSION', 170, 'DegC'),
(512625, 'MAX_TEMP_COMPRESSION', 200 , 'DegC'),
(512785, 'MAX_TEMP_COMPRESSION', 150 , 'DegC'),
(512792, 'MAX_TEMP_COMPRESSION', 180 , 'DegC')
SELECT a.FE_KEY,
MAX(CASE WHEN b.CV_CODE = 'MAX_TEMP_COMPRESSION' THEN b.CV_VALUE ELSE NULL END) AS MAX_TEMP_COMPRESSION,
MAX(CASE WHEN b.CV_CODE = 'MAX_TEMP_COMPRESSION' THEN b.CV_UOM ELSE NULL END) AS MAX_TEMP_COMPRESSIONUOM ,
MAX(CASE WHEN c.CV_CODE = 'EST_ISENTROPIC_POWER' THEN c.CV_VALUE ELSE NULL END) AS EST_ISENTROPIC_POWER,
MAX(CASE WHEN c.CV_CODE = 'EST_ISENTROPIC_POWER' THEN c.CV_UOM ELSE NULL END) AS EST_ISENTROPIC_POWERUOM
FROM table1 a
left JOIN CVDATA b ON a.ID = b.FE_ID
left JOIN CVDATA c ON a.ID = c.FE_ID
WHERE a.FE_KEY Like '6-K-%'
GROUP BY a.FE_KEY
Output:
FE_KEY MAX_TEMP_COMPRESSION MAX_TEMP_COMPRESSIONUOM EST_ISENTROPIC_POWER EST_ISENTROPIC_POWERUOM
6-K-1201 200 DegC 445 KW
6-K-1410 170 DegC 47000 KW
6-K-1411 200 DegC 30000 KW
6-K-1430 150 DegC 70456 KW
6-K-1450 180 DegC NULL NULL
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
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.