Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Trying to get Pivot Table working in MS SQL 2005

Trying to get Pivot Table working in MS SQL 2005

Scheduled Pinned Locked Moved Database
databasequestionhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VREntropy
    wrote on last edited by
    #1

    I have never used a pivot before and I am starting to think that one doesnt work. Please help provide guidence. I have 3 tables _Contracts, which contain contractid and sepworkstatus _contracts -------------------- 1,[Sent] 2,[Sent] 3,[No Further Action] _vendors which has umbrellaname and vendornumber _contracts.vendorid is foreign to _vendors.vendornumber _vendors --------------------- um1,1 um2,2 um2,3 and _UmbrellaData which has umbrellaname and a few other needless columns. umbrellaname connects _UmbrellaData and _vendors _UmbrellaData --------------------- um1 um2 The outer apply will grab a list of all the statuses and their counts for each umbrella company The results will look similar to um1,sent,1 um2,No Further Action, 1 um2,sent,1 um3,Executed my output needs to look like this umbrellaname, sent, [No Further Action], [Executed] um1,1,null,null um2,1,1,null My current query looks like this select u.umbrellaname, u.stream, functionalarea, [In Negotiation],[Prework Complete],[Sent],[No Further Action],[Executed],[Separation Work Started],[Ready to Send],[Prework Started] from dbo._UmbrellaData u outer apply ( select v.umbrellaname, c.sepworkstatus, count(c.sepworkstatus) as counte from dbo._Vendor v join dbo._Contracts c on c.vendorid = v.vendornumber group by v.umbrellaname, c.sepworkstatus ) as oa pivot(max(oa.counte) for oa.sepworkstatus in ( [In Negotiation],[Prework Complete],[Sent],[No Further Action],[Executed],[Separation Work Started],[Ready to Send],[Prework Started] )) as cw where oa.umbrellaname = u.umbrellaname Unfortunately when I run it I get the following errors. Msg 8156, Level 16, State 1, Line 2 The column 'umbrellaname' was specified multiple times for 'cw'. Msg 4104, Level 16, State 1, Line 2 The multi-part identifier "oa.umbrellaname" could not be bound. Msg 4104, Level 16, State 1, Line 2 The multi-part identifier "u.umbrellaname" could not be bound. Msg 4104, Level 16, State 1, Line 2 The multi-part identifier "u.umbrellaname" could not be bound. Msg 4104, Level 16, State 1, Line 2 The multi-part identifier "u.stream" could not be bound. Why? How do I get a pivot to work with an outer apply? Any help on this would be greatly appreciated.

    M 1 Reply Last reply
    0
    • V VREntropy

      I have never used a pivot before and I am starting to think that one doesnt work. Please help provide guidence. I have 3 tables _Contracts, which contain contractid and sepworkstatus _contracts -------------------- 1,[Sent] 2,[Sent] 3,[No Further Action] _vendors which has umbrellaname and vendornumber _contracts.vendorid is foreign to _vendors.vendornumber _vendors --------------------- um1,1 um2,2 um2,3 and _UmbrellaData which has umbrellaname and a few other needless columns. umbrellaname connects _UmbrellaData and _vendors _UmbrellaData --------------------- um1 um2 The outer apply will grab a list of all the statuses and their counts for each umbrella company The results will look similar to um1,sent,1 um2,No Further Action, 1 um2,sent,1 um3,Executed my output needs to look like this umbrellaname, sent, [No Further Action], [Executed] um1,1,null,null um2,1,1,null My current query looks like this select u.umbrellaname, u.stream, functionalarea, [In Negotiation],[Prework Complete],[Sent],[No Further Action],[Executed],[Separation Work Started],[Ready to Send],[Prework Started] from dbo._UmbrellaData u outer apply ( select v.umbrellaname, c.sepworkstatus, count(c.sepworkstatus) as counte from dbo._Vendor v join dbo._Contracts c on c.vendorid = v.vendornumber group by v.umbrellaname, c.sepworkstatus ) as oa pivot(max(oa.counte) for oa.sepworkstatus in ( [In Negotiation],[Prework Complete],[Sent],[No Further Action],[Executed],[Separation Work Started],[Ready to Send],[Prework Started] )) as cw where oa.umbrellaname = u.umbrellaname Unfortunately when I run it I get the following errors. Msg 8156, Level 16, State 1, Line 2 The column 'umbrellaname' was specified multiple times for 'cw'. Msg 4104, Level 16, State 1, Line 2 The multi-part identifier "oa.umbrellaname" could not be bound. Msg 4104, Level 16, State 1, Line 2 The multi-part identifier "u.umbrellaname" could not be bound. Msg 4104, Level 16, State 1, Line 2 The multi-part identifier "u.umbrellaname" could not be bound. Msg 4104, Level 16, State 1, Line 2 The multi-part identifier "u.stream" could not be bound. Why? How do I get a pivot to work with an outer apply? Any help on this would be greatly appreciated.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You might want to work through this article[^], not so much for the detail but more for the way the pivot is constructed.

      Never underestimate the power of human stupidity RAH

      V 1 Reply Last reply
      0
      • M Mycroft Holmes

        You might want to work through this article[^], not so much for the detail but more for the way the pivot is constructed.

        Never underestimate the power of human stupidity RAH

        V Offline
        V Offline
        VREntropy
        wrote on last edited by
        #3

        Thanks. Somehow I had missed that article. It does give a shove in the right direction. So far we have built a temp table instead. Maybe that can be made cleaner. :) Don

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups