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 ie 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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.