Results 1 to 4 of 4

Thread: SQL Query Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Singapore
    Posts
    118

    Question SQL Query Question

    In my table, I have records that contain Customer, Invoice, Total ... Initially, the records will be something like this:

    Customer Invoice Total

    Customer A Invoice 1 10
    Customer A Invoice 1 20
    Customer A Invoice 1 30 = 60

    Customer B Invocie 2 15
    Customer B Invocie 2 25
    Customer B Invocie 2 30 = 70

    Customer A Invoice 3 20
    Customer A Invoice 3 20
    Customer A Invoice 3 20 = 60

    Customer B Invoice 4 10
    Customer B Invoice 4 10 = 20

    What i want for my query is to display these

    Customer Sub Total
    Customer A 120
    Customer B 90

    Sub Total, is the one that i do not know abt.. totalling up all the totals, based on diff customer.. any idea?

    thanks..
    FujIn

  2. #2
    Frenzied Member John McKernan's Avatar
    Join Date
    Jan 2002
    Location
    SE PA
    Posts
    1,295
    Just off the top of my head (I haven't tested it):
    Code:
    Select CustomerName, Sum(InvoiceAmt) As SubTotal From Customers Group By CustomerName

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Singapore
    Posts
    118
    Thanks a lot! Appreciate it..
    FujIn

  4. #4
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking 5am start...9pm finish!!! Bugger!

    Assuming that you would also like to know the total for each customer, per invoice then use:
    Code:
    SELECT CustomerName, InvoiceNumber, SUM(InvoiceAmt) AS SubTotal 
    FROM Customers
    GROUP BY CustomerName, InvoiceNumber
    ORDER BY CustomerName, InvoiceNumber
    If you want to see which Companies have had order worth greater than say £100 then use the following, which is the same as above, but one added line:
    Code:
    SELECT CustomerName, InvoiceNumber, SUM(InvoiceAmt) AS SubTotal 
    FROM Customers
    GROUP BY CustomerName, InvoiceNumber
    HAVING SUM(InvoiceAmt) > 100
    ORDER BY CustomerName, InvoiceNumber
    Thought you might be interested in that...

    Hope it helps,

    Woka

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