Query Result Not Returned As Expected

谁说胖子不能爱 提交于 2020-05-17 07:48:07

问题


I am struggling with a query that tried few days back with a suggestion of one of SO user and got it work with the following:

WITH your_table(ID,STOREDATE,VALUE,INFO)
AS
(
SELECT 1122,'1/1/2020',2,'DONE' UNION ALL
SELECT 1122,'1/2/2020',1,'DONE' UNION ALL
SELECT 1122,'1/3/2020',7,'DONE' UNION ALL
SELECT 1122,'1/4/2020',8,'DONE'
),

CTE AS
(
    SELECT ID,
    STOREDATE,
    VALUE,
    CASE 
        WHEN VALUE = 8 THEN 0
        WHEN VALUE + LAG(VALUE) OVER(ORDER BY STOREDATE) = 8 THEN 0
        WHEN VALUE + LEAD(VALUE) OVER(ORDER BY STOREDATE) = 8 THEN 0
        ELSE VALUE
    END VALUE2,
    INFO
    FROM your_table
)

SELECT *,
CASE 
    WHEN 
        (
            SELECT COUNT(*) FROM CTE A 
            WHERE A.VALUE2 = 0 AND A.STOREDATE < B.STOREDATE
        ) >= 1 AND B.VALUE = 8 THEN B.VALUE
    ELSE B.VALUE2
END VALUE3
FROM CTE B

N.B: My idea was to get the summation of 8 at any given row. So for the above input, my expected output is this.

Output:

ID      STOREDATE   VALUE   INFO
1122    1/1/2020    2       DONE
1122    1/2/2020    0           //1 + 7 = 8; it'll update both the rows with zero
1122    1/3/2020    0           //For this, it's just fine
1122    1/4/2020    8       DONE

Now my problem is with the below inputs that I can't figure out how to get expected result set.

Input:

ID      STOREDATE   VALUE   INFO
1122    1/1/2020    2       DONE
1122    1/2/2020    1       DONE
1122    1/3/2020    2       DONE
1122    1/4/2020    7       DONE

Expected Output:

ID      STOREDATE   VALUE   INFO
1122    1/1/2020    2       DONE
1122    1/2/2020    0       
1122    1/3/2020    2       DONE
1122    1/4/2020    0       

Input:

ID      STOREDATE   VALUE   INFO
1122    1/1/2020    2       DONE
1122    1/2/2020    2       DONE
1122    1/3/2020    2       DONE
1122    1/4/2020    2       DONE

Expected Output:

ID      STOREDATE   VALUE   INFO
1122    1/1/2020    0       
1122    1/2/2020    0       
1122    1/3/2020    0       
1122    1/4/2020    0       

I may have several ids to get the exact result as below but making tweaks to the query doesn't get me to what I wanted. Any suggestion would be appreciated - Thanks.

WITH your_table(ID,STOREDATE,VALUE,INFO)
AS
(
SELECT 1122,'1/1/2020',2,'DONE' UNION ALL
SELECT 1122,'1/2/2020',7,'DONE' UNION ALL
SELECT 1122,'1/3/2020',1,'DONE' UNION ALL
SELECT 1122,'1/4/2020',8,'DONE' UNION ALL
SELECT 4466,'1/1/2020',2,'DONE' UNION ALL
SELECT 4466,'1/2/2020',7,'DONE' UNION ALL
SELECT 4466,'1/3/2020',1,'DONE' UNION ALL
SELECT 4466,'1/4/2020',8,'DONE'
),

CTE AS
(
    SELECT ID,
    STOREDATE,
    VALUE,
    CASE 
        WHEN VALUE = 8 THEN 0
        WHEN VALUE + LAG(VALUE) OVER(ORDER BY STOREDATE) = 8 THEN 0
        WHEN VALUE + LEAD(VALUE) OVER(ORDER BY STOREDATE) = 8 THEN 0
        ELSE VALUE
    END VALUE2,
    INFO
    FROM your_table
)

SELECT *,
CASE 
    WHEN 
        (
            SELECT COUNT(*) FROM CTE A 
            WHERE A.VALUE2 = 0 AND A.STOREDATE < B.STOREDATE
        ) >= 1 AND B.VALUE = 8 THEN B.VALUE
    ELSE B.VALUE2
END VALUE3
FROM CTE B

Expected Output:

ID      STOREDATE   VALUE   INFO
1122    1/1/2020    2       DONE
1122    1/2/2020    0           
1122    1/3/2020    0           
1122    1/4/2020    8       DONE
4466    1/1/2020    2       DONE
4466    1/2/2020    0           
4466    1/3/2020    0           
4466    1/4/2020    8       DONE

Update 1: Please check the below

Input:

ID      STOREDATE   VALUE   INFO
1122    1/1/2020    2       DONE
1122    1/2/2020    1       DONE
1122    1/3/2020    2       DONE
1122    1/4/2020    7       DONE

Expected Output:

ID      STOREDATE   VALUE   INFO
1122    1/1/2020    2       DONE
1122    1/2/2020    0       
1122    1/3/2020    2       DONE
1122    1/4/2020    0       

Input:

ID      STOREDATE   VALUE   INFO
1122    1/1/2020    2       DONE
1122    1/2/2020    2       DONE
1122    1/3/2020    2       DONE
1122    1/4/2020    2       DONE

Expected Output:

ID      STOREDATE   VALUE   INFO
1122    1/1/2020    0       
1122    1/2/2020    0       
1122    1/3/2020    0       
1122    1/4/2020    0   

回答1:


Your first big mistake is that 'stordate' is not a date, but a character string representation of a date. Thus, all comparisons will be comparisons of a string, not a date. As a string, which comes first, '01/02/2020' or '02/01/2019'.

WITH your_table(ID,STOREDATE,VALUE,INFO)
AS
(
SELECT 1122,to_date('01/01/2020','dd/mm/yyyy'),2,'DONE' UNION ALL
SELECT 1122,to_date('01/02/2020','dd/mm/yyyy'),1,'DONE' UNION ALL
SELECT 1122,to_date('01/03/2020','dd/mm/yyyy'),7,'DONE' UNION ALL
SELECT 1122,to_date('01/04/2020','dd/mm/yyyy'),8,'DONE'
),



回答2:


Can you please check this bellow solution-

DEMO HERE

DEMO2 HERE

WITH your_table(ID,STOREDATE,VALUE,INFO)
AS
(
SELECT 1122,'1/1/2020',2,'DONE' UNION ALL
SELECT 1122,'1/2/2020',7,'DONE' UNION ALL -- This two returns zero
SELECT 1122,'1/3/2020',1,'DONE' UNION ALL -- As 1 and 7 have sum of 8; it should check once I mean the sum 
SELECT 1122,'1/4/2020',7,'DONE' UNION ALL -- Unfortunately this returns zero as well
SELECT 4466,'1/1/2020',2,'DONE' UNION ALL
SELECT 4466,'1/2/2020',7,'DONE' UNION ALL
SELECT 4466,'1/3/2020',1,'DONE' UNION ALL
SELECT 4466,'1/4/2020',8,'DONE'
),

CTE AS
(
    SELECT ID,
    STOREDATE,
    VALUE,
    CASE 
        WHEN VALUE = 8 THEN 0
        WHEN VALUE + LAG(VALUE) OVER(ORDER BY ID, STOREDATE) = 8 
            AND ID = LAG(ID) OVER(ORDER BY ID, STOREDATE) THEN 0
        WHEN VALUE + LEAD(VALUE) OVER(ORDER BY ID, STOREDATE) = 8 
            AND ID = LEAD(ID) OVER(ORDER BY ID, STOREDATE) THEN 0
        ELSE VALUE
    END VALUE2,
    INFO
    FROM your_table
)

SELECT *,
CASE 
    WHEN 
        (
            SELECT COUNT(*) FROM CTE A 
            WHERE A.VALUE2 = 0 AND A.STOREDATE < B.STOREDATE
            AND A.ID = B.ID
        ) >= 1 AND B.VALUE = 8 THEN B.VALUE

    WHEN
        (
            SELECT SUM(A.VALUE) 
            FROM CTE A 
            WHERE A.VALUE2 = 0 
            AND A.STOREDATE < B.STOREDATE
            AND A.ID = B.ID
        )>= 8  THEN B.VALUE

    ELSE B.VALUE2
END VALUE3
FROM CTE B


来源:https://stackoverflow.com/questions/60905844/query-result-not-returned-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!