SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Nov 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Retrieving fields in several table.

    Hi guys, for instance I have to tables - payment and services.
    The payment table contains the following field - OR_ID, amount, mode, entrydate.
    My services table contains the following field - code, description, amount, entrydate.

    I already have a query on how to get the payment of customer based on cash, check and credit card. However, I do not know how to include the total amount of services being rendered by the customer. I wanted to retrieve it in place it in a single row.



    HTML Code:
    SELECT SUM(amount) AS 'Cash', (SELECT SUM(amount) FROM payment WHERE mode='Check') AS 'Cheque', (SELECT SUM(amount) FROM payment WHERE mode='Credit Card') AS 'Credit Card' FROM payment;

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    your tables seems to be missing a customer column
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict
    Join Date
    Nov 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use inner join and it gives me a message...subquery returns more than 1 row.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    change your first query to this --
    Code:
    SELECT SUM(amount) AS 'Cash'
         , SUM(CASE WHEN mode='Check'
                    THEN amount 
                    ELSE NULL END) AS 'Cheque'
         , SUM(CASE WHEN mode='Credit Card'
                    THEN amount 
                    ELSE NULL END) AS 'Credit Card'
      FROM payment
    once you've done this, you will have a result set consisting of only one row

    do the other query, which should also produce a result set with only one row, right?

    then i'll show you how to CROSS JOIN them

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •