Results 1 to 4 of 4

Thread: SQL 2005 Pivot Command

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    SQL 2005 Pivot Command

    How do I use the Pivot Command in SQL Server 2005. I have been looking at samples but I cannot Figure out what I am supposed to do with the two columns in the IN. I marked them as [...], [...] below.
    vb Code:
    1. SELECT [0], [1]
    2. FROM
    3.     SELECT     ServiceClassID, ServiceClassCode
    4.     FROM         vServiceClasses
    5. PIVOT
    6. (COUNT (ServiceClassID)
    7. FOR ServiceClassCode IN
    8. ([0], [1]
    9. ) AS pvt
    Last edited by FastEddie; Aug 17th, 2007 at 03:10 PM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL 2005 Pivot Command

    Post #3 in this thread

    http://www.vbforums.com/showthread.php?t=380853

    which is the sticky at the top of the db forum here - has an example of a PIVOT command that I gave an example of.

    Study the difference between the old method of CASE/SUM and the new method and hopefully it will make sense.

    If you give us some same data from your table and what you expect for output we could possibly help with your query specifically. But with the query you showed I have no idea what your output goal is.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: SQL 2005 Pivot Command

    Here is my query:
    vb Code:
    1. SELECT    
    2.     vTickets.StoreID,
    3.     vServiceClasses.Description,
    4.     Count(vServiceClasses.Description) as QTY
    5. FROM        
    6.     vServiceClasses RIGHT OUTER JOIN
    7.     vServices AS vServices ON
    8.     vServiceClasses.ServiceClassID =
    9.     vServices.ServiceClassID RIGHT OUTER JOIN
    10.     vTickets AS vTickets LEFT OUTER JOIN
    11.     vTicketItems AS vTicketItems ON
    12.     vTickets.TicketID = vTicketItems.TicketID ON
    13.     vServices.ServiceCode = vTicketItems.ServiceCode
    14. WHERE  
    15.     (vTickets.SaleDate BETWEEN '2007-08-05' AND '2007-8-11') AND
    16.     (vTickets.IsDeleted = 0) AND (vTickets.TicketStatus = 'Complete' OR
    17.      vTickets.TicketStatus = 'Backorder') AND
    18.     (vTicketItems.IsDeleted = 0) AND
    19.     (vTicketItems.SystemItemType = 'Service') AND
    20.     (vServices.ServiceCode = 'BATTERY' OR
    21.      vServices.ServiceCode = 'SERPENTINE BELT' OR
    22.      vServices.ServiceCode = 'ENGINE FLUSH' OR
    23.      vServices.ServiceCode = 'POWER STEER FLUID')  
    24. GROUP BY vTickets.StoreID, vServiceClasses.Description
    25. ORDER BY vTickets.StoreID, vServiceClasses.Description

    I get results like this:

    Store1 Engine Flush 3
    Store1 Serpentine Belts 3
    Store1 Batteries 3
    Store1 Power Steering Fluid 3

    What I need it to show is this:


    Store1 Engine Flush 3 Serpentine Belts 3 .

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL 2005 Pivot Command

    Here's an example using some of your data in a table I created on the fly

    Code:
    Exec sp_dbcmptlevel StuFiles,90
    Go
    Set NoCount On
    Declare @TestTbl Table (Store varchar(10),Class varchar(10),ItemCount int)
    Insert into @TestTbl values ('Store1','Battery',5)
    Insert into @TestTbl values ('Store1','Battery',3)
    Insert into @TestTbl values ('Store2','Battery',1)
    Insert into @TestTbl values ('Store2','Battery',10)
    Insert into @TestTbl values ('Store3','Battery',50)
    Insert into @TestTbl values ('Store3','Battery',100)
    
    Insert into @TestTbl values ('Store1','Belt',2)
    Insert into @TestTbl values ('Store1','Belt',1)
    Insert into @TestTbl values ('Store2','Belt',7)
    Insert into @TestTbl values ('Store2','Belt',80)
    Insert into @TestTbl values ('Store3','Belt',10)
    Insert into @TestTbl values ('Store3','Belt',1000)
    
    Insert into @TestTbl values ('Store1','Fluid',50)
    Insert into @TestTbl values ('Store1','Fluid',35)
    Insert into @TestTbl values ('Store2','Fluid',15)
    Insert into @TestTbl values ('Store2','Fluid',105)
    Insert into @TestTbl values ('Store3','Fluid',111)
    Insert into @TestTbl values ('Store3','Fluid',222)
    
    Insert into @TestTbl values ('Store1','Flush',55)
    Insert into @TestTbl values ('Store1','Flush',33)
    Insert into @TestTbl values ('Store2','Flush',11)
    Insert into @TestTbl values ('Store2','Flush',1010)
    Insert into @TestTbl values ('Store3','Flush',5050)
    Insert into @TestTbl values ('Store3','Flush',1001)
    And this for a query
    Code:
    Select Store,[Battery],[Belt],[Fluid],[Flush]
    From
    (select ItemCount,Class,Store From @TestTbl) a
    Pivot (Sum (ItemCount) For Class in ([Battery],[Belt],[Fluid],[Flush])) as Pvt
    Order by Store
    Produced this for output

    Code:
    Store      Battery     Belt        Fluid       Flush
    ---------- ----------- ----------- ----------- -----------
    Store1     8           3           85          88
    Store2     11          87          120         1021
    Store3     150         1010        333         6051
    Note that the compatability level has to be set to 90 for this to work.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width