Received: from narn.deckpoint.ch (narn.deckpoint.ch [194.38.160.133]) by postgresql.org (8.11.3/8.11.4) with ESMTP id fA7G07m11271 for ; Wed, 7 Nov 2001 11:00:12 -0500 (EST) (envelope-from dbucher@niftycom.com) Received: from orange.niftycom.com (hidden-user@[213.162.13.76]) by narn.deckpoint.ch (8.11.6/8.11.6) with ESMTP id fA7G05W09561 for ; Wed, 7 Nov 2001 17:00:06 +0100 Received: from violet.niftycom.com ([192.168.200.4]) by orange.niftycom.com (8.10.2/8.10.2/SuSE Linux 8.10.0-0.3) with ESMTP id fA7G02I18324 for ; Wed, 7 Nov 2001 17:00:06 +0100 X-Authentication-Warning: orange.niftycom.com: Host [192.168.200.4] claimed to be violet.niftycom.com Message-Id: <5.1.0.14.0.20011107164717.03153940@mail.niftycom.com> X-Sender: dbucher@mail.niftycom.com X-Mailer: QUALCOMM Windows Eudora Version 5.1 Date: Wed, 07 Nov 2001 16:58:31 +0100 To: PGSQL-SQL From: Denis Bucher Subject: Need help with embedded CASEs Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Archive-Number: 200111/104 X-Sequence-Number: 5488 Hello ! I came across a very hard SELECT and Postgres refuses it. If someone could help me it would be great ! Here is a simplified version of the problem that I have : SELECT CASE WHEN '2001-11-07' = current_date THEN 't' ELSE 'f' END AS flag_today, CASE WHEN flag_today THEN current_time ELSE '00:00' END AS time_iftoday; Why doesn't it work ? Or how could I use the result of the CASE in another ? Thanks a lot for any help ! Denis Bucher NiftyCom P.S. The real request, for fun, is : SELECT sc.datetime_deliver, SUM( CASE WHEN sc.type_cmd=1 AND b.b='TRUE' THEN sl.number_main WHEN sc.type_cmd=4 AND b.b='TRUE' AND true_nb_shop=sc.nb_shop_main THEN sl.number_main ELSE '0' END ) AS sum_nb_entre, CASE WHEN sc.type_cmd=1 AND b.b='TRUE' THEN sc.nb_shop_main WHEN sc.type_cmd=3 AND b.b='TRUE' THEN sc.nb_shop_other WHEN sc.type_cmd=4 AND b.b='TRUE' THEN sc.nb_shop_main WHEN sc.type_cmd=4 AND b.b='FALSE' THEN sc.nb_shop_other ELSE '0' END AS true_nb_shop, SUM( CASE WHEN sc.type_cmd=3 AND b.b='TRUE' THEN sl.number_other+sl.number_trash+sl.number_eaten WHEN sc.type_cmd=4 AND b.b='FALSE' THEN sl.number_other ELSE '0' END ) AS sum_nb_sorti FROM stock_cmd sc, stock_lig sl, (SELECT boolean 'TRUE' AS b UNION SELECT 'FALSE') as b WHERE sc.datetime_deliver BETWEEN '2001-01-01 11:00:00' AND '2002-01-01 12:00:00' AND sc.type_cmd<>2 AND sl.id_cmd=sc.id AND (sc.type_cmd=4 OR b.b='TRUE') GROUP BY sc.datetime_deliver, true_nb_shop