Results 1 to 5 of 5

Thread: SQL: SELECT SUM giving weird results.

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    49

    SQL: SELECT SUM giving weird results.

    Here's a strange problem (at least to me it's strange). First of all I'm using Access 2000 for the time being to give a customer a proof of concept.

    Let's say I have 2 tables: TableA, TableB
    TableA has two fields: ID, Price
    TableB has two fields: ID, Cost

    Data in TableA
    ID|Price
    1|2
    2|3
    3|4

    Data in TableB
    ID|Cost
    1|1
    2|2

    SELECT SUM([TableA].[Price]) As [TPrice] FROM [TableA]
    Returns [TPrice] with a value of 9. That makes sense.

    SELECT SUM([TableB].[Cost]) As [TCost] FROM [TableB]
    Returns [TCost] with a value of 3. That makes sense too.

    SELECT SUM([TableA].[Price]) AS [TPrice],SUM( [TableB].[Cost]) AS [TCost] FROM [TableA], [TableB]
    Returns [TPrice] with 18 and [TCost] with 9. It looks like it's multiplying the SUM value by the number of records in the other table.

    Does anyone know why this is happening or what I'm doing wrong?



    Thanks,

    Leecher

  2. #2
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    I believe that is the default operation when joining or not joining while doing multiple aggregate SQL functions.

    My advice... do it in 2 seperate SQL queries.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Since you din't specify any conditions for the join, it first creates the table as follows

    Table A Price|Table B Cost
    2|1
    2|2
    3|1
    3|2
    4|1
    4|2

    It's like an OUTER JOIN. Every item in A is paired with each item in B.

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    49
    Is there a way to get the correct sum figures in one query?

  5. #5
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by Leecher
    Is there a way to get the correct sum figures in one query?
    no

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