Updating an is_latest column on redshift table
up vote
0
down vote
favorite
I'm attempting to update an is_latest
column on a redshift table grouped by source
and source_primary_key
with the following update statement, but getting an error that window functions are not allowed in update statements. What is the best way to go about this?
update my_schema.production_log
set is_latest =
case when run_start_time = max(run_start_time) over (partition by
source, source_primary_key)
then ‘t’ else ‘f’
end
sql amazon-redshift
add a comment |
up vote
0
down vote
favorite
I'm attempting to update an is_latest
column on a redshift table grouped by source
and source_primary_key
with the following update statement, but getting an error that window functions are not allowed in update statements. What is the best way to go about this?
update my_schema.production_log
set is_latest =
case when run_start_time = max(run_start_time) over (partition by
source, source_primary_key)
then ‘t’ else ‘f’
end
sql amazon-redshift
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm attempting to update an is_latest
column on a redshift table grouped by source
and source_primary_key
with the following update statement, but getting an error that window functions are not allowed in update statements. What is the best way to go about this?
update my_schema.production_log
set is_latest =
case when run_start_time = max(run_start_time) over (partition by
source, source_primary_key)
then ‘t’ else ‘f’
end
sql amazon-redshift
I'm attempting to update an is_latest
column on a redshift table grouped by source
and source_primary_key
with the following update statement, but getting an error that window functions are not allowed in update statements. What is the best way to go about this?
update my_schema.production_log
set is_latest =
case when run_start_time = max(run_start_time) over (partition by
source, source_primary_key)
then ‘t’ else ‘f’
end
sql amazon-redshift
sql amazon-redshift
edited Nov 20 at 1:03
asked Nov 20 at 0:35
willbarclay
62
62
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Redshift supports a FROM
clause in the update
statement. Try this:
update my_schema.production_log
set is_latest = (case when run_start_time = max_run_start_time
then 't' else 'f'
end)
from (select source, source_primary_key, max(run_start_time) as max_run_start_time
from my_schema.production_log
group by source, source_primary_key
) pl2
where pl2.source = production_log.source and
pl2.source_primary_key = production_log.source_primary_key;
add a comment |
up vote
0
down vote
You need to rewrite the query so it can join to a derived table:
UPDATE u
SET u.is_latest =CASE
WHEN u.run_start_time = j.max_time THEN 't'
ELSE 'f'
END
FROM my_schema.production_log AS u
INNER JOIN (
SELECT
source
, source_primary_key
, MAX( run_start_time ) max_time
FROM my_schema.production_log
GROUP BY
source
, source_primary_key
) AS j
ON u.source = j.source
AND u.source_primary_key = j.source_primary_key
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Redshift supports a FROM
clause in the update
statement. Try this:
update my_schema.production_log
set is_latest = (case when run_start_time = max_run_start_time
then 't' else 'f'
end)
from (select source, source_primary_key, max(run_start_time) as max_run_start_time
from my_schema.production_log
group by source, source_primary_key
) pl2
where pl2.source = production_log.source and
pl2.source_primary_key = production_log.source_primary_key;
add a comment |
up vote
0
down vote
Redshift supports a FROM
clause in the update
statement. Try this:
update my_schema.production_log
set is_latest = (case when run_start_time = max_run_start_time
then 't' else 'f'
end)
from (select source, source_primary_key, max(run_start_time) as max_run_start_time
from my_schema.production_log
group by source, source_primary_key
) pl2
where pl2.source = production_log.source and
pl2.source_primary_key = production_log.source_primary_key;
add a comment |
up vote
0
down vote
up vote
0
down vote
Redshift supports a FROM
clause in the update
statement. Try this:
update my_schema.production_log
set is_latest = (case when run_start_time = max_run_start_time
then 't' else 'f'
end)
from (select source, source_primary_key, max(run_start_time) as max_run_start_time
from my_schema.production_log
group by source, source_primary_key
) pl2
where pl2.source = production_log.source and
pl2.source_primary_key = production_log.source_primary_key;
Redshift supports a FROM
clause in the update
statement. Try this:
update my_schema.production_log
set is_latest = (case when run_start_time = max_run_start_time
then 't' else 'f'
end)
from (select source, source_primary_key, max(run_start_time) as max_run_start_time
from my_schema.production_log
group by source, source_primary_key
) pl2
where pl2.source = production_log.source and
pl2.source_primary_key = production_log.source_primary_key;
answered Nov 20 at 2:43
Gordon Linoff
750k34285391
750k34285391
add a comment |
add a comment |
up vote
0
down vote
You need to rewrite the query so it can join to a derived table:
UPDATE u
SET u.is_latest =CASE
WHEN u.run_start_time = j.max_time THEN 't'
ELSE 'f'
END
FROM my_schema.production_log AS u
INNER JOIN (
SELECT
source
, source_primary_key
, MAX( run_start_time ) max_time
FROM my_schema.production_log
GROUP BY
source
, source_primary_key
) AS j
ON u.source = j.source
AND u.source_primary_key = j.source_primary_key
add a comment |
up vote
0
down vote
You need to rewrite the query so it can join to a derived table:
UPDATE u
SET u.is_latest =CASE
WHEN u.run_start_time = j.max_time THEN 't'
ELSE 'f'
END
FROM my_schema.production_log AS u
INNER JOIN (
SELECT
source
, source_primary_key
, MAX( run_start_time ) max_time
FROM my_schema.production_log
GROUP BY
source
, source_primary_key
) AS j
ON u.source = j.source
AND u.source_primary_key = j.source_primary_key
add a comment |
up vote
0
down vote
up vote
0
down vote
You need to rewrite the query so it can join to a derived table:
UPDATE u
SET u.is_latest =CASE
WHEN u.run_start_time = j.max_time THEN 't'
ELSE 'f'
END
FROM my_schema.production_log AS u
INNER JOIN (
SELECT
source
, source_primary_key
, MAX( run_start_time ) max_time
FROM my_schema.production_log
GROUP BY
source
, source_primary_key
) AS j
ON u.source = j.source
AND u.source_primary_key = j.source_primary_key
You need to rewrite the query so it can join to a derived table:
UPDATE u
SET u.is_latest =CASE
WHEN u.run_start_time = j.max_time THEN 't'
ELSE 'f'
END
FROM my_schema.production_log AS u
INNER JOIN (
SELECT
source
, source_primary_key
, MAX( run_start_time ) max_time
FROM my_schema.production_log
GROUP BY
source
, source_primary_key
) AS j
ON u.source = j.source
AND u.source_primary_key = j.source_primary_key
answered Nov 20 at 2:50
Used_By_Already
21.9k21838
21.9k21838
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384602%2fupdating-an-is-latest-column-on-redshift-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown