Results 1 to 5 of 5

Thread: [RESOLVED] Help with a PIVOT table

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Resolved [RESOLVED] Help with a PIVOT table

    Hi everyone,

    I am trying to make a PIVOT table + aggragate data that I cannot quite get working. It is from only two tables.

    TicketPayments:
    [fkTicketID][intType][decValue]

    Tickets:
    [pkTicketID][dtDate][intCount][boolPaid]

    My goal is to have a query return a table in the following format.
    [1][2][3][4][5][Sum(intCount)]

    Columes 1-5 are the 5 types found in [intType]. Here is the working PIVOT portion I have. This is getting me a table of the format [1][2][3][4][5].
    Code:
    SELECT 
    [1],
    [2],
    [3],
    [4],
    [5] 
    FROM (SELECT intType, decValue, fkTicketID FROM TicketPayments) o
     PIVOT (SUM(decValue) FOR intType IN([1],[2],[3],[4],[5])) p
     JOIN Tickets c ON p.fkTicketID = c.pkTicketID
    I cannot figure how to add the [SUM(intCount)] column to the returned table.

    Any points on what I am missing?

  2. #2
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Question Re: Help with a PIVOT table

    Which database are you using?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Re: Help with a PIVOT table

    Mssql 2005

  4. #4
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Question Re: Help with a PIVOT table

    Quote Originally Posted by yaplej View Post
    Hi everyone,

    I am trying to make a PIVOT table + aggragate data that I cannot quite get working. It is from only two tables.

    TicketPayments:
    [fkTicketID][intType][decValue]

    Tickets:
    [pkTicketID][dtDate][intCount][boolPaid]

    My goal is to have a query return a table in the following format.
    [1][2][3][4][5][Sum(intCount)]

    Columes 1-5 are the 5 types found in [intType]. Here is the working PIVOT portion I have. This is getting me a table of the format [1][2][3][4][5].
    Code:
    SELECT 
    [1],
    [2],
    [3],
    [4],
    [5] 
    FROM (SELECT intType, decValue, fkTicketID FROM TicketPayments) o
     PIVOT (SUM(decValue) FOR intType IN([1],[2],[3],[4],[5])) p
     JOIN Tickets c ON p.fkTicketID = c.pkTicketID
    I cannot figure how to add the [SUM(intCount)] column to the returned table.

    Any points on what I am missing?
    Is the join on TICKETID required?

    If you use this


    Code:
    SELECT 
    [1],
    [2],
    [3]
    FROM (SELECT intType, decValue FROM TicketPayments) o
     PIVOT (SUM(decValue) FOR intType IN([1],[2],[3])) p
    you should get your desired results.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Re: Help with a PIVOT table

    Thanks,

    I was finally able to get the data I wanted. I am no SQL expert so figuring this guy out was a pain. There might be a more efficient way of doing this but I dont know it.

    Output looks like:
    [Date][TotalGuests][TotalCash][TotalCheck][TotalVisa][TotalGift][TotalCoupon]

    Code:
       
    SELECT Date, 
         SUM(TotalGuests) AS TotalGuests, 
         SUM(TotalCash) AS TotalCash, 
         SUM(TotalCheck) AS TotalCheck, 
         SUM(TotalVisa) AS TotalVisa, 
         SUM(TotalGift) AS TotalGift, 
         SUM(TotalCoupon) AS TotalCoupon
    FROM (
         SELECT convert(varchar, dtDate, 101) AS Date, 
              SUM(intGuestCount) AS TotalGuests, 
              SUM([1]) AS TotalCash, 
              SUM([2]) AS TotalCheck, 
              SUM([3]) AS TotalVisa, 
              SUM([4]) AS TotalGift, 
              SUM([5]) AS TotalCoupon 
              FROM (SELECT dtDate, 
                   intGuestCount, 
                   intType, 
                   decValue FROM TicketPayments 
              JOIN Tickets ON Tickets.pkTicketID = TicketPayments.fkTicketID ) o
                   PIVOT (SUM(decValue) FOR intType IN([1],[2],[3],[4],[5])) p
         GROUP BY dtDate) q
    GROUP BY Date
    Last edited by yaplej; Nov 6th, 2010 at 11:15 PM.

Tags for this Thread

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