2

i am new to SQL server and i am trying to combine multiple row in single row but i am not able to do it.Can anyone help me out?

Input :

Id      |RED    |BUY    |BSW
------------------------------------
1328        NULL    NULL    0.05
1328        NULL    0.06    NULL
1328        0.01    NULL    NULL
1328        0.05    NULL    NULL
1329        NULL    NULL    0.05
1329        NULL    0.05    NULL
1329        0.05    NULL    NULL

Output

Id         |RED    |BUY    |BSW
------------------------------------
1328        0.01    0.06    0.05
1328        0.05    NUll    NULL
1329        0.05    0.05    0.05

Editing data so as to remove SUM() conflict.

Flying_Machine
  • 313
  • 4
  • 15

2 Answers2

0

Try like this

SELECT id, 
       Sum(Isnull(red, 0)) 
         OVER ( 
           partition BY id 
           ORDER BY id), 
       Sum(Isnull(buy, 0)) 
         OVER ( 
           partition BY id 
           ORDER BY id), 
       Sum(Isnull(bsw, 0)) 
         OVER ( 
           partition BY id 
           ORDER BY id) 
FROM Table1
GROUP  BY id 
Vignesh Kumar A
  • 26,868
  • 11
  • 59
  • 105
0

first of all change the field default setting to zero instread of null it will increase the processing speed then try this

SELECT id, 
       Sum((red, 0)) 
         OVER ( 
           partition BY id 
           ORDER BY id), 
       Sum((buy, 0)) 
         OVER ( 
           partition BY id 
           ORDER BY id), 
       Sum((bsw, 0)) 
         OVER ( 
           partition BY id 
           ORDER BY id) 
GROUP  BY id 
Vignesh Kumar A
  • 26,868
  • 11
  • 59
  • 105
vignesh pbs
  • 398
  • 2
  • 4
  • 16