X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id F2A23D1B220 for ; Mon, 17 May 2004 06:25:25 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 06837-07 for ; Mon, 17 May 2004 06:25:08 -0300 (ADT) Received: from mail.cobweb.nl (mx1.cobweb.nl [80.242.96.4]) by svr1.postgresql.org (Postfix) with ESMTP id F055CD1B22F for ; Mon, 17 May 2004 06:25:03 -0300 (ADT) Received: from farcourier.com (dp3-26.dsl.cobweb.nl [193.67.120.154] (may be forged)) by mail.cobweb.nl (8.12.8/8.12.7) with ESMTP id i4H9PBu1025162 for ; Mon, 17 May 2004 11:25:11 +0200 (CEST) (envelope-from s.vanroye@farcourier.com) Subject: Re: a wierd query Date: Mon, 17 May 2004 11:24:33 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Message-ID: <71E201BE5E881C46811BA160694C5FCB046739@fs1000.farcourier.com> X-MS-Has-Attach: X-MIMEOLE: Produced By Microsoft Exchange V6.0.6249.0 Content-Class: urn:content-classes:message X-MS-TNEF-Correlator: Thread-Topic: [SQL] a wierd query Thread-Index: AcQ75oiFl7CJCSniTVe6sT6avH49vwAB4qug From: "Stijn Vanroye" To: Cc: "sad" X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200405/144 X-Sequence-Number: 17858 First of all, I don't select distinct on 1 value, but on 2. Meaning I want = each unique combination of task_id (or employee_id in this example) and dat= e. That way both fields still have meaning. the workhour_id field is indeed redundant, but was still there from some pe= vious testing work. (It is now removed). both other fields are used. What I'm trying to achieve here is the following: for each task get all dat= e's in wich that task has been performed (and each date only once per task)= . Since workhours have a begin date and time, as well as an end date and ti= me. Begin- and enddate don't have to be the same day. But if a task is perf= ormed starting monday and lasting till tueseday, both days have to be inclu= ded in the count. What I use now is this: select distinct on (date, employee_id) employee_id, date from ( select distinct on (begindate, employee_id) begindate as date, employ= ee_id from workhour UNION select distinct on (enddate, employee_id) enddate as date, employee_i= d from workhour )as dist The workhour table looks something like this: workhour(workhour_id, task_id, employee_id, begindate, begintime, enddate, = endtime) I Can't think of any other solution to achieve this. As far as I can tell, = Im not missing something and I don't have meaningless fields (suggestions a= lways welcome). Later on some grouping will be done to count the number of = days worked on a certain task (or by a certain employee) in a given period. This still keeps my question open wether or not a UNION does only show uniq= ue values in the union. > -----Original Message----- > From: sad [mailto:sad@bankir.ru] > Sent: maandag 17 mei 2004 9:13 > To: pgsql-sql@postgresql.org > Subject: Re: [SQL] a wierd query >=20 >=20 > > I forgot one situation: > > > > if I run the query like so: > > select distinct on (task_id, begindate) task_id, workhour_id, > > begindate as date from workhour UNION > > select distinct on (task_id, enddate) task_id,=20 > workhour_id, enddate > > as date from workhour I get yet another value: 2961 rows. > > > > So I got 3 different result sets for 3 different ways to=20 > run the query. > > Even in this last case the UNION doesn't seem to only=20 > return unique values, > > and I will still need the top-level select. >=20 > if we suppose this situation possible to program in SQL > it causes data-loss in query > (i mean unpredictable query result: > if you have two records (f=3D1,b=3D2),(f=3D1,b=3D3) > "distinct ON f" makes a value of b meaningless) >=20 > So you MUST NOT select that way >=20 > P.S. This situation means: you have wrong data structure.=20 >=20 >=20 > ---------------------------(end of=20 > broadcast)--------------------------- > TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to=20 > majordomo@postgresql.org) >=20