pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14079: Issues with query
5+ messages / 5 participants
[nested] [flat]

* BUG #14079: Issues with query
@ 2016-04-08 21:47 hector.bejarano@gmail.com
  2016-04-08 22:53 ` Re: BUG #14079: Issues with query Phillip Couto <phillip.couto01@gmail.com>
  2016-04-08 22:55 ` Re: BUG #14079: Issues with query John R Pierce <pierce@hogranch.com>
  0 siblings, 2 replies; 5+ messages in thread

From: hector.bejarano@gmail.com @ 2016-04-08 21:47 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14079
Logged by:          Hector Bejarano
Email address:      hector.bejarano@gmail.com
PostgreSQL version: 9.5.0
Operating system:   Ubuntu 14
Description:        

This query works:
select 1 as a

But this one fails:
select 1 as a where a = 1

And I think they should both work.

Regards,
Hector.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #14079: Issues with query
  2016-04-08 21:47 BUG #14079: Issues with query hector.bejarano@gmail.com
@ 2016-04-08 22:53 ` Phillip Couto <phillip.couto01@gmail.com>
  2016-04-09 13:37   ` Re: BUG #14079: Issues with query Hector Bejarano <hector.bejarano@gmail.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Phillip Couto @ 2016-04-08 22:53 UTC (permalink / raw)
  To: hector.bejarano@gmail.com; pgsql-bugs

The WHERE is executed before the SELECT. The value of a is not available to
the WHERE clause as it is assigned at the end of the query.

To actually make your query work it would have to read:
select * from (select 1 as a) as b where a = 1

The from provides the data that the where will operate on, the select then
determines what to be returned.

On Fri, 8 Apr 2016 at 18:41 <hector.bejarano@gmail.com> wrote:

> The following bug has been logged on the website:
>
> Bug reference:      14079
> Logged by:          Hector Bejarano
> Email address:      hector.bejarano@gmail.com
> PostgreSQL version: 9.5.0
> Operating system:   Ubuntu 14
> Description:
>
> This query works:
> select 1 as a
>
> But this one fails:
> select 1 as a where a = 1
>
> And I think they should both work.
>
> Regards,
> Hector.
>
>
> --
> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-bugs
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #14079: Issues with query
  2016-04-08 21:47 BUG #14079: Issues with query hector.bejarano@gmail.com
  2016-04-08 22:53 ` Re: BUG #14079: Issues with query Phillip Couto <phillip.couto01@gmail.com>
@ 2016-04-09 13:37   ` Hector Bejarano <hector.bejarano@gmail.com>
  2016-04-10 16:13     ` Re: BUG #14079: Issues with query David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Hector Bejarano @ 2016-04-09 13:37 UTC (permalink / raw)
  To: Phillip Couto <phillip.couto01@gmail.com>; +Cc: pgsql-bugs

Right, this explains the how but not the why, which is really my question.
For instance, there are other statements I can use with this derived column
like a group by:

select 1 as a group by a

But then if I try to use "having" then it fails just like it does with the
WHERE clause:

select 1 as a group by a having a=1

See, the thing here is I don't understand WHY it was designed this way,
because to me it would be very useful to have the ability to use the
derived column without having to wrap the whole thing in a derived query.

On Fri, Apr 8, 2016 at 4:53 PM, Phillip Couto <phillip.couto01@gmail.com>
wrote:

> The WHERE is executed before the SELECT. The value of a is not available
> to the WHERE clause as it is assigned at the end of the query.
>
> To actually make your query work it would have to read:
> select * from (select 1 as a) as b where a = 1
>
> The from provides the data that the where will operate on, the select then
> determines what to be returned.
>
> On Fri, 8 Apr 2016 at 18:41 <hector.bejarano@gmail.com> wrote:
>
>> The following bug has been logged on the website:
>>
>> Bug reference:      14079
>> Logged by:          Hector Bejarano
>> Email address:      hector.bejarano@gmail.com
>> PostgreSQL version: 9.5.0
>> Operating system:   Ubuntu 14
>> Description:
>>
>> This query works:
>> select 1 as a
>>
>> But this one fails:
>> select 1 as a where a = 1
>>
>> And I think they should both work.
>>
>> Regards,
>> Hector.
>>
>>
>> --
>> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-bugs
>>
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #14079: Issues with query
  2016-04-08 21:47 BUG #14079: Issues with query hector.bejarano@gmail.com
  2016-04-08 22:53 ` Re: BUG #14079: Issues with query Phillip Couto <phillip.couto01@gmail.com>
  2016-04-09 13:37   ` Re: BUG #14079: Issues with query Hector Bejarano <hector.bejarano@gmail.com>
@ 2016-04-10 16:13     ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: David G. Johnston @ 2016-04-10 16:13 UTC (permalink / raw)
  To: Hector Bejarano <hector.bejarano@gmail.com>; +Cc: Phillip Couto <phillip.couto01@gmail.com>; pgsql-bugs

On Sat, Apr 9, 2016 at 6:37 AM, Hector Bejarano <hector.bejarano@gmail.com>
wrote:

> Right, this explains the how but not the why, which is really my question.
> For instance, there are other statements I can use with this derived column
> like a group by:
>
> select 1 as a group by a
>
> But then if I try to use "having" then it fails just like it does with the
> WHERE clause:
>
> select 1 as a group by a having a=1
>
> See, the thing here is I don't understand WHY it was designed this way,
> because to me it would be very useful to have the ability to use the
> derived column without having to wrap the whole thing in a derived query.
>
>
​Tom answered the why a long time ago:

Google: postgresql having not using alias
http://www.postgresql.org/message-id/7608.1259177709@sss.pgh.pa.us

Basically, it shouldn't work for GROUP BY but it does so we live with it.

The difference is that in GROUP BY all you are referring to is an alias
(grouping_element) while in HAVING you are using it in an expression
(condition)

http://www.postgresql.org/docs/current/static/sql-select.html#SQL-HAVING

Its not any different than the fact you can say:  "GROUP BY 1, 2" but
saying "1 = 'alpha'" in the HAVING clause would downright confusing.

David J.


> On Fri, Apr 8, 2016 at 4:53 PM, Phillip Couto <phillip.couto01@gmail.com>
> wrote:
>
>> The WHERE is executed before the SELECT. The value of a is not available
>> to the WHERE clause as it is assigned at the end of the query.
>>
>> To actually make your query work it would have to read:
>> select * from (select 1 as a) as b where a = 1
>>
>> The from provides the data that the where will operate on, the select
>> then determines what to be returned.
>>
>> On Fri, 8 Apr 2016 at 18:41 <hector.bejarano@gmail.com> wrote:
>>
>>> The following bug has been logged on the website:
>>>
>>> Bug reference:      14079
>>> Logged by:          Hector Bejarano
>>> Email address:      hector.bejarano@gmail.com
>>> PostgreSQL version: 9.5.0
>>> Operating system:   Ubuntu 14
>>> Description:
>>>
>>> This query works:
>>> select 1 as a
>>>
>>> But this one fails:
>>> select 1 as a where a = 1
>>>
>>> And I think they should both work.
>>>
>>> Regards,
>>> Hector.
>>>
>>>
>>> --
>>> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
>>> To make changes to your subscription:
>>> http://www.postgresql.org/mailpref/pgsql-bugs
>>>
>>
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #14079: Issues with query
  2016-04-08 21:47 BUG #14079: Issues with query hector.bejarano@gmail.com
@ 2016-04-08 22:55 ` John R Pierce <pierce@hogranch.com>
  1 sibling, 0 replies; 5+ messages in thread

From: John R Pierce @ 2016-04-08 22:55 UTC (permalink / raw)
  To: hector.bejarano@gmail.com; pgsql-bugs

On 4/8/2016 2:47 PM, hector.bejarano@gmail.com wrote:
> This query works:
> select 1 as a
>
> But this one fails:
> select 1 as a where a = 1
>
> And I think they should both work.


nope.      doesn't work that way.   Aliases to selected fields can not 
be used in the WHERE clause, because the WHERE clause is executed BEFORE 
the fields are selected and calculated.

this is per the SQL standard.


-- 
john r pierce, recycling bits in santa cruz



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2016-04-10 16:13 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-04-08 21:47 BUG #14079: Issues with query hector.bejarano@gmail.com
2016-04-08 22:53 ` Phillip Couto <phillip.couto01@gmail.com>
2016-04-09 13:37   ` Hector Bejarano <hector.bejarano@gmail.com>
2016-04-10 16:13     ` David G. Johnston <david.g.johnston@gmail.com>
2016-04-08 22:55 ` John R Pierce <pierce@hogranch.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox