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. Problem with a T-SQL 2005 PIVOT

Problem with a T-SQL 2005 PIVOT

Scheduled Pinned Locked Moved Database
databasehelpsql-serversysadmin
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.
  • S Offline
    S Offline
    Steven J Jowett
    wrote on last edited by
    #1

    I am trying to run the following query on a MS SQL Server 2005 Express.

    SELECT * FROM
    (
    SELECT * FROM vuWeighTicketReport
    WHERE [TicketDate] >= '2010-09-10'
    ) AS TicketData PIVOT (
    SUM(ProductWeight) FOR ProductCode IN (
    [Blood],[Bonecake],[Carcass],[CFat],[Desiel],[DMeal],[Feather],[FMeal],
    [Greaves],[Head],[Misc],[Offal],[PBlend],[PFB],[PMeal],[Skin],[Slurry],
    [Tail],[Tallow],[TBottom],[Thigh],[TMeal],[Wash]
    )
    )

    SQL Managment Studio tells me there is an error near ')' I know the error is after the PIVOT statement, but what the actual problem is, I don't know. I would be very grateful if someone could point my mistake out to me. Thanks

    Steve Jowett ------------------------- Real Programmers don't need comments -- the code is obvious.

    S 1 Reply Last reply
    0
    • S Steven J Jowett

      I am trying to run the following query on a MS SQL Server 2005 Express.

      SELECT * FROM
      (
      SELECT * FROM vuWeighTicketReport
      WHERE [TicketDate] >= '2010-09-10'
      ) AS TicketData PIVOT (
      SUM(ProductWeight) FOR ProductCode IN (
      [Blood],[Bonecake],[Carcass],[CFat],[Desiel],[DMeal],[Feather],[FMeal],
      [Greaves],[Head],[Misc],[Offal],[PBlend],[PFB],[PMeal],[Skin],[Slurry],
      [Tail],[Tallow],[TBottom],[Thigh],[TMeal],[Wash]
      )
      )

      SQL Managment Studio tells me there is an error near ')' I know the error is after the PIVOT statement, but what the actual problem is, I don't know. I would be very grateful if someone could point my mistake out to me. Thanks

      Steve Jowett ------------------------- Real Programmers don't need comments -- the code is obvious.

      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #2

      SELECT *
      FROM vuWeighTicketReport
      WHERE [TicketDate] >= '2010-09-10'
      AS TicketData PIVOT
      (
      SUM(ProductWeight) FOR ProductCode IN ([Blood],[Bonecake],[Carcass],[CFat],[Desiel],[DMeal],[Feather],FMeal],[Greaves],[Head],[Misc],[Offal],[PBlend],[PFB],[PMeal],[Skin],[Slurry],[Tail],[Tallow],TBottom],[Thigh],[TMeal],[Wash])
      )

      try that

      As barmey as a sack of badgers Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.

      S 1 Reply Last reply
      0
      • S Simon_Whale

        SELECT *
        FROM vuWeighTicketReport
        WHERE [TicketDate] >= '2010-09-10'
        AS TicketData PIVOT
        (
        SUM(ProductWeight) FOR ProductCode IN ([Blood],[Bonecake],[Carcass],[CFat],[Desiel],[DMeal],[Feather],FMeal],[Greaves],[Head],[Misc],[Offal],[PBlend],[PFB],[PMeal],[Skin],[Slurry],[Tail],[Tallow],TBottom],[Thigh],[TMeal],[Wash])
        )

        try that

        As barmey as a sack of badgers Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.

        S Offline
        S Offline
        Steven J Jowett
        wrote on last edited by
        #3

        Thanks for the reply, but your suggestion did not work. I have now resolved the problem, and my final SELECT query now looks like this :-

        SELECT * FROM
        (
        SELECT * FROM vuWeighTicketReport
        WHERE [TicketDate] >= '2010-09-10'
        ) AS Tickets PIVOT
        (
        SUM(ProductWeight) FOR ProductCode IN
        (
        [Blood],[Bonecake],[Carcass],[CFat],[Desiel],[DMeal],[Feather],
        [FMeal],[Greaves],[Head],[Misc],[Offal],[PBlend],[PFB],[PMeal],
        [Skin],[Slurry],[Tail],[Tallow],[TBottom],[Thigh],[TMeal],[Wash]
        )
        ) AS TicketProductWeights

        The only difference to the original is that I have added AS TicketProductWeights to the end. Typically, the problem was stiring me in the face, and I couldn't see it. :doh: Thanks again.

        Steve Jowett ------------------------- Real Programmers don't need comments -- the code is obvious.

        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