Results 1 to 5 of 5

Thread: [RESOLVED] is there any query string that can do this?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Resolved [RESOLVED] is there any query string that can do this?

    This are the tables examples.

    ServicesAndProductsTable
    productID, productDescription, type, productCategory
    1000, product1, product, skincare
    1001, product2, service, facial
    1002, product3, product, skincare
    1003, product4, service, manicure

    ReceiptSummaryTable
    ReceiptID, clientID, totalAmt, employeeName
    11000, 100, $10, James
    11001, 110, $5, James
    11002, 150, $15, Sandy

    ReceiptRowsTable
    ReceiptID, productID, type, amt
    11000, 1000, product, $5
    11000, 1003, service, $5
    11001, 1002, product, $5
    11002, 1003, service, $5
    11002, 1001, service, $10

    ==============================================
    Expected output:

    employee ID, employee Name, productTotal, facialTotal, manicureTotal
    1, James, $10, $0, $5
    6, Sandy, $0, $10, $5

    is there a query which can do this?
    productTotal is calculated from the ReceiptSummaryTable where type = product

    As for facialTotal and manicureTotal will be slightly complicated.
    facialTotal is calculated from ReceiptSummaryTable where type = product AND category = facial in ServicesAndProductsTable.
    likewise for manicureTotal.

    anyway, i'm using sql server 2000.
    Last edited by sphericalx; Dec 11th, 2008 at 03:07 AM.
    Thanks,

    Ron

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Re: is there any query string that can do this?

    i'm working on a window application thus i need the result to be returned in such a way therefore i can pass it over to my window application to be shown on a datagridview.
    Thanks,

    Ron

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: is there any query string that can do this?

    Well, there is a way but I would first recommend dropping the summary table (you really don't need it) and adding Employees and Clients (if you don't already have those).
    See attached image.
    Attached Images Attached Images  

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    151

    Re: is there any query string that can do this?

    i had my employee and client table all along.

    anyway, manage to get the solution to it from someone else. thanks alot for ur help too!! ^^

    for the benefits of whoever that might come across with this problem, below is the solution

    Quote Originally Posted by visakh16
    SELECT RST.employeeName,
    SUM(CASE WHEN SPT.type='product' THEN RRT.amt ELSE 0 END) AS productTotal,
    SUM(CASE WHEN SPT.productCategory='facial' THEN RRT.amt ELSE 0 END) AS facialTotal,
    SUM(CASE WHEN SPT.productCategory='manicure' THEN RRT.amt ELSE 0 END) AS manicureTotal
    FROM ReceiptSummaryTable RST
    INNER JOIN ReceiptRowsTable RRT
    ON RRT.ReceiptID = RST.ReceiptID
    INNER JOIN ServicesAndProductsTable SPT
    ON SPT.productID = RRT.productID AND SPT.type = RRT.type
    GROUP BY RST.employeeName
    Thanks,

    Ron

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