using Pivot to Insert from Select query

I have a Select query that has the following output how do i use a PIVOT to insert into a table with the same field names.

Output from Select Statement:
Count            Fields
-----------------------
101      critical
150265      error
541848      notice
306      warning


Table where data needs to be inserted:
RecordID      int      
ClientNumber      varchar(20)      
DateTime      varchar(20)      
Critical      int      
Alert      int      
Warning      int      
Error      int      
Notice      int      
            

byte1Asked:
Who is Participating?
 
ralmadaCommented:
Also, you might want to put the count column name between brackets since "count" is a reserved word in SQL. You should avoid using reserved words to name your columns.
insert tableb(critical, error, notice, warning)
select [critical], [error], [notice], [warning]
from
(
select fields, [count] --here goes your current query
from yourtable
) o
pivot(max([count]) for fields in ([critical], [error], [notice], [warning]))p

Open in new window

0
 
ralmadaCommented:
something along this line.
 

insert tableb(critical, error, notice, warning)
select [critical], [error], [notice], [warning]
from
(
select fields, count --here goes your current query
from yourtable
) o
pivot(max(count) for fields in ([critical], [error], [notice], [warning]))p

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.