SQL UPDATE in a SELECT rank over Partition sentence
SQL UPDATE in a SELECT rank over Partition sentence
这是我的问题,我有一个 table 这样的:
Company, direction, type, year, month, value, rank
当我创建 table 时,等级默认为 0,我想要的是使用此 select:
更新 table 中的等级
SELECT company, direction, type, year, month, value, rank() OVER (PARTITION BY direction, type, year, month ORDER BY value DESC) as rank
FROM table1
GROUP BY company, direction, type, year, month, value
ORDER BY company, direction, type, year, month, value;
这个Select工作正常,但我找不到用它来更新的方法table1
我还没有找到用这种句子解决类似问题的答案。如果有人可以给我任何关于是否可行的建议,我将不胜感激。
谢谢!
您可以加入 子查询 并执行 UPDATE:
UPDATE table_name t2
SET t2.rank=
SELECT t1.rank FROM(
SELECT company,
direction,
type,
YEAR,
MONTH,
value,
rank() OVER (PARTITION BY direction, type, YEAR, MONTH ORDER BY value DESC) AS rank
FROM table_name
GROUP BY company,
direction,
TYPE,
YEAR,
MONTH,
VALUE
ORDER BY company,
direction,
TYPE,
YEAR,
MONTH,
VALUE
) t1
WHERE t1.company = t2.company
AND t1.direction = t2.direction;
向谓词添加所需条件。
或者,
您可以使用 MERGE 并将该查询保留在 USING 子句中:
MERGE INTO table_name t USING
(SELECT company,
direction,
TYPE,
YEAR,
MONTH,
VALUE,
rank() OVER (PARTITION BY direction, TYPE, YEAR, MONTH ORDER BY VALUE DESC) AS rank
FROM table1
GROUP BY company,
direction,
TYPE,
YEAR,
MONTH,
VALUE
ORDER BY company,
direction,
TYPE,
YEAR,
MONTH,
VALUE
) s
ON(t.company = s.company AND t.direction = s.direction)
WHEN MATCHED THEN
UPDATE SET t.rank = s.rank;
在 ON 子句中添加必要的条件。
你可以像这样简单地完成它。这不是众所周知,但你可以在 SELECT
上制作 UPDATE
UPDATE
(SELECT
company, direction, TYPE, YEAR, MONTH, VALUE,
RANK() OVER (PARTITION BY direction, TYPE, YEAR, MONTH ORDER BY VALUE DESC) AS NEW_RANK
FROM table1) a
SET RANK = NEW_RANK;
这是我的问题,我有一个 table 这样的:
Company, direction, type, year, month, value, rank
当我创建 table 时,等级默认为 0,我想要的是使用此 select:
更新 table 中的等级SELECT company, direction, type, year, month, value, rank() OVER (PARTITION BY direction, type, year, month ORDER BY value DESC) as rank
FROM table1
GROUP BY company, direction, type, year, month, value
ORDER BY company, direction, type, year, month, value;
这个Select工作正常,但我找不到用它来更新的方法table1
我还没有找到用这种句子解决类似问题的答案。如果有人可以给我任何关于是否可行的建议,我将不胜感激。
谢谢!
您可以加入 子查询 并执行 UPDATE:
UPDATE table_name t2
SET t2.rank=
SELECT t1.rank FROM(
SELECT company,
direction,
type,
YEAR,
MONTH,
value,
rank() OVER (PARTITION BY direction, type, YEAR, MONTH ORDER BY value DESC) AS rank
FROM table_name
GROUP BY company,
direction,
TYPE,
YEAR,
MONTH,
VALUE
ORDER BY company,
direction,
TYPE,
YEAR,
MONTH,
VALUE
) t1
WHERE t1.company = t2.company
AND t1.direction = t2.direction;
向谓词添加所需条件。
或者,
您可以使用 MERGE 并将该查询保留在 USING 子句中:
MERGE INTO table_name t USING
(SELECT company,
direction,
TYPE,
YEAR,
MONTH,
VALUE,
rank() OVER (PARTITION BY direction, TYPE, YEAR, MONTH ORDER BY VALUE DESC) AS rank
FROM table1
GROUP BY company,
direction,
TYPE,
YEAR,
MONTH,
VALUE
ORDER BY company,
direction,
TYPE,
YEAR,
MONTH,
VALUE
) s
ON(t.company = s.company AND t.direction = s.direction)
WHEN MATCHED THEN
UPDATE SET t.rank = s.rank;
在 ON 子句中添加必要的条件。
你可以像这样简单地完成它。这不是众所周知,但你可以在 SELECT
UPDATE
UPDATE
(SELECT
company, direction, TYPE, YEAR, MONTH, VALUE,
RANK() OVER (PARTITION BY direction, TYPE, YEAR, MONTH ORDER BY VALUE DESC) AS NEW_RANK
FROM table1) a
SET RANK = NEW_RANK;