Avatar of klyles95
klyles95

asked on 

How do I use PIVOT function to summarize data in SQLServer table

I have the following situation:

CREATE TABLE SalesLog (
      vendorID      int
      , salesDate      datetime
      , salesTypeID      int
      , totalSales      int )

INSERT INTO SalesLog VALUES ( 1, '1/1/2006', 1, 203 )
INSERT INTO SalesLog VALUES ( 1, '1/1/2006', 2, 77 )
INSERT INTO SalesLog VALUES ( 1, '1/1/2006', 3, 100 )

INSERT INTO SalesLog VALUES ( 2, '1/1/2006', 1, 9 )
INSERT INTO SalesLog VALUES ( 2, '1/1/2006', 2, 0 )
INSERT INTO SalesLog VALUES ( 2, '1/1/2006', 3, 23 )

I wish to present this information in summary format like this:

1, '1/1/2006', 203, 77, 100
2, '1/1/2006', 9, 0, 23

I believe the PIVOT function will help but I am having problems utilizing it.  

Can someone please help?
SQL

Avatar of undefined
Last Comment
frankytee

8/22/2022 - Mon