By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

CASE SELECT syntax

P: n/a
What about a SELECT syntax like that?

SELECT
CASE WHEN bar.foo_id IS NULL THEN bar.*
ELSE foo.*
FROM foo, bar

Anyway, I need a SELECT query that gets data from a FIRST TABLE if a
specific value in a SECOND TABLE is NULL or from SECOND TABLE itself if
the value IS NOT NULL.

Any suggestion?

Thank you!
--
Marco Lazzeri [ n o z e S.r.l. ]
Via Giuntini, 25/29 - 56023 Navacchio - Cascina (PI)
Tel +39 (0)50 754380 - Fax +39 (0)50 754381
mailto:ma*******@noze.it - http://www.noze.it
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Thu, Jan 15, 2004 at 18:23:47 +0100,
Marco Lazzeri <ma*******@noze.it> wrote:
What about a SELECT syntax like that?

SELECT
CASE WHEN bar.foo_id IS NULL THEN bar.*
ELSE foo.*
FROM foo, bar

Anyway, I need a SELECT query that gets data from a FIRST TABLE if a
specific value in a SECOND TABLE is NULL or from SECOND TABLE itself if
the value IS NOT NULL.

Any suggestion?


You almost certainly want some join condition between foo and bar.
You can't actually use *. You will need a CASE for each column.
CASE statements need and END.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 22 '05 #2

P: n/a
On Thu, 15 Jan 2004, Bruno Wolff III wrote:
On Thu, Jan 15, 2004 at 18:23:47 +0100,
Marco Lazzeri <ma*******@noze.it> wrote:
What about a SELECT syntax like that?

SELECT
CASE WHEN bar.foo_id IS NULL THEN bar.*
ELSE foo.*
FROM foo, bar

Anyway, I need a SELECT query that gets data from a FIRST TABLE if a
specific value in a SECOND TABLE is NULL or from SECOND TABLE itself if
the value IS NOT NULL.

Any suggestion?


You almost certainly want some join condition between foo and bar.
You can't actually use *. You will need a CASE for each column.
CASE statements need and END.


Alternatively (though it fails to use the 'CASE' syntax) would be to use
COALESCE. Something like:

SELECT COALESCE((SELECT foo_id FROM table2 WHERE ...),
(SELECT bar_id FROM table1 WHERE ...));

HTH
-frank

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.