473,394 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

CASE SELECT syntax

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
2 13568
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave | last post by:
I am getting a syntax error on Select Case Now Case > "2/2/2004 12:30:00 AM" response.redirect "votingover.asp" case else m=0 End Select
9
by: Kevin | last post by:
Hi, I am getting a syntax error Microsoft VBScript compilation error '800a03ea' Syntax error On the code below. The error references the "End Select" line Can anyone help me with what I am...
8
by: Penny | last post by:
Hi all, My browser throws this Select Case block back at me pointing out a syntax error on the line: 'Case < 251', between the word 'Case' and the '<' symbol. *************************** ...
25
by: CJM | last post by:
I'm getting a syntax error with a Select Case statement: Select Case CSng(rs.fields("Field1")) Case 0 Response.Write "Test1" Case Is < 0 <<< Syntax Error...
2
by: scole954387 | last post by:
Hi, I have a problem. I have written a SQL statement that has a nested select case statement on the 'where' clause to condition the results. ...
6
by: Spam Catcher | last post by:
Does anyone know why this doesn't work: Select Case X Case X or Y msgbox("hello world!") End Select It seems X or Y doesn't evaluate correctly (I think only the first 1/2 is
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
4
by: makinha | last post by:
Hello, I am getting a syntax error Microsoft VBScript compilation error '800a0400' Expected statement line 49 Case 164 To 269 ---------------^ (The error references...
2
by: arunmenon | last post by:
Dear Folks, I have an query which something looks like this select ILTRDJ AS YEAR, DATEPART(MONTH,ILTRDJ) AS MONTH, FMSTR.IMITM AS CODE,FMSTR.IMSRP6_UDC AS DESCRIPTION, FTRANS.ILDCT = CASE...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.