Thread: Same column names in a subresult table
Hello!
As I remember, InterBase simply renamed the second XDate to XDate_1 or XDate1.
But as I see the PGSQL keeps the first and second too (which comes with an asterisk "*").
Like select t.* (EXCEPT XDate) from t
Or can I reference them by the order?
I have a big "WITH QUERY", with many subqueries.
I have a date field, named "XDate".
In the middle I duplicated this field:
...
midqry1 as (
select coalesce(XDate , '0001-01-01'), * from prevqry
),
midqry2 as (
select ArtID, max(XDate) as Max_XDate from midqry1
where acq = True
group by ArtID
where acq = True
group by ArtID
)
...
Result: ERROR: column reference "XDate" is ambiguous
As I remember, InterBase simply renamed the second XDate to XDate_1 or XDate1.
But as I see the PGSQL keeps the first and second too (which comes with an asterisk "*").
So in midqry1 I have two XDate columns.
If the two XDates came from different tables, I can use the table prefix.
But now they are in one table.
Is there any way to suppress the original field?
Or say to PGSQL to skip the first XDate field?
Like select t.* (EXCEPT XDate) from t
Or can I reference them by the order?
Ok, I can solve this problem by renaming the new first XDate, but I want to know is there any solution to this problem?
Thank you for any help, info, example!
Best regards
dd
On 14/05/21, Durumdara (durumdara@gmail.com) wrote: > ... > midqry1 as ( > select coalesce(XDate , '0001-01-01'), * from prevqry > ), > midqry2 as ( > select ArtID, max(XDate) as Max_XDate from midqry1 > where acq = True > group by ArtID > ) > ... > > Result: ERROR: column reference "XDate" is ambiguous test=> create table stuff (xdate date, artid integer, acq boolean); test=> insert into stuff values (date'2020-01-01', 1, true) ,(date'2020-01-02', 1, true) ,(date'2020-01-03', 1, false) ,(date'2020-01-01', 2, true) ; test=> \e with prevquery as ( select * from stuff ) ,midqry1 as ( select coalesce (xdate, date'0001-01-01') as xdate, artid, acq from prevquery ) ,midqry2 as ( select artid, max(midqry1.xdate) as max_xdate from midqry1 where acq is true group by artid ) select * from midqry2; artid | max_xdate -------+------------ 2 | 2020-01-01 1 | 2020-01-02 (2 rows) At present your midqry1 has two columns named xdate. Otherwise it is a good idea to qualify the column by the temporary table named by each WITH. Although you've only shown a snippet of your query here it would be pretty simple to aggregate those two WITHs or put them into the main query. This is likely to be a lot faster if you are querying a lot of data. Rory
On Friday, May 14, 2021, Durumdara <durumdara@gmail.com> wrote:
Is there any way to suppress the original field?
Remove the star and list the other columns you do want.
Or say to PGSQL to skip the first XDate field?
Like select t.* (EXCEPT XDate) from t
No, though I’ve expressed a desire for this in the past as well.
Or can I reference them by the order?
No
Ok, I can solve this problem by renaming the new first XDate, but I want to know is there any solution to this problem?
No, you need to rename one of them.
David J.