SQL UPDATE and CASE statement does not work

情到浓时终转凉″ 提交于 2021-01-29 15:09:07

问题


I have the following table "Sales" in MS SQL Server, where [FieldX] is not updated properly:

(..[My comment] is of course not an actual column in the table)

The reason why the last 3 rows should be = 3, is because I am updating the fields with the following rules:

with cte_previous_rows AS (
SELECT Staff_Id, LAG(FieldX) OVER (partition by Staff_Id ORDER by [date]) as Prev_Row
FROM Sales )

UPDATE Sales
SET FieldX = (CASE WHEN Staff_id_sales < 1500 AND ClosedSale = 0 THEN 0
           WHEN Staff_id_sales = 1500 and CosedSale = 0 THEN 5
           WHEN Staff_id_sales > 3000 and c.Prev_Row <> 2 and c.Prev_Row = 1 OR c.Prev_Row = 0 THEN 2
           WHEN Staff_id_sales > 3000 and c.Prev_Row = 2 or c.Prev_Row = 3 THEN 3 END) 
FROM Sales as Sales
JOIN cte_previous_rows as c
 on sales.staff_id = c.staff_id

The idea here is to identify what the previous row holds as value in [FieldX]. By "previous row" I refer to other rows with the SAME staff_id, and the closest previous [date]

The last two WHEN statements do not work as intended, as I get the output displayed in Sales table.

The correct output should look like this:

What am I doing wrong? How can it be fixed? Thank you.


回答1:


Please check the next query:

WITH cte_previous_rows AS (
    SELECT Date, Staff_Id, LAG(FieldX) OVER (partition by Staff_Id ORDER by [date]) as Prev_Row
FROM Sales 
) UPDATE Sales
SET FieldX = (CASE 
    WHEN Staff_id_sales < 1500 AND ClosedSale = 0 THEN 0
    WHEN Staff_id_sales = 1500 and ClosedSale = 0 THEN 5
    WHEN Staff_id_sales > 3000 and (c.Prev_Row = 1 OR c.Prev_Row = 0) THEN 2
    WHEN Staff_id_sales > 3000 and (c.Prev_Row = 2 or c.Prev_Row = 3) THEN 3 
    ELSE FieldX
END) 
FROM Sales
JOIN cte_previous_rows as c ON Sales.staff_id = c.staff_id AND Sales.Date = c.Date;

result:

+============+==========+================+============+========+
| Date       | Staff_Id | Staff_id_sales | ClosedSale | FieldX |
+============+==========+================+============+========+
| 2000-01-01 | 1        | 500            | 0          | 0      |
+------------+----------+----------------+------------+--------+
| 2001-01-01 | 2        | 200            | 0          | 0      |
+------------+----------+----------------+------------+--------+
| 2001-02-26 | 3        | 500            | 0          | 0      |
+------------+----------+----------------+------------+--------+
| 2001-01-25 | 4        | 1500           | 0          | 5      |
+------------+----------+----------------+------------+--------+
| 2001-03-25 | 4        | 1500           | 0          | 5      |
+------------+----------+----------------+------------+--------+
| 2001-03-25 | 5        | 2500           | 0          | 1      |
+------------+----------+----------------+------------+--------+
| 2001-04-25 | 5        | 3000           | 0          | 1      |
+------------+----------+----------------+------------+--------+
| 2001-05-25 | 5        | 3500           | 0          | 2      |
+------------+----------+----------------+------------+--------+
| 2001-05-26 | 5        | 4000           | 0          | 3      |
+------------+----------+----------------+------------+--------+
| 2001-05-27 | 5        | 5000           | 0          | 3      |
+------------+----------+----------------+------------+--------+
| 2001-05-28 | 5        | 7500           | 0          | 3      |
+------------+----------+----------------+------------+--------+


来源:https://stackoverflow.com/questions/65596838/sql-update-and-case-statement-does-not-work

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