CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2008
    Posts
    31

    Question Problem in using Sum in SQL

    I Have these columns: A,B,C,D,E,F,G,H. F including numbers. I tried this SQL, but didn't succcess with me. May you help?

    " SELECT B,C,D,E, ( (SUM(F) where A= 'Tom')-(SUM(F) where A= 'George' and A ='Kevin' ) ) ,LAST(G),LAST(H)FROM Table1 GROUP BY B,C,D,E
    Last edited by nader; October 23rd, 2008 at 07:44 AM.

  2. #2
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: Problem in using Sum in SQL

    One problem I notice is your where statement for the second SUM():

    where A= 'George' and A ='Kevin'

    this will always be 0, because 'George' does not = 'Kevin'
    you are probably looking for:

    where A= 'George' OR A ='Kevin'





    Also, I am not sure exactly what the data contains and also not sure what you are trying to find with your query, but I doubt you would need:

    GROUP BY B,C,D,E

    my guess is that GROUP BY B would probably do the trick.




    Please post what errors you are getting from your query (if any).

    Also, an explaination of what the table contains and what you are trying to get with your query may help as well.
    Last edited by Nightwolf629; October 23rd, 2008 at 08:18 AM. Reason: edit

  3. #3
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: Problem in using Sum in SQL

    actually... now that I look at it again,

    you would need to run sub-queries for the SUM()'s
    you can't just tack on a where statment after them.

    Code:
    SELECT B,C,D,E, 
    ((SELECT SUM(F) FROM Table1 where A= 'Tom')-(SELECT SUM(F) FROM Table1 where A= 'George' OR A ='Kevin' ) ) as 'sum_statement' ,
    LAST(G),LAST(H)
    FROM Table1 
    GROUP BY B
    I think this may work for you
    Last edited by Nightwolf629; October 23rd, 2008 at 08:25 AM. Reason: edit

  4. #4
    Join Date
    May 2008
    Posts
    31

    Re: Problem in using Sum in SQL

    The first code I used is this
    " SELECT B,C,D,E, SUM(F),LAST(G),LAST(H)FROM Table1 GROUP BY B,C,D,E "
    this will sum all the values in the Column F and ...
    Example:
    B C D E F G H
    Box – Usa – Football – Classic - 10 - POP - 2500
    Box Usa - Football – Calssic - 5 - Rock - 1000

    The Result is:
    Box - Usa - Football – Calssic - 15 - Rock – 1000

    -Then I add in new column A
    A B C D E F G H
    Goerge - Box – Usa – Football – Classic - 5 - POP - 2500
    Tom - Box Usa - Football – Calssic - 15 - Rock - 1000
    Tom - Box Usa - Football – Calssic - 25 - Rock - 300
    Kevin - Box Usa - Football – Calssic - 10 - Rock - 300
    Goerge - Box – Usa – Football – Classic - 5 - POP - 2500
    Kevin - Box Usa - Football – Calssic -15 - Rock - 300

    This will take in the first :
    Box – Usa – Football – Classic - the problem is here - Rock – 300
    How to sum is by
    sum where A = Tom .. the tesult is 40
    sum where A= George .. the tesult is 10
    sum where A= Kevin .. the tesult is 25

    (40)- ( 10+25) = 5

    the End Result is :
    Box – Usa – Football – Classic - 5 - Rock – 300

  5. #5
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: Problem in using Sum in SQL

    Ok, you have me pretty confused now...


    Did you try anything I suggested?
    Are you still having a problem?

    ...Im pretty sure you never even asked a question.

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: Problem in using Sum in SQL

    You haven't explained what you are attempting to do with the original SQL statement.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    May 2008
    Posts
    31

    Re: Problem in using Sum in SQL

    I used your code. It gives to each different 4 column the same number of sum all the values in column F. it's mean the result according your code will be

    B C D E F G H
    Box – Usa – Football – Classic - 5 - Rock – 300

    Suit – Eng – Basketball– Classic - 5 - Blues - 3200

    But it must sum the specific values that belong to each 4 differevt column B,C,D,E

    B C D E F G H
    Box – Usa – Football – Classic - 5 - Rock – 300

    Suit – Eng – Basketball– Classic - 15 - Blues - 3200

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Problem in using Sum in SQL

    Use this, in combination with your brain, to solve your problem:

    Code:
    SELECT
      SUM(CASE WHEN name = 'Tom' THEN age ELSE 0 END) as sum_all_toms_ages,
      SUM(CASE WHEN name = 'Jeff' THEN age ELSE 0 END) as sum_all_jeffs_ages
    FROM
     tblTable
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

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

Featured