Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id AB6DF64FCBB for ; Fri, 15 Aug 2008 00:56:56 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 55270-04 for ; Fri, 15 Aug 2008 00:56:33 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.182]) by postgresql.org (Postfix) with ESMTP id C665B64FCBA for ; Fri, 15 Aug 2008 00:56:14 -0300 (ADT) Received: by wa-out-1112.google.com with SMTP id n4so399101wag.5 for ; Thu, 14 Aug 2008 20:56:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:sender :to:subject:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references :x-google-sender-auth; bh=TiH0cT16s/14cL4Yyv1MWDBhSYR4WGDo6b1gJ7C4S4Q=; b=PA/87fBNceiFlXtZl9hEKAMQHedUSb/uHCoRBvp9caaQPyXwDh4k822P4sWWtzOETg qdLQIN8FAssqHGSusBL29NO59Lm/GDpLOs+ctpLGe/yI3RAKiXIczNC3m7uesADKg+jb uOtwWLXqqURNatisJkv2AYv9bDub6RsWMVYGY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:sender:to:subject:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references:x-google-sender-auth; b=OlZwbVuwnvdO1kcSMzGkHnXF5Vo31ncIsVlTf68puyz32ntawXQqAccWCpdw5YdfOw S7yS0N8mfsDgWVhXEp6WjcOJpeNkpSG04rTvTImXUxcaRj1sCyrG1Xm3nWpoedyRrwHg Be7JoQY/8HUT3AvFp6fakyWKjOBDh7agt6Lq8= Received: by 10.114.56.4 with SMTP id e4mr2119861waa.0.1218771973500; Thu, 14 Aug 2008 20:46:13 -0700 (PDT) Received: by 10.114.81.15 with HTTP; Thu, 14 Aug 2008 20:46:13 -0700 (PDT) Message-ID: Date: Fri, 15 Aug 2008 13:46:13 +1000 From: novice To: pgsql-general@postgresql.org Subject: Re: Query help In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: X-Google-Sender-Auth: 10566207b71f8b78 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200808/515 X-Sequence-Number: 136345 2008/8/15 novice : > Hi, > I have a table > > select id, config_id, start_day, end_day, start_time, end_time from config; > > id | config_id | start_day | end_day | start_time | end_time > -----+-----------+-----------+---------+------------+---------- > 1 | 101 | Mon | Sun | 08:30:00 | 18:00:00 > 2 | 101 | Mon | Sun | 18:00:00 | 22:00:00 > 3 | 555 | Mon | Fri | 08:30:00 | 16:00:00 > > > > I'd like to write a query to generate the following... is it possible at all? > > config_id | day | start_time | end_time > -----------+-----------+---------+------------- > 101 | Mon | 08:30:00 | 18:00:00 > 101 | Mon | 18:00:00 | 22:00:00 > 101 | Tue | 08:30:00 | 18:00:00 > 101 | Tue | 18:00:00 | 22:00:00 > 101 | Wed | 08:30:00 | 18:00:00 > 101 | Wed | 18:00:00 | 22:00:00 > 101 | Thu | 08:30:00 | 18:00:00 > 101 | Thu | 18:00:00 | 22:00:00 > 101 | Fri | 08:30:00 | 18:00:00 > 101 | Fri | 18:00:00 | 22:00:00 > 101 | Sat | 08:30:00 | 18:00:00 > 101 | Sat | 18:00:00 | 22:00:00 > 101 | Sun | 08:30:00 | 18:00:00 > 101 | Sun | 18:00:00 | 22:00:00 > 555 | Mon | 08:30:00 | 18:00:00 > 555 | Tue | 08:30:00 | 18:00:00 > 555 | Wed | 08:30:00 | 18:00:00 > 555 | Thu | 08:30:00 | 18:00:00 > 555 | Fri | 08:30:00 | 18:00:00 > > Thanks > Solved: create TABLE weekday ( wd varchar(3), seq int ) INSERT INTO weekday (wd, seq) VALUES ('Mon', '1'), ('Tue', '2'), ('Wed', '3'), ('Thu', '4'), ('Fri', '5'), ('Sat', '6'), ('Sun', '7'); SELECT config.config_id, w.wd, config.start_time, config.end_time FROM config INNER JOIN weekday s ON config.start_day = s.wd INNER JOIN weekday e ON config.end_day = e.wd CROSS JOIN weekday w WHERE w.seq >= s.seq AND w.seq <= e.seq ORDER BY config.config_id, w.seq, w.wd