group by function, make SQL cleaner?

Поиск
Список
Период
Сортировка
Искать

group by function, make SQL cleaner?

От:
Bryce Nesbitt <bryce1@obviously.com>
Дата:
I've got a working query:

stage=# SELECT date_trunc('day',endtime),count(*)
FROM eg_event where endtime >= '2006-02-01' and endtime < '2006-03-01'
GROUP BY  date_trunc('day',endtime)
ORDER BY date_trunc('day',endtime);
    date_trunc      | count
---------------------+-------2006-02-01 00:00:00 |   2532006-02-02 00:00:00 |   2452006-02-03 00:00:00 |   2312006-02-04 00:00:00 |   3132006-02-05 00:00:00 |   2852006-02-06 00:00:00 |   1942006-02-07 00:00:00 |   2292006-02-08 00:00:00 |   2392006-02-09 00:00:00 |   2502006-02-10 00:00:00 |   2452006-02-11 00:00:00 |   275

Is there a way to eliminate the ugly repeated use of
date_trunc('day',endtime)?


Re: group by function, make SQL cleaner?

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Bryce Nesbitt  writes:
> SELECT date_trunc('day',endtime),count(*)
> FROM eg_event where endtime >= '2006-02-01' and endtime < '2006-03-01'
> GROUP BY  date_trunc('day',endtime)
> ORDER BY date_trunc('day',endtime);

> Is there a way to eliminate the ugly repeated use of
> date_trunc('day',endtime)?

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.

This does not work in any context except repeating a SELECT result
expression in GROUP BY or ORDER BY.
		regards, tom lane

Re: group by function, make SQL cleaner?

От:
Bryce Nesbitt <bryce1@obviously.com>
Дата:
 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


It brings up a question though: is there any way in Postgres to set a "quirks" or "standards" mode.  Or get Postgres to log the compliance level of each command and command element used, e,g.:
ORDER BY n
SQL92
GROUP BY n
PSQL
(SELECT ...)
SQL99
SELECT
SQL99
count(...)
SQL99
date_trunc(string,...)
PQSL
It is so easy to get lazy and start shrink-wrapping code to the database.  That's certainly why mysql SQL tends to be so non-portable....


Re: group by function, make SQL cleaner?

От:
pgsql@yukonho.de
Дата:
this should work, 
#
SELECT date_trunc('day',endtime),count(*)FROM eg_event where endtime >= '2006-02-01' and endtime < '2006-03-01'GROUP BY  1 ORDER BY 1;

hope this helps 
best regards,


Stefan









Am Donnerstag, 16. März 2006 06:18 schrieb Bryce Nesbitt:
> I've got a working query:
>
> stage=# SELECT date_trunc('day',endtime),count(*)
> FROM eg_event where endtime >= '2006-02-01' and endtime < '2006-03-01'
> GROUP BY  date_trunc('day',endtime)
> ORDER BY date_trunc('day',endtime);
>
>      date_trunc      | count
> ---------------------+-------
>  2006-02-01 00:00:00 |   253
>  2006-02-02 00:00:00 |   245
>  2006-02-03 00:00:00 |   231
>  2006-02-04 00:00:00 |   313
>  2006-02-05 00:00:00 |   285
>  2006-02-06 00:00:00 |   194
>  2006-02-07 00:00:00 |   229
>  2006-02-08 00:00:00 |   239
>  2006-02-09 00:00:00 |   250
>  2006-02-10 00:00:00 |   245
>  2006-02-11 00:00:00 |   275
>
> Is there a way to eliminate the ugly repeated use of
> date_trunc('day',endtime)?
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
>        subscribe-nomail command to majordomo@postgresql.org so that your
>        message can get through to the mailing list cleanly

-- 
email: stefan@yukonho.de
tel  : +49 (0)6232-497631
http://www.yukonho.de

Re: group by function, make SQL cleaner?

От:
Stefan Becker <stefan@yukonho.de>
Дата:
this should work, 
#
SELECT date_trunc('day',endtime),count(*)FROM eg_event where endtime >= '2006-02-01' and endtime < '2006-03-01'GROUP BY  1 ORDER BY 1;


best regards,


Stefan









Am Donnerstag, 16. März 2006 06:18 schrieb Bryce Nesbitt:
> I've got a working query:
>
> stage=# SELECT date_trunc('day',endtime),count(*)
> FROM eg_event where endtime >= '2006-02-01' and endtime < '2006-03-01'
> GROUP BY  date_trunc('day',endtime)
> ORDER BY date_trunc('day',endtime);
>
>      date_trunc      | count
> ---------------------+-------
>  2006-02-01 00:00:00 |   253
>  2006-02-02 00:00:00 |   245
>  2006-02-03 00:00:00 |   231
>  2006-02-04 00:00:00 |   313
>  2006-02-05 00:00:00 |   285
>  2006-02-06 00:00:00 |   194
>  2006-02-07 00:00:00 |   229
>  2006-02-08 00:00:00 |   239
>  2006-02-09 00:00:00 |   250
>  2006-02-10 00:00:00 |   245
>  2006-02-11 00:00:00 |   275
>
> Is there a way to eliminate the ugly repeated use of
> date_trunc('day',endtime)?
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
>        subscribe-nomail command to majordomo@postgresql.org so that your
>        message can get through to the mailing list cleanly

-- 
email: stefan@yukonho.de
tel  : +49 (0)6232-497631
http://www.yukonho.de

Re: group by function, make SQL cleaner?

От:
Bryce Nesbitt <bryce1@obviously.com>
Дата:
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/


FAQ