1

Hi I have a query which returns data as below

select
    n.Key, np.value
from
    Test1 np
    join Test2 n on n.Key = np.Key
where
    n.NodeKey = 10000002 && np.pKey in (4,6,7,10,12)

which returns data as below

Key            value
--------       ------
10000002        2
10000002        0
10000002        2
10000002       True
10000002        1

Test2 is look up Table as below

  Key      PKey           Value                                                                                                                                                                                                                                                    
---------------------------------------
10000002     4               2                                                                                                                                                                                                                                                                
10000002     6               0                                                                                                                                                                                                                                                                
10000002     7               2                                                                                                                                                                                                                                                                
10000002     10              True
10000002     12              1     

Wanted to change the query so that it should return data as below

NodeKey   Value1   Value2   Value3  Value4  Value5
--------------------------------------------------
10000002     2        0        2       True    1

Can you please help me with this.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55

2 Answers2

1

You may use pivot clause as :

with t as
(
select i.* from Test2 i 
) 
select [Key] as 'NodeKey', 
       [4]   as 'Value1',
       [6]   as 'Value2',
       [7]   as 'Value3',
       [10]  as 'Value4',
       [12]  as 'Value5'
  from t
 pivot
 (
  max(Value) for PKey in ([4], [6], [7], [10], [12])    
 ) q;

NodeKey  Value1 Value2  Value3  Value4  Value5
-------- ------ ------  ------  ------ -------
10000002    2     0       2      True     1

Rextester Demo

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
0

You can try using sql-server pivot For structuring data horizontally. Try below code..

Add another column using row_number() function and Store whole result in temp table

select
    n.Key, np.value,
    'Value'+convert(varchar(2),ROW_NUMBER() over (partition by [key] order by [key])) as tmpCol 
    into #tempTable --store result in tmp table
from
    Test1 np
    join Test2 n on n.Key = np.Key
where
    n.NodeKey = 10000002 && np.pKey in (4,6,7,10,12)

then use below pivot code.

select *
from 
(
  select [key], value,tmpCol
  from #tempTable
) src
pivot
(
  max(value)
  for tmpCol in (value1,value2,value3,value4,value5)
) piv;