Thread: [GENERAL] Help with query (fwd)
---------- Forwarded message ---------- Date: Fri, 5 Nov 1999 09:35:25 -0400 (VET) From: Vegeta <vegeta@cuaima.ica.luz.ve> To: pgsql-general@postgresql.org Subject: Help with query Dear list members: I am new to the list and to SQL. I am using PostgreSQL 6.4.2 on Linux and want to do the following query: Given a table "works" with fields "idworker" (int4) and "timeofwork" (datetime), i want to have a query who has the number of hours worked by each "worker" in the form: idworker hoursworked I tried with: SELECT idworker, COUNT(DISTINCT int8(timeofwork)/3600) as hoursworked FROM works GROUP BY idworker but pgsql does not accept the DISTINCT inside the count (I read in a book that there are some RDBMS that do accept it). Is there another way to acomplish this? Do I need to create a user-defined aggregate? If so, how should I do it? Thanks in advance, Guido Urdaneta PS: Sorry for my bad english
On 1999-11-05, Vegeta mentioned: > Given a table "works" with fields "idworker" (int4) and "timeofwork" > (datetime), i want to have a query who has the number of hours worked by > each "worker" in the form: > idworker hoursworked > > I tried with: > SELECT idworker, COUNT(DISTINCT int8(timeofwork)/3600) as hoursworked > FROM works > GROUP BY idworker That query doesn't make much sense to me. I think what you meant was something like this: SELECT idworker, SUM(int8(timeofwork)/3600) FROM works GROUP BY idworker I'm not sure about the appropriateness of your attempted typecast there, though. The timeofwork column should most likely be a timespan field. You can then fill it with values like '5 hours 45 minutes'. Then your query could be: SELECT idworker, SUM(date_part('epoch', timeofwork) * 3600) FROM works GROUP BY idworker -- Peter Eisentraut Sernanders vaeg 10:115 peter_e@gmx.net 75262 Uppsala http://yi.org/peter-e/ Sweden