我正在努力尝试几天前提出的 SO 用户之一的建议,并使其与以下各项一起使用:
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
NB :我的想法是在任何给定的行中获得8的总和。因此,对于上面的输入,我的预期输出是这样。
输出:
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
现在我的问题是以下输入内容,我无法弄清楚如何获得预期的结果集。
输入:
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
预期产量:
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
输入:
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
预期产量:
ID STOREDATE VALUE INFO
1122 1/1/2020 0
1122 1/2/2020 0
1122 1/3/2020 0
1122 1/4/2020 0
我可能有几个ID来获得如下所示的准确结果,但是对查询进行调整并不能使我了解所需的内容。任何建议将不胜感激-谢谢。
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
预期产量:
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
更新1 :请检查以下内容
输入:
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
预期产量:
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
输入:
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
预期产量:
ID STOREDATE VALUE INFO
1122 1/1/2020 0
1122 1/2/2020 0
1122 1/3/2020 0
1122 1/4/2020 0
答案 0 :(得分:2)
您的第一个大错误是'stordate'不是日期,而是日期的字符串表示形式。因此,所有比较将是字符串的比较,而不是日期。作为字符串,首先是'01 / 02/2020'或'02 / 01/2019'。
import torch
torch.manual_seed(2020)
m = torch.tensor([[0, 1, 0]]).to(torch.int32)
x = torch.rand((1, 3, 2))
o = torch.rand((1, 3, 2))
print(o)
# tensor([[[0.5899, 0.8105],
# [0.2512, 0.6307],
# [0.5403, 0.8033]]])
print(x)
# tensor([[[0.4869, 0.1052],
# [0.5883, 0.1161],
# [0.4949, 0.2824]]])
o[:, m[0].to(torch.bool), :] = x[:, m[0].to(torch.bool), :]
# or
# o[:, m[0] == 1, :] = x[:, m[0] == 1, :]
print(o)
# tensor([[[0.5899, 0.8105],
# [0.5883, 0.1161],
# [0.5403, 0.8033]]])
答案 1 :(得分:1)
能否请您查看以下解决方案-
1
2020-03-29 01:45:37.099114
2
2020-03-29 01:45:37.099114
c
2020-03-29 01:45:52.114733
6
2020-03-29 01:45:52.114733
10
2020-03-29 01:45:52.114733
1
2020-03-29 01:45:52.114733
e
2020-03-29 01:46:07.115963
11
2020-03-29 01:46:07.115963
78
2020-03-29 01:46:07.115963
a
2020-03-29 01:46:22.117232
b
2020-03-29 01:46:37.118046