SQL Server中按组按逻辑条件替换值

时间:2018-11-04 11:38:49

标签: sql sql-server ssms

这是我的样本数据

   shop_code product_id                doc_date ship_count mark_1 outputer y
-----------------------------------------------------------------------------
1   00664НСК      11628 2015-01-03 00:00:00.000         12      1        8 1
2   00664НСК      11628 2015-01-05 00:00:00.000          7      1        8 1
3   00664НСК      11628 2015-01-06 00:00:00.000         24      0        8 1
4   00664НСК      11628 2015-01-07 00:00:00.000         18      1        8 1
5   00664НСК      11628 2015-01-08 00:00:00.000         12      1        8 1
6   00664НСК      11628 2015-01-09 00:00:00.000         18      0        8 1
7   00664НСК      11628 2015-01-10 00:00:00.000          6      0        6 1
8   00664НСК      11628 2015-01-11 00:00:00.000          6      1        6 1
9   00664НСК      11628 2015-01-12 00:00:00.000          6      1        6 1
10  00664НСК      11628 2015-01-13 00:00:00.000         18      1       12 0
11  00664НСК      11628 2015-01-14 00:00:00.000          6      1        6 0
12  00664НСК      11628 2015-01-15 00:00:00.000         18      1       12 0
13  00664НСК      11628 2015-01-16 00:00:00.000         12      1       12 1
14  00664НСК      11628 2015-01-17 00:00:00.000         18      1       12 1
15  00664НСК      11628 2015-01-18 00:00:00.000         12      1       12 1
16  00664НСК      11628 2015-01-19 00:00:00.000         10      1       10 0
17  00664НСК      11628 2015-01-20 00:00:00.000         24      1       12 0
18  00664НСК      11628 2015-01-21 00:00:00.000          6      1        6 0
19  00664НСК      11628 2015-01-24 00:00:00.000          6      1        6 0
20  00664НСК      11628 2015-01-25 00:00:00.000          6      0        6 0
21  00664НСК      11628 2015-01-26 00:00:00.000         10      0       10 1
22  00664НСК      11628 2015-01-27 00:00:00.000          6      1        6 0
23  00664НСК      11628 2015-01-28 00:00:00.000         10      1       10 0
24  00664НСК      11628 2015-01-29 00:00:00.000         70      0       12 1
25  00664НСК      11628 2015-01-30 00:00:00.000        100      1       12 1

类似的问题,我问过R并得到了可行的解决方案,但现在我想使用T-SQL来完成。

我需要观察这样的条件:如果y = 1且mark1 = 1,则必须将mark1 = 1的输出替换为输出变量中y = 0且mark1 = 1的第一个值。

如果输出中第一个用于Y = 0且mark1 = 1的值大于ship_count,则在输出中保留ship_count的实际值

用于输出的mark1的零类别,我们不会碰。

此操作必须由ship_code + product_id组完成

因此所需的输出应如下所示:

shop_code product_id                doc_date ship_count mark_1 outputer   y
----------------------------------------------------------------------------
1   00664НСК      11628 2015-01-03 00:00:00.000         12      1       *12 1
2   00664НСК      11628 2015-01-05 00:00:00.000          7      1       *7 1
3   00664НСК      11628 2015-01-06 00:00:00.000         24      0       24 1
4   00664НСК      11628 2015-01-07 00:00:00.000         18      1       *12 1
5   00664НСК      11628 2015-01-08 00:00:00.000         12      1       *12 1
6   00664НСК      11628 2015-01-09 00:00:00.000         18      0       18 1
7   00664НСК      11628 2015-01-10 00:00:00.000          6      0        6 1
8   00664НСК      11628 2015-01-11 00:00:00.000          6      1        6 1
9   00664НСК      11628 2015-01-12 00:00:00.000          6      1        6 1
10  00664НСК      11628 2015-01-13 00:00:00.000         18      1       *12 0
11  00664НСК      11628 2015-01-14 00:00:00.000          6      1        6 0
12  00664НСК      11628 2015-01-15 00:00:00.000         18      1       12 0
13  00664НСК      11628 2015-01-16 00:00:00.000         12      1       *10 1
14  00664НСК      11628 2015-01-17 00:00:00.000         18      1       *10 1
15  00664НСК      11628 2015-01-18 00:00:00.000         12      1       *10 1
16  00664НСК      11628 2015-01-19 00:00:00.000         10      1       10 0
17  00664НСК      11628 2015-01-20 00:00:00.000         24      1       12 0
18  00664НСК      11628 2015-01-21 00:00:00.000          6      1        6 0
19  00664НСК      11628 2015-01-24 00:00:00.000          6      1        6 0
20  00664НСК      11628 2015-01-25 00:00:00.000          6      0        6 0
21  00664НСК      11628 2015-01-26 00:00:00.000         10      0       10 1
22  00664НСК      11628 2015-01-27 00:00:00.000          6      1        6 1
23  00664НСК      11628 2015-01-28 00:00:00.000         20      1       *12 0
24  00664НСК      11628 2015-01-29 00:00:00.000         70      1       12 0
25  00664НСК      11628 2015-01-30 00:00:00.000        100      1       12 1

1 个答案:

答案 0 :(得分:1)

晚上好

您应该使用案例声明来完成工作。

要查找描述子句的第一个值,请使用子查询,在该子查询中保留所需的顺序(order by)并选择前1个值。

尝试一下,如果您遇到一些问题,请再次询问。