Solution found ! Re: Need help with embedded CASEs

From: Denis Bucher <dbucher(at)niftycom(dot)com>
To: PGSQL-SQL <pgsql-sql(at)postgresql(dot)org>
Subject: Solution found ! Re: Need help with embedded CASEs
Date: 2001-11-08 11:06:14
Message-ID: 5.1.0.14.0.20011108120107.03333ec0@mail.niftycom.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

At 16:58 07.11.01 +0100, you wrote:

Hello !

I think it is a bug or at least an unimplemented feature of Postgres 7.1...

>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;

Conclusion it doesn't work to use a newly created column in a CASE

And the solution are :

a) replace the field by the complete expression (can become very complex)
b) do sub-request :

The erroneous sql query was :
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

I change it into :
SELECT
vi.datetime_deliver, vi.true_nb_shop,
SUM( CASE WHEN vi.type_cmd=1 AND vi.b='TRUE' THEN vi.number_main WHEN
vi.type_cmd=4 AND vi.b='TRUE' AND vi.true_nb_shop=vi.nb_shop_main THEN
vi.number_main ELSE '0' END ) AS sum_nb_entre,
SUM( CASE WHEN vi.type_cmd=3 AND vi.b='TRUE' THEN
vi.number_other+vi.number_trash+vi.number_eaten WHEN vi.type_cmd=4 AND
vi.b='FALSE' THEN vi.number_other ELSE '0' END ) AS sum_nb_sorti
FROM
(SELECT sc.type_cmd, b.b, sl.number_main, sl.number_other, sl.number_trash,
sl.number_eaten, sc.nb_shop_main, sc.datetime_deliver, 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 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'))
AS vi
GROUP BY vi.datetime_deliver, vi.true_nb_shop

And it's great ! It seems to work ;-))

Thanks to all that helped me and gave me the solution !

Denis Bucher
NiftyCom

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2001-11-08 14:30:05 Re: Can this query be optimized?
Previous Message Bruce Momjian 2001-11-08 02:36:00 Re: Can't find docs on Postgresql.conf