Re: group by function, make SQL cleaner? - Mailing list pgsql-sql

From Bryce Nesbitt
Subject Re: group by function, make SQL cleaner?
Date
Msg-id 44319A9C.9040800@obviously.com
Whole thread Raw
In response to Re: group by function, make SQL cleaner?  (Bryce Nesbitt <bryce1@obviously.com>)
List pgsql-sql
Bryce Nesbitt wrote:
> Tom Lane wrote:
>> In this particular case you could say
>>
>>     ... GROUP BY 1 ORDER BY 1;
>>
>> "ORDER BY n" as a reference to the n'th SELECT output column is in the
>> SQL92 spec.  (IIRC they removed it in SQL99, but we still support it,
>> and I think most other DBMSes do too.)  "GROUP BY n" is *not* in any
>> version of the spec but we allow it anyway.  I'm not sure how common
>> that notation is.
>>
>>   
> Thanks.  Markus Bertheau also supplied this solution:
> SELECT enddate, count(*) FROM (
>     SELECT date_trunc('day', endtime) AS enddate FROM eg_event WHERE
> endtime >= '2006-01-01'  and endtime < '2006-03-01') as foo
> GROUP BY enddate
> ORDER BY enddate
>   

Hmm.  Is there a way to specify the "n" column in a WHERE?


demo=> select p_last_name,count(*) from xx_person group by p_last_name
where 2  > 28;
ERROR:  syntax error at or near "where" at character 65
LINE 1: ...name,count(*) from eg_person group by p_last_name where '3' ...


demo=> select p_last_name,count(*) from xx_person group by p_last_name
order by 2 desc limit 6;
p_last_name | count
-------------+-------
Smith | 44
Miller | 37
Lee | 35
Williams | 33
Johnson | 30
Jones | 28
(6 rows)


-- 
----
Visit http://www.obviously.com/



pgsql-sql by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: References NULL field
Next
From: "Eugene E."
Date:
Subject: Re: have you feel anything when you read this ?