-4

I have result set as follows:

Employer_id Type            Amount
1           penalty         100
1           interest        120
2           penalty         50
2           interest        60
2           contribution    70
1           contribution    140

I need result as:

Employer_id  penalty interest contribution**
1            100     120       140
2            50      60        70

How can I do this in SQL?

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 1
    Does this answer your question? [SQL Server dynamic PIVOT query?](https://stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query) – A. S. K. Dec 13 '19 at 20:04

1 Answers1

3

Please take a moment and learn how to format your questions.

A simple conditional aggregation should do the trick

Select Employer_id
      ,penalty      = sum(case when [type]='penalty'      then amount else 0 end)
      ,interest     = sum(case when [type]='interest'     then amount else 0 end)
      ,contribution = sum(case when [type]='contribution' then amount else 0 end)
 From  YourTable
 Group By Employer_id
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66