RANK used in 'where' returns invalid column, but exists in results setUsing column alias in a WHERE clause doesn't workNon-integer primary key considerationsPostgreSQL DELETE FROM fails with `Error: attempted to delete invisible tuple`search_path set and seen but not used!Index not used with many WHERE LOWER(column) IN (…) conditions, works fine with just a fewMulti-Column index not used for index-only scan, but partial index ispostgresql random function returns duplicate results when order by random used in queryQuery returns results on pgAdmin running on remote client but not directly through psql on db serverPostgres, `where` condition, sort by first column, but I don't want that
Find "a" so that 99*a=1a1
Features seen on the Space Shuttle's solid booster; what does "LOADED" mean exactly?
Was Robin Hood's point of view ethically sound?
Why is the the worst case for this function O(n*n)
How should we understand "unobscured by flying friends" in this context?
How do you say "to hell with everything" in French?
Does the 2019 UA artificer need to prepare the Lesser Restoration spell to cast it with their Alchemical Mastery feature?
Why would an airport be depicted with symbology for runways longer than 8,069 feet even though it is reported on the sectional as 7,200 feet?
Why do the British opposition parties not want a new election?
Do you need to burn fuel between gravity assists?
When does order matter in probability?
Friend is very nitpicky about side comments I don't intend to be taken too seriously
How can I return only the number of paired values in array?
The meaning of "offing" in "an agreement in the offing"
How can Schrödinger's cat be both dead and alive?
What exactly is Apple Cider
Remove outer padding in tikzcd
Word for something that used to be popular but not anymore
The Green Glass Door, Revisited
What's the biggest difference between these two photos?
Bit floating sequence
The pirate treasure of Leatherback Atoll
What is the purpose of the rotating plate in front of the lock?
Supervisor wants me to support a diploma-thesis software tool after I graduated
RANK used in 'where' returns invalid column, but exists in results set
Using column alias in a WHERE clause doesn't workNon-integer primary key considerationsPostgreSQL DELETE FROM fails with `Error: attempted to delete invisible tuple`search_path set and seen but not used!Index not used with many WHERE LOWER(column) IN (…) conditions, works fine with just a fewMulti-Column index not used for index-only scan, but partial index ispostgresql random function returns duplicate results when order by random used in queryQuery returns results on pgAdmin running on remote client but not directly through psql on db serverPostgres, `where` condition, sort by first column, but I don't want that
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
been trying to get this query to work, it's almost perfect , but I just need to add the commented out 'where' condition and it'll do exactly what I want, but when I uncomment,I get an error 'column doesnt exist'. Been having trouble figuring out the syntax for this, would appreciate any help!
I basically have a table with date, timestamp, DID, coordinates
I want a query that will return rows with the last coordinate from day X, and the first coord from day X+1 , and the coordinates (so it will only return results where there are 2 consecutive dates)
This is what I came up with
SELECT a.timestamp_intersecting_date d1,b.timestamp_intersecting_date d2,
a."DID",
a.timestamp_intersecting_max t1, b.timestamp_intersecting_min t2,
RANK () OVER (
PARTITION BY a.timestamp_intersecting_date
ORDER BY a.timestamp_intersecting_max DESC
) timestamp_d1_rank ,
RANK () OVER (
PARTITION BY b.timestamp_intersecting_date
ORDER BY b.timestamp_intersecting_max ASC
) timestamp_d2_rank,
a.coords_centroid, b.coords_centroid
FROM
signals a
INNER JOIN signals b ON (a."DID" = b."DID")
WHERE (b.timestamp_intersecting_date = a.timestamp_intersecting_date + INTERVAL '1 DAY')
AND a."DID" = b."DID"
--AND timestamp_d1_rank = 1
--AND timestamp_d2_rank = 1
ORDER BY a."DID", t1 desc, t2 asc
thanks!
postgresql
add a comment |
been trying to get this query to work, it's almost perfect , but I just need to add the commented out 'where' condition and it'll do exactly what I want, but when I uncomment,I get an error 'column doesnt exist'. Been having trouble figuring out the syntax for this, would appreciate any help!
I basically have a table with date, timestamp, DID, coordinates
I want a query that will return rows with the last coordinate from day X, and the first coord from day X+1 , and the coordinates (so it will only return results where there are 2 consecutive dates)
This is what I came up with
SELECT a.timestamp_intersecting_date d1,b.timestamp_intersecting_date d2,
a."DID",
a.timestamp_intersecting_max t1, b.timestamp_intersecting_min t2,
RANK () OVER (
PARTITION BY a.timestamp_intersecting_date
ORDER BY a.timestamp_intersecting_max DESC
) timestamp_d1_rank ,
RANK () OVER (
PARTITION BY b.timestamp_intersecting_date
ORDER BY b.timestamp_intersecting_max ASC
) timestamp_d2_rank,
a.coords_centroid, b.coords_centroid
FROM
signals a
INNER JOIN signals b ON (a."DID" = b."DID")
WHERE (b.timestamp_intersecting_date = a.timestamp_intersecting_date + INTERVAL '1 DAY')
AND a."DID" = b."DID"
--AND timestamp_d1_rank = 1
--AND timestamp_d2_rank = 1
ORDER BY a."DID", t1 desc, t2 asc
thanks!
postgresql
Yes, you can’t refer to them in the where clause as window functions are evaluated after the where clause. You can put your query into a with clause, or wrap it as a subquery.
– Colin 't Hart
4 hours ago
add a comment |
been trying to get this query to work, it's almost perfect , but I just need to add the commented out 'where' condition and it'll do exactly what I want, but when I uncomment,I get an error 'column doesnt exist'. Been having trouble figuring out the syntax for this, would appreciate any help!
I basically have a table with date, timestamp, DID, coordinates
I want a query that will return rows with the last coordinate from day X, and the first coord from day X+1 , and the coordinates (so it will only return results where there are 2 consecutive dates)
This is what I came up with
SELECT a.timestamp_intersecting_date d1,b.timestamp_intersecting_date d2,
a."DID",
a.timestamp_intersecting_max t1, b.timestamp_intersecting_min t2,
RANK () OVER (
PARTITION BY a.timestamp_intersecting_date
ORDER BY a.timestamp_intersecting_max DESC
) timestamp_d1_rank ,
RANK () OVER (
PARTITION BY b.timestamp_intersecting_date
ORDER BY b.timestamp_intersecting_max ASC
) timestamp_d2_rank,
a.coords_centroid, b.coords_centroid
FROM
signals a
INNER JOIN signals b ON (a."DID" = b."DID")
WHERE (b.timestamp_intersecting_date = a.timestamp_intersecting_date + INTERVAL '1 DAY')
AND a."DID" = b."DID"
--AND timestamp_d1_rank = 1
--AND timestamp_d2_rank = 1
ORDER BY a."DID", t1 desc, t2 asc
thanks!
postgresql
been trying to get this query to work, it's almost perfect , but I just need to add the commented out 'where' condition and it'll do exactly what I want, but when I uncomment,I get an error 'column doesnt exist'. Been having trouble figuring out the syntax for this, would appreciate any help!
I basically have a table with date, timestamp, DID, coordinates
I want a query that will return rows with the last coordinate from day X, and the first coord from day X+1 , and the coordinates (so it will only return results where there are 2 consecutive dates)
This is what I came up with
SELECT a.timestamp_intersecting_date d1,b.timestamp_intersecting_date d2,
a."DID",
a.timestamp_intersecting_max t1, b.timestamp_intersecting_min t2,
RANK () OVER (
PARTITION BY a.timestamp_intersecting_date
ORDER BY a.timestamp_intersecting_max DESC
) timestamp_d1_rank ,
RANK () OVER (
PARTITION BY b.timestamp_intersecting_date
ORDER BY b.timestamp_intersecting_max ASC
) timestamp_d2_rank,
a.coords_centroid, b.coords_centroid
FROM
signals a
INNER JOIN signals b ON (a."DID" = b."DID")
WHERE (b.timestamp_intersecting_date = a.timestamp_intersecting_date + INTERVAL '1 DAY')
AND a."DID" = b."DID"
--AND timestamp_d1_rank = 1
--AND timestamp_d2_rank = 1
ORDER BY a."DID", t1 desc, t2 asc
thanks!
postgresql
postgresql
edited 14 hours ago
roy naufal
asked 15 hours ago
roy naufalroy naufal
1274 bronze badges
1274 bronze badges
Yes, you can’t refer to them in the where clause as window functions are evaluated after the where clause. You can put your query into a with clause, or wrap it as a subquery.
– Colin 't Hart
4 hours ago
add a comment |
Yes, you can’t refer to them in the where clause as window functions are evaluated after the where clause. You can put your query into a with clause, or wrap it as a subquery.
– Colin 't Hart
4 hours ago
Yes, you can’t refer to them in the where clause as window functions are evaluated after the where clause. You can put your query into a with clause, or wrap it as a subquery.
– Colin 't Hart
4 hours ago
Yes, you can’t refer to them in the where clause as window functions are evaluated after the where clause. You can put your query into a with clause, or wrap it as a subquery.
– Colin 't Hart
4 hours ago
add a comment |
2 Answers
2
active
oldest
votes
You cannot use window/ranking functions or their aliases in the WHERE
clause because they are evaluated in SELECT
, after the WHERE
has been evaluated.
You can use a subquery (derived table or CTE) to process a second WHERE
clause, after the window functions have been evaluated:
SELECT d1, d2,
"DID",
t1, t2,
coords_centroid_a,
coords_centroid_b
FROM
(
SELECT a.timestamp_intersecting_date d1, b.timestamp_intersecting_date d2,
a."DID",
a.timestamp_intersecting_max t1, b.timestamp_intersecting_min t2,
RANK () OVER (
PARTITION BY a.timestamp_intersecting_date
ORDER BY a.timestamp_intersecting_max DESC
) timestamp_d1_rank ,
RANK () OVER (
PARTITION BY b.timestamp_intersecting_date
ORDER BY b.timestamp_intersecting_max ASC
) timestamp_d2_rank,
a.coords_centroid AS coords_centroid_a,
b.coords_centroid AS coords_centroid_b
FROM
signals a
INNER JOIN signals b ON (a."DID" = b."DID")
WHERE (b.timestamp_intersecting_date = a.timestamp_intersecting_date + INTERVAL '1 DAY')
AND a."DID" = b."DID"
) AS t
WHERE t.timestamp_d1_rank = 1
AND t.timestamp_d2_rank = 1
ORDER BY "DID", t1 DESC, t2 ASC ;
thanks, that worked...i tried something similar but using WITH t AS ( slect ..., what you put in th from statement) select t.*, from t then joins where ranks=1/2, it was forcing me to add the columsn to group by or use aggregates, it ended up looking like this, mind telling me my mess up ? thanks pastebin.com/5aN3E29k
– roy naufal
2 hours ago
Ah, you have bothGROUP BY a."DID"
to get MAX/MIN and window functions there. There are ways that that's valid but it's usually complicated.
– ypercubeᵀᴹ
53 mins ago
If my query above solved the issue, use it. You cab use a CTE of course but that may result in different performance, as Postgres materializes CTE results (at least in versions less than 12).
– ypercubeᵀᴹ
52 mins ago
add a comment |
You cannot use aliases in WHERE
clauses!
To demonstrate, consider this example (fiddle).
CREATE TABLE foo (bar INT);
Populate it:
INSERT INTO foo VALUES (1), (2), (3);
Then
SELECT bar AS baz
FROM foo
WHERE baz = 2;
Causes the error:
ERROR: column "baz" does not exist
LINE 2: where baz = 2;
^
HINT: Perhaps you meant to reference the column "foo.bar".
You didn't post your error message or PostgreSQL version, but your problem is that timestamp_d1_rank
and timestamp_d2_rank
are aliases.
Maybe the PostgreSQL hint isn't obvious in your case because of the Window function? I'm afraid you're going to have to put the entire expression into your WHERE
clause! Or, you could consider using a CTE
(i.e. WITH
clause) as outlined here.
For a good explanation of this issue, see here.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2fdba.stackexchange.com%2fquestions%2f247275%2frank-used-in-where-returns-invalid-column-but-exists-in-results-set%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You cannot use window/ranking functions or their aliases in the WHERE
clause because they are evaluated in SELECT
, after the WHERE
has been evaluated.
You can use a subquery (derived table or CTE) to process a second WHERE
clause, after the window functions have been evaluated:
SELECT d1, d2,
"DID",
t1, t2,
coords_centroid_a,
coords_centroid_b
FROM
(
SELECT a.timestamp_intersecting_date d1, b.timestamp_intersecting_date d2,
a."DID",
a.timestamp_intersecting_max t1, b.timestamp_intersecting_min t2,
RANK () OVER (
PARTITION BY a.timestamp_intersecting_date
ORDER BY a.timestamp_intersecting_max DESC
) timestamp_d1_rank ,
RANK () OVER (
PARTITION BY b.timestamp_intersecting_date
ORDER BY b.timestamp_intersecting_max ASC
) timestamp_d2_rank,
a.coords_centroid AS coords_centroid_a,
b.coords_centroid AS coords_centroid_b
FROM
signals a
INNER JOIN signals b ON (a."DID" = b."DID")
WHERE (b.timestamp_intersecting_date = a.timestamp_intersecting_date + INTERVAL '1 DAY')
AND a."DID" = b."DID"
) AS t
WHERE t.timestamp_d1_rank = 1
AND t.timestamp_d2_rank = 1
ORDER BY "DID", t1 DESC, t2 ASC ;
thanks, that worked...i tried something similar but using WITH t AS ( slect ..., what you put in th from statement) select t.*, from t then joins where ranks=1/2, it was forcing me to add the columsn to group by or use aggregates, it ended up looking like this, mind telling me my mess up ? thanks pastebin.com/5aN3E29k
– roy naufal
2 hours ago
Ah, you have bothGROUP BY a."DID"
to get MAX/MIN and window functions there. There are ways that that's valid but it's usually complicated.
– ypercubeᵀᴹ
53 mins ago
If my query above solved the issue, use it. You cab use a CTE of course but that may result in different performance, as Postgres materializes CTE results (at least in versions less than 12).
– ypercubeᵀᴹ
52 mins ago
add a comment |
You cannot use window/ranking functions or their aliases in the WHERE
clause because they are evaluated in SELECT
, after the WHERE
has been evaluated.
You can use a subquery (derived table or CTE) to process a second WHERE
clause, after the window functions have been evaluated:
SELECT d1, d2,
"DID",
t1, t2,
coords_centroid_a,
coords_centroid_b
FROM
(
SELECT a.timestamp_intersecting_date d1, b.timestamp_intersecting_date d2,
a."DID",
a.timestamp_intersecting_max t1, b.timestamp_intersecting_min t2,
RANK () OVER (
PARTITION BY a.timestamp_intersecting_date
ORDER BY a.timestamp_intersecting_max DESC
) timestamp_d1_rank ,
RANK () OVER (
PARTITION BY b.timestamp_intersecting_date
ORDER BY b.timestamp_intersecting_max ASC
) timestamp_d2_rank,
a.coords_centroid AS coords_centroid_a,
b.coords_centroid AS coords_centroid_b
FROM
signals a
INNER JOIN signals b ON (a."DID" = b."DID")
WHERE (b.timestamp_intersecting_date = a.timestamp_intersecting_date + INTERVAL '1 DAY')
AND a."DID" = b."DID"
) AS t
WHERE t.timestamp_d1_rank = 1
AND t.timestamp_d2_rank = 1
ORDER BY "DID", t1 DESC, t2 ASC ;
thanks, that worked...i tried something similar but using WITH t AS ( slect ..., what you put in th from statement) select t.*, from t then joins where ranks=1/2, it was forcing me to add the columsn to group by or use aggregates, it ended up looking like this, mind telling me my mess up ? thanks pastebin.com/5aN3E29k
– roy naufal
2 hours ago
Ah, you have bothGROUP BY a."DID"
to get MAX/MIN and window functions there. There are ways that that's valid but it's usually complicated.
– ypercubeᵀᴹ
53 mins ago
If my query above solved the issue, use it. You cab use a CTE of course but that may result in different performance, as Postgres materializes CTE results (at least in versions less than 12).
– ypercubeᵀᴹ
52 mins ago
add a comment |
You cannot use window/ranking functions or their aliases in the WHERE
clause because they are evaluated in SELECT
, after the WHERE
has been evaluated.
You can use a subquery (derived table or CTE) to process a second WHERE
clause, after the window functions have been evaluated:
SELECT d1, d2,
"DID",
t1, t2,
coords_centroid_a,
coords_centroid_b
FROM
(
SELECT a.timestamp_intersecting_date d1, b.timestamp_intersecting_date d2,
a."DID",
a.timestamp_intersecting_max t1, b.timestamp_intersecting_min t2,
RANK () OVER (
PARTITION BY a.timestamp_intersecting_date
ORDER BY a.timestamp_intersecting_max DESC
) timestamp_d1_rank ,
RANK () OVER (
PARTITION BY b.timestamp_intersecting_date
ORDER BY b.timestamp_intersecting_max ASC
) timestamp_d2_rank,
a.coords_centroid AS coords_centroid_a,
b.coords_centroid AS coords_centroid_b
FROM
signals a
INNER JOIN signals b ON (a."DID" = b."DID")
WHERE (b.timestamp_intersecting_date = a.timestamp_intersecting_date + INTERVAL '1 DAY')
AND a."DID" = b."DID"
) AS t
WHERE t.timestamp_d1_rank = 1
AND t.timestamp_d2_rank = 1
ORDER BY "DID", t1 DESC, t2 ASC ;
You cannot use window/ranking functions or their aliases in the WHERE
clause because they are evaluated in SELECT
, after the WHERE
has been evaluated.
You can use a subquery (derived table or CTE) to process a second WHERE
clause, after the window functions have been evaluated:
SELECT d1, d2,
"DID",
t1, t2,
coords_centroid_a,
coords_centroid_b
FROM
(
SELECT a.timestamp_intersecting_date d1, b.timestamp_intersecting_date d2,
a."DID",
a.timestamp_intersecting_max t1, b.timestamp_intersecting_min t2,
RANK () OVER (
PARTITION BY a.timestamp_intersecting_date
ORDER BY a.timestamp_intersecting_max DESC
) timestamp_d1_rank ,
RANK () OVER (
PARTITION BY b.timestamp_intersecting_date
ORDER BY b.timestamp_intersecting_max ASC
) timestamp_d2_rank,
a.coords_centroid AS coords_centroid_a,
b.coords_centroid AS coords_centroid_b
FROM
signals a
INNER JOIN signals b ON (a."DID" = b."DID")
WHERE (b.timestamp_intersecting_date = a.timestamp_intersecting_date + INTERVAL '1 DAY')
AND a."DID" = b."DID"
) AS t
WHERE t.timestamp_d1_rank = 1
AND t.timestamp_d2_rank = 1
ORDER BY "DID", t1 DESC, t2 ASC ;
answered 5 hours ago
ypercubeᵀᴹypercubeᵀᴹ
81k11 gold badges140 silver badges230 bronze badges
81k11 gold badges140 silver badges230 bronze badges
thanks, that worked...i tried something similar but using WITH t AS ( slect ..., what you put in th from statement) select t.*, from t then joins where ranks=1/2, it was forcing me to add the columsn to group by or use aggregates, it ended up looking like this, mind telling me my mess up ? thanks pastebin.com/5aN3E29k
– roy naufal
2 hours ago
Ah, you have bothGROUP BY a."DID"
to get MAX/MIN and window functions there. There are ways that that's valid but it's usually complicated.
– ypercubeᵀᴹ
53 mins ago
If my query above solved the issue, use it. You cab use a CTE of course but that may result in different performance, as Postgres materializes CTE results (at least in versions less than 12).
– ypercubeᵀᴹ
52 mins ago
add a comment |
thanks, that worked...i tried something similar but using WITH t AS ( slect ..., what you put in th from statement) select t.*, from t then joins where ranks=1/2, it was forcing me to add the columsn to group by or use aggregates, it ended up looking like this, mind telling me my mess up ? thanks pastebin.com/5aN3E29k
– roy naufal
2 hours ago
Ah, you have bothGROUP BY a."DID"
to get MAX/MIN and window functions there. There are ways that that's valid but it's usually complicated.
– ypercubeᵀᴹ
53 mins ago
If my query above solved the issue, use it. You cab use a CTE of course but that may result in different performance, as Postgres materializes CTE results (at least in versions less than 12).
– ypercubeᵀᴹ
52 mins ago
thanks, that worked...i tried something similar but using WITH t AS ( slect ..., what you put in th from statement) select t.*, from t then joins where ranks=1/2, it was forcing me to add the columsn to group by or use aggregates, it ended up looking like this, mind telling me my mess up ? thanks pastebin.com/5aN3E29k
– roy naufal
2 hours ago
thanks, that worked...i tried something similar but using WITH t AS ( slect ..., what you put in th from statement) select t.*, from t then joins where ranks=1/2, it was forcing me to add the columsn to group by or use aggregates, it ended up looking like this, mind telling me my mess up ? thanks pastebin.com/5aN3E29k
– roy naufal
2 hours ago
Ah, you have both
GROUP BY a."DID"
to get MAX/MIN and window functions there. There are ways that that's valid but it's usually complicated.– ypercubeᵀᴹ
53 mins ago
Ah, you have both
GROUP BY a."DID"
to get MAX/MIN and window functions there. There are ways that that's valid but it's usually complicated.– ypercubeᵀᴹ
53 mins ago
If my query above solved the issue, use it. You cab use a CTE of course but that may result in different performance, as Postgres materializes CTE results (at least in versions less than 12).
– ypercubeᵀᴹ
52 mins ago
If my query above solved the issue, use it. You cab use a CTE of course but that may result in different performance, as Postgres materializes CTE results (at least in versions less than 12).
– ypercubeᵀᴹ
52 mins ago
add a comment |
You cannot use aliases in WHERE
clauses!
To demonstrate, consider this example (fiddle).
CREATE TABLE foo (bar INT);
Populate it:
INSERT INTO foo VALUES (1), (2), (3);
Then
SELECT bar AS baz
FROM foo
WHERE baz = 2;
Causes the error:
ERROR: column "baz" does not exist
LINE 2: where baz = 2;
^
HINT: Perhaps you meant to reference the column "foo.bar".
You didn't post your error message or PostgreSQL version, but your problem is that timestamp_d1_rank
and timestamp_d2_rank
are aliases.
Maybe the PostgreSQL hint isn't obvious in your case because of the Window function? I'm afraid you're going to have to put the entire expression into your WHERE
clause! Or, you could consider using a CTE
(i.e. WITH
clause) as outlined here.
For a good explanation of this issue, see here.
add a comment |
You cannot use aliases in WHERE
clauses!
To demonstrate, consider this example (fiddle).
CREATE TABLE foo (bar INT);
Populate it:
INSERT INTO foo VALUES (1), (2), (3);
Then
SELECT bar AS baz
FROM foo
WHERE baz = 2;
Causes the error:
ERROR: column "baz" does not exist
LINE 2: where baz = 2;
^
HINT: Perhaps you meant to reference the column "foo.bar".
You didn't post your error message or PostgreSQL version, but your problem is that timestamp_d1_rank
and timestamp_d2_rank
are aliases.
Maybe the PostgreSQL hint isn't obvious in your case because of the Window function? I'm afraid you're going to have to put the entire expression into your WHERE
clause! Or, you could consider using a CTE
(i.e. WITH
clause) as outlined here.
For a good explanation of this issue, see here.
add a comment |
You cannot use aliases in WHERE
clauses!
To demonstrate, consider this example (fiddle).
CREATE TABLE foo (bar INT);
Populate it:
INSERT INTO foo VALUES (1), (2), (3);
Then
SELECT bar AS baz
FROM foo
WHERE baz = 2;
Causes the error:
ERROR: column "baz" does not exist
LINE 2: where baz = 2;
^
HINT: Perhaps you meant to reference the column "foo.bar".
You didn't post your error message or PostgreSQL version, but your problem is that timestamp_d1_rank
and timestamp_d2_rank
are aliases.
Maybe the PostgreSQL hint isn't obvious in your case because of the Window function? I'm afraid you're going to have to put the entire expression into your WHERE
clause! Or, you could consider using a CTE
(i.e. WITH
clause) as outlined here.
For a good explanation of this issue, see here.
You cannot use aliases in WHERE
clauses!
To demonstrate, consider this example (fiddle).
CREATE TABLE foo (bar INT);
Populate it:
INSERT INTO foo VALUES (1), (2), (3);
Then
SELECT bar AS baz
FROM foo
WHERE baz = 2;
Causes the error:
ERROR: column "baz" does not exist
LINE 2: where baz = 2;
^
HINT: Perhaps you meant to reference the column "foo.bar".
You didn't post your error message or PostgreSQL version, but your problem is that timestamp_d1_rank
and timestamp_d2_rank
are aliases.
Maybe the PostgreSQL hint isn't obvious in your case because of the Window function? I'm afraid you're going to have to put the entire expression into your WHERE
clause! Or, you could consider using a CTE
(i.e. WITH
clause) as outlined here.
For a good explanation of this issue, see here.
edited 5 hours ago
answered 9 hours ago
VéraceVérace
17.4k3 gold badges37 silver badges55 bronze badges
17.4k3 gold badges37 silver badges55 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- 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%2fdba.stackexchange.com%2fquestions%2f247275%2frank-used-in-where-returns-invalid-column-but-exists-in-results-set%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
Yes, you can’t refer to them in the where clause as window functions are evaluated after the where clause. You can put your query into a with clause, or wrap it as a subquery.
– Colin 't Hart
4 hours ago