You need to do some more work before you can pivot like that, because pivoting takes row data and makes it into column names, but none of your row data is 1, 2, 3, 4...
to use as a column name (inp_value1 <-- the 1 here)
You can do this, which is probably easier to understand:
SELECT
Empid,
Ele_name,
MAX(CASE WHEN rown = 1 THEN Inp_name END) as Inp_name1,
MAX(CASE WHEN rown = 1 THEN Inp_value END) as Inp_Value1,
MAX(CASE WHEN rown = 2 THEN Inp_name END) as Inp_name2,
MAX(CASE WHEN rown = 2 THEN Inp_value END) as Inp_Value2,
MAX(CASE WHEN rown = 3 THEN Inp_name END) as Inp_name3,
MAX(CASE WHEN rown = 3 THEN Inp_value END) as Inp_Value3,
MAX(CASE WHEN rown = 4 THEN Inp_name END) as Inp_name4,
MAX(CASE WHEN rown = 4 THEN Inp_value END) as Inp_Value4,
MAX(CASE WHEN rown = 5 THEN Inp_name END) as Inp_name5,
MAX(CASE WHEN rown = 5 THEN Inp_value END) as Inp_Value5,
MAX(CASE WHEN rown = 6 THEN Inp_name END) as Inp_name6,
MAX(CASE WHEN rown = 6 THEN Inp_value END) as Inp_Value6,
EntryId,
Start_date,
End_Date
FROM
(SELECT t2.*, ROW_NUMBER() OVER(PARTITION BY EmpId, Ele_name ORDER BY 1) as rown FROM t2) d
GROUP BY
Empid,
Ele_name,
EntryId,
Start_date,
End_Date
- ps; why specify name3/4/5/6 in your expected output if they're all
null? If the data will never have more than 2 rows per empid/ele_name pair then you can just write
null as input_name3..
and so on
- pps: i called my table t2 - edit your name into the query
- ppps; I don't know if the column "end date" really has a space in the name, I called mine with an underscore
Or you can pivot like this (harder to understand but more compact):
SELECT
Empid,
Ele_name,
pvt.*,
EntryId,
Start_date,
End_Date
FROM
(SELECT t2.*, ROW_NUMBER() OVER(PARTITION BY EmpId, Ele_name ORDER BY 1) as rown
FROM t2) d
PIVOT(
MAX(inp_name) as inp_name,
MAX(inp_value) as inp_value
FOR rown in (1,2,3,4,5,6)
) pvt
but the columns will come out of the pvt.*
with names as 1_inp_name, 1_inp_value .. You'll have to use AS
to rename them