Pivot help

  • I'm trying to use the Pivot to help replace an excel document that I have.

    The data that I have is a breakdown of amounts by vendor / state for each day of the week.  Example is:

    Weekday / Vendor / State / Count / Amount

    Saturday / 123 / OH / 50 / 100

    Monday / 145 / GA / 75 / 25

    What I'm wanting to do is be able to have each day of the week at the top and have the Vendor Name, State, Count and Amount as listed underneath each day of the week.

    Is this something that can be done with Pivot or is there a better alternative to programatically doing all of the calculations.

  • You could use pivots to rotate the talbe, like:

    SELECT Vendor,

    [State],

    [Monday],

    [Teusday],

    [Wednesday],

    [Thursday],

    [Friday],

    [Saturday],

    [Sunday]

    FROM ( SELECT Vendor,

    [State],

    [Weekday],

    Amount

    FROM table1

    ) p PIVOT ( SUM(Amount) FOR [Weekday] IN ( [Monday], [Teusday],

    [Wednesday], [Thursday],

    [Friday], [Saturday],

    [Sunday] ) ) AS pvt 

     

     

    But I'm not sure if this will do what you want. Could you describe what you mean by "listed underneath" (" week at the top and have the Vendor Name, State, Count and Amount as listed underneath each day of the week"). An example result would help. The result of the above query is :

     

     

    Vendor State Monday Teusday Wednesday Thursday Friday Saturday Sunday

    ----------- -------------------- ----------- ----------- ----------- ----------- ----------- ----------- -----------

    145 GA 25 NULL NULL NULL NULL NULL NULL

    123 OH NULL NULL NULL NULL NULL 100 NULL

     

    Regards,

       Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • I'm trying out your code, but what I want ultimately is:

      DateIDData  
      Monday Tuesday 
    Dept_NoStateSum of CntSum of AmountSum of CntSum of Amount
    ADSAAL    
     CA52$1,133.0023$458.00
     GA20$537.0018$396.00
     IN1$10.00 
     KY  
     MO  
     NM4$75.002$35.00
     NV1$22.00 
     OH34$551.0044$649.00
     TX48$1,144.0021$406.00
    ADSA Total 160$3,472.00108$1,944.00

     

    Repeated throughout for each of my vendors and days of the week (note this is copied from a pivot table in excel, so I cut it off after two days rather than send all of the cells).

     

  • Also, your code does everything except also give me the breakdown of the counts.  I can always work to replace the nulls with 0's.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply