Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Condition with CASE in SQL

According to GORDONS answer I implemented the query to get the following output.

ROW_ID      ARTIKEL      SUPPLIERID      ORGID      PIECES      COSTPRICE      DISCOUNT    VALUE_DRILL_DOWN
1           TV            SONY          922         6            110           2.5         14
2           TV            SONY          922         10           80            1            4
3           TV            SONY          922         6            65            1.5          0
4           TV            SONY          922         14           95            1.5          0
5           TV            SONY          922         18           95            1.5          0
6           TV            SONY          922         4            95            1.5          0

My query is as follows:

SELECT "SUPPLIERID","ARTIKEL",(case when @x - "cumlativesum") < 0 then NULL else (@x - "cumlativesum") end) as "VALUE_DRILL_DOWN"
from 
(SELECT T1."ARTIKEL",T1."SUPPLIERID",(select sum("PIECES") from EXAMPLE_TABLE T2    where T2."ROW_ID" <= T1."ROW_ID"and T2."SUPPLIERID" = T1."SUPPLIERID" and T2."ARTIKEL"=T1."ARTIKEL") as "cumlativesum" from :EXAMPLE_TABLE T1)

But I want the output in this manner:

ROW_ID      ARTIKEL      SUPPLIERID      ORGID      PIECES      COSTPRICE      DISCOUNT    VALUE_DRILL_DOWN
1           TV            SONY          922         6            110           2.5         14
2           TV            SONY          922         10           80            1           4
3           TV            SONY          922         6           65            1.5          0
4           TV            SONY          922         14           95            1.5         Null
5           TV            SONY          922         18           95            1.5         Null
6           TV            SONY          922         4            95            1.5         Null

I want the rows which take part in the calculation i.e from rows 1 to 3 only to have values and all the rows which do not take part in the calculation to be NULL. As we can see in the third row, the value is (4-6 = -2). Even though the value is negative this row is a part of the value "20". SO the value is set to 0 instead of -2 or NULL. If I change the condition to be (case when @x - "cumlativesum") < 0 then NULL, then all the rows where the value goes beyond 0 are set to NULL.

Any suggestions would be appreciated.

like image 285
Sangamesh Hs Avatar asked May 08 '26 15:05

Sangamesh Hs


1 Answers

You can get what you want by looking for the record where the cumulative sum passes/reaches 0 for the first time. You can do this by subtracting out the value you are accumulating. I think the query looks like:

SELECT "SUPPLIERID", "ARTIKEL",
       (case when @x - cumulativesum <= 0 and @x - (cumulativesum -BZBAS_AW) > 0
             then 0
             when @x - "cumulativesum" <= 0
             then NULL
             else @x - "cumulativesum"
        end) as "VALUE_DRILL_DOWN"
from (SELECT T1."ARTIKEL", T1."SUPPLIERID", T1.BZBAS_AW
             (select sum("BZBAS_AW")
              from EXAMPLE_TABLE T2
              where T2."ROW_TEST" <= T1."ROW_TEST" and T2."LIFNR" = T1."LIFNR" and T2."ARTIKEL"=T1."ARTIKEL"
             ) as "cumulativesum"
     from EXAMPLE_TABLE T1
     ) t
like image 183
Gordon Linoff Avatar answered May 11 '26 04:05

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!