How to change the value of sum function inside the query?

Basel

I have a query with sum aggregation function :

SELECT sum(case when result=1 then 1 when result=2 then 0)as final_result From results

I want to change this part when result=2 then 0 to something like that when result=2 then final_result equals zero

Is it possible to do this ? or there is another way for that?

Raging Bull

Try this :

SELECT CASE WHEN cnt=sum THEN sum ELSE 0 END as Final_result
FROM
(
    SELECT count(*) as cnt,
           SUM(case when result=1 then 1 else 0 end) as sum
     from results
) Temp

Working Fiddle here.

Explanation:

Inner query select the total number of records and the sum of the records. Then if those count and sum are equal that means all values are 1.

NB: Removed the checking when result=2 then 0 because, the query will select 0 for any values other than 1. So there is no need to check for result=2.

EDIT:

To find the count of 10 consecutive 1's, you can do:

SELECT SUM(CASE WHEN RN=10 THEN 1 ELSE 0 END) AS final_result
FROM
(
SELECT CASE WHEN result=1 THEN @row_number:=@row_number+1 ELSE @row_number:=0 END AS RN,
       result 
FROM results, (SELECT @row_number:=0) AS t
) Temp

Sample Fiddle here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change value inside function

From Dev

How to change the options of a function (sum()) called inside a function (by()) without giving sum() a specific argument in R

From Dev

Change the value of a variable inside a function

From Dev

How to change the value of data inside a struct through a function?

From Dev

How to change the value of data inside a struct through a function?

From Dev

How do I change the value of any global variable inside of a function?

From Dev

call function on change of value inside <p> tag

From Dev

Change step value inside range function?

From Dev

(Oracle) How to get sum value inside loop?

From Dev

how to get a value inside a function

From Dev

how to change Mysql variable inside query?

From Dev

how to add the order by Sum function in a query?

From Dev

How can i change the value of a variable inside a function in node.js?

From Dev

How to execute SQL query inside a PHP function

From Dev

How to use select query inside max function?

From Dev

How to execute SQL query inside a PHP function

From Dev

How can I change user inside function

From Dev

How to change a variable declare inside a function in javascript

From Dev

How to prevent value change inside $watch

From Dev

How to change step value inside the loop?

From Dev

How to change value of property inside data attribute

From Dev

How to change the default value of a function with another function?

From Dev

function SQL SUM query

From Dev

Sum function in sql query

From Dev

query with CASE inside SUM not working

From Dev

Change global variable value with a function from inside window.onload

From Dev

jQuery function to change td value with button inside td

From Dev

How to apply a function to a value inside a constructor in haskell?

From Dev

How to get a value inside a clickHandler function jQuery

Related Related

  1. 1

    How to change value inside function

  2. 2

    How to change the options of a function (sum()) called inside a function (by()) without giving sum() a specific argument in R

  3. 3

    Change the value of a variable inside a function

  4. 4

    How to change the value of data inside a struct through a function?

  5. 5

    How to change the value of data inside a struct through a function?

  6. 6

    How do I change the value of any global variable inside of a function?

  7. 7

    call function on change of value inside <p> tag

  8. 8

    Change step value inside range function?

  9. 9

    (Oracle) How to get sum value inside loop?

  10. 10

    how to get a value inside a function

  11. 11

    how to change Mysql variable inside query?

  12. 12

    how to add the order by Sum function in a query?

  13. 13

    How can i change the value of a variable inside a function in node.js?

  14. 14

    How to execute SQL query inside a PHP function

  15. 15

    How to use select query inside max function?

  16. 16

    How to execute SQL query inside a PHP function

  17. 17

    How can I change user inside function

  18. 18

    How to change a variable declare inside a function in javascript

  19. 19

    How to prevent value change inside $watch

  20. 20

    How to change step value inside the loop?

  21. 21

    How to change value of property inside data attribute

  22. 22

    How to change the default value of a function with another function?

  23. 23

    function SQL SUM query

  24. 24

    Sum function in sql query

  25. 25

    query with CASE inside SUM not working

  26. 26

    Change global variable value with a function from inside window.onload

  27. 27

    jQuery function to change td value with button inside td

  28. 28

    How to apply a function to a value inside a constructor in haskell?

  29. 29

    How to get a value inside a clickHandler function jQuery

HotTag

Archive