locked
SUM that filters out data RRS feed

  • Question

  • Hi

    I want to sum some data, where it is not a certain value. For example I can do this for everything;

    SUM(Profits)

    However I want to do something like this

    SUM(Profits) where not internal

    How can I do this? sorry most likely a dumb question

    Friday, February 28, 2014 2:32 PM

Answers

  • use CASE WHEN inside

    ie like below

    SUM(CASE WHEN Internal=0 THEN Profits ELSE 0 END) 

    assuming internal is a bit field

    if not just write appropriate condition within WHEN


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    • Proposed as answer by Naomi N Friday, February 28, 2014 8:36 PM
    • Marked as answer by Fanny Liu Monday, March 10, 2014 1:19 AM
    Friday, February 28, 2014 2:52 PM

All replies

  • use CASE WHEN inside

    ie like below

    SUM(CASE WHEN Internal=0 THEN Profits ELSE 0 END) 

    assuming internal is a bit field

    if not just write appropriate condition within WHEN


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    • Proposed as answer by Naomi N Friday, February 28, 2014 8:36 PM
    • Marked as answer by Fanny Liu Monday, March 10, 2014 1:19 AM
    Friday, February 28, 2014 2:52 PM
  • A query like this is perfectly valid:

    SELECT SUM(Profits)
    FROM my_table
    WHERE internal='no'
    


    Gert-Jan

    • Proposed as answer by Naomi N Friday, February 28, 2014 8:36 PM
    Friday, February 28, 2014 3:11 PM