Results 1 to 13 of 13

Thread: Using SUM, why do I need two queries for one result

Hybrid View

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Using SUM, why do I need two queries for one result

    It has been a full 24 hours since I first posted this so I’m hoping the mods don’t get upset because I’m bumping it.

    Then again, perhaps there is no reason to bump it. As it is a full 24 hours old, had several views, but no responses, I’m inclined to believe that there is no way to do it other than my solution.

    What I have works. The totals are correct, the data is all on the report, and my users are happy. It just seems there would have to be a better way.

    But, if not, I can live with this.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Using SUM, why do I need two queries for one result

    As a guess ('cause I dunno what filters etc there were).

    The first should return totals if all the fields 3-12 are the same for all records.
    You got 0 because of the filters (probably).

    One way to check is to (re)create the totals first query, but supply no filters. See if it returns totals, especially if they are right. then add in one or two of your filters until you get back to 0.


    It does seem weird that you need to get the totals in one query and the data to display in the other...
    Was field5 unique - 'cause otherwise the totals lines may not match?

    Anyway- good luck fixing what is not broke but ensuring your happiness with it

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Using SUM, why do I need two queries for one result

    I also cannot believe that this was required - the SUM() should work...

    How many rows were returned in the first example?

    Do you have nulls in any of these fields?

    What is the back end DB?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Using SUM, why do I need two queries for one result

    Quote Originally Posted by szlamany
    How many rows were returned in the first example?
    One row with two fields. One containing the sum of field1 and one containing the sum of field2
    Quote Originally Posted by szlamany
    Do you have nulls in any of these fields?
    Not in the query doing the totals, but in the query returning the remainder of the data, yes. Which fields are empty, or NULL, will depending entirely on the selection criteria, which will differ each time the query is run.
    Quote Originally Posted by szlamany
    What is the back end DB?
    SQL Server 2000

    Also, when I run the first query (doing the SUMs and gathering the data) from Query Analyser, I also get 0 zeros for the totals.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Using SUM, why do I need two queries for one result

    If only one row is returned why ORDER BY FIELD5?

    I would love to get my hands on the data in that table - can you post it?

    If you go into ENTERPRISE MANAGER and open that table you can select all the rows and columns and copy/paste it into a .TXT file...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Using SUM, why do I need two queries for one result

    Quote Originally Posted by szlamany
    If only one row is returned why ORDER BY FIELD5?
    By this I meant when I do that SUM query all by itself I get one row, and I don't use an ORDER BY when I run that. When I put the two together, as the first code example in Post #1, I get a multitude of records. The number of records returned, as I said, will vary depending on the criteria in the WHERE and AND clauses. Regardless, however, I still need those two columns totaled, as well as all of the rest of the records.
    Quote Originally Posted by szlamany
    I would love to get my hands on the data in that table - can you post it?

    If you go into ENTERPRISE MANAGER and open that table you can select all the rows and columns and copy/paste it into a .TXT file...
    I don't think my management would be real happy if I did that.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Using SUM, why do I need two queries for one result

    The SUM() without the GROUP BY is a SUM() of the entire table - but limited by the WHERE clause.

    The SUM() with a GROUP BY gives a different SUM() for each row - a SUM() related to the GROUP BY fields.

    So I do not understand how you can be doing both - or even trying to do both.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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