如何在另一列中按句号减去
How to minus by period in another column
我需要负列中的结果,例如:
例如,我们取第一个结果 A = 23(1)
我们 34(2) - 23(1) = 11,然后 23(3) - 23(1)...
等等。对于每个类别。
+--------+----------+--------+-------+
| Period | Category | Result | Minus |
+--------+----------+--------+-------+
| 1 | A | 23 | n/a |
| 1 | B | 24 | n/a |
| 1 | C | 25 | n/a |
| 2 | A | 34 | 11 |
| 2 | B | 23 | -1 |
| 2 | C | 1 | -24 |
| 3 | A | 23 | 0 |
| 3 | B | 90 | 66 |
| 3 | C | 21 | -4 |
+--------+----------+--------+-------+
你能帮帮我吗?
我们可以在这里使用分区或引导吗?
你可以这样做:
select b.*, b.result - a.result as "minus"
from t a
join t b on b.category = a.category and a.period = 1
结果:
period category result minus
------- --------- ------- -----
1 A 23 0
1 B 24 0
1 C 25 0
2 A 34 11
2 B 23 -1
2 C 1 -24
3 A 23 0
3 B 90 66
3 C 21 -4
请参阅 DB Fiddle 中的 运行 示例。
SELECT
*,
Result - FIRST_VALUE(Result) OVER (PARTITION BY Category ORDER BY Period) AS Minus
FROM
yourTable
这不会创建 hello
值,而是创建 returns 0
。我不确定在整数列中返回任意字符串是否有意义,所以我没有这样做。
如果你真的需要避免 0
你可以使用 CASE
语句...
CASE WHEN 1 = Period
THEN NULL
ELSE Result - FIRST_VALUE(Result) OVER (PARTITION BY Category ORDER BY Period)
END
或者,更稳健...
CASE WHEN 1 = ROW_NUMBER() OVER (PARTITION BY Category ORDER BY Period)
THEN NULL
ELSE Result - FIRST_VALUE(Result) OVER (PARTITION BY Category ORDER BY Period)
END
(如有任何打字错误,我深表歉意,我在 phone。)
好的,只是不要重复我的问题
怎么做?
如果
对于每个新的子周期,我们应该重复 first_value 逻辑
+------------+----------+------------+----------+---------+
| Sub period | Period | Category | Result | Minus |
+------------+----------+------------+----------+---------+
| SA | 1 | A | 23 | n/a |
| SA | 2 | A | 34 | 11 |
| SA | 3 | A | 35 | 12 |
| SA | 4 | A | 36 | 13 |
| KS | 1 | A | 23 | n/a |
| KS | 2 | A | 21 | -2 |
| KS | 3 | A | 23 | 0 |
| KS | 4 | A | 21 | -2 |
+------------+----------+------------+----------+---------+
我需要负列中的结果,例如:
例如,我们取第一个结果 A = 23(1)
我们 34(2) - 23(1) = 11,然后 23(3) - 23(1)...
等等。对于每个类别。
+--------+----------+--------+-------+
| Period | Category | Result | Minus |
+--------+----------+--------+-------+
| 1 | A | 23 | n/a |
| 1 | B | 24 | n/a |
| 1 | C | 25 | n/a |
| 2 | A | 34 | 11 |
| 2 | B | 23 | -1 |
| 2 | C | 1 | -24 |
| 3 | A | 23 | 0 |
| 3 | B | 90 | 66 |
| 3 | C | 21 | -4 |
+--------+----------+--------+-------+
你能帮帮我吗?
我们可以在这里使用分区或引导吗?
你可以这样做:
select b.*, b.result - a.result as "minus"
from t a
join t b on b.category = a.category and a.period = 1
结果:
period category result minus
------- --------- ------- -----
1 A 23 0
1 B 24 0
1 C 25 0
2 A 34 11
2 B 23 -1
2 C 1 -24
3 A 23 0
3 B 90 66
3 C 21 -4
请参阅 DB Fiddle 中的 运行 示例。
SELECT
*,
Result - FIRST_VALUE(Result) OVER (PARTITION BY Category ORDER BY Period) AS Minus
FROM
yourTable
这不会创建 hello
值,而是创建 returns 0
。我不确定在整数列中返回任意字符串是否有意义,所以我没有这样做。
如果你真的需要避免 0
你可以使用 CASE
语句...
CASE WHEN 1 = Period
THEN NULL
ELSE Result - FIRST_VALUE(Result) OVER (PARTITION BY Category ORDER BY Period)
END
或者,更稳健...
CASE WHEN 1 = ROW_NUMBER() OVER (PARTITION BY Category ORDER BY Period)
THEN NULL
ELSE Result - FIRST_VALUE(Result) OVER (PARTITION BY Category ORDER BY Period)
END
(如有任何打字错误,我深表歉意,我在 phone。)
好的,只是不要重复我的问题
怎么做?
如果
对于每个新的子周期,我们应该重复 first_value 逻辑
+------------+----------+------------+----------+---------+
| Sub period | Period | Category | Result | Minus |
+------------+----------+------------+----------+---------+
| SA | 1 | A | 23 | n/a |
| SA | 2 | A | 34 | 11 |
| SA | 3 | A | 35 | 12 |
| SA | 4 | A | 36 | 13 |
| KS | 1 | A | 23 | n/a |
| KS | 2 | A | 21 | -2 |
| KS | 3 | A | 23 | 0 |
| KS | 4 | A | 21 | -2 |
+------------+----------+------------+----------+---------+