tags:

views:

76

answers:

4

I have this simple query

select * from users where name = 'User1'

I'd like to extend the query's functionality whatever the query returns 0 records the query will fetch the data by other clause.

where name = 'Default'

If the first clause will fetch some records the second clause will be ignored.

EDIT

Oracle

+4  A: 
 IF EXISTS (SELECT * from users where name = 'User1')
   SELECT * from users where name = 'User1';
 ELSE
   SELECT * from users where name = 'Default';
Michael Pakhantsov
The only answer so far that gives the correct results in all situations.
Yves M.
Except that is does not work in Oracle
Quassnoi
@Quassnoi, rdbms was not specified first time. :), @mdma provided correct answer for oracle.
Michael Pakhantsov
@Michael: I didn't downvote :)
Quassnoi
+7  A: 
SELECT * FROM users WHERE name = 'User1'
UNION ALL 
SELECT * FROM users WHERE name = 'Default' 
    AND NOT EXISTS (SELECT 1 FROM users WHERE name='User1')
mdma
Voting your post instead of correcting mine
Quassnoi
@Quassnoi: Wasn't yours slightly better in terms of performance, having one less `SELECT`? ... EDIT: But I guess that was assuming `name` was unique.
Daniel Vassallo
I don't know oracle - I posted this before the OP specified that. It's verbose, but works on SQL Server. How about Oracle?
mdma
@Daniel: mine works wrong, it can return at most one record.
Quassnoi
@mdma: your query will work in `Oracle` too (and yield correct results).
Quassnoi
@Quassnoi - Thanks for the confirmation.
mdma
+1  A: 
select top 1 * from users where name in ('User1', 'Default') order by name desc

:P

tenfour
I think Oracle uses something different than `TOP`. This will work though if there is a unique constraint on `name`.
Martin Smith
@Martin: I think the OP didn't specify Oracle originally.
Daniel Vassallo
@Daniel - Yes I didn't downvote.
Martin Smith
@OP: if you do use this, be very careful understanding that this query depends on the alphabetic order of `User1` and `Default`.
tenfour
The author's query can return more than 1 record while this one can return at most 1. In `SQL Server`, this could be worked around using `TOP 1 WITH TIES`, but this is `Oracle`.
Quassnoi
@Quassnoi - You can use `RANK` to deal with ties.
Martin Smith
And what does that return if the username queried for is "Anna" instead of "User1"... Then you get the wrong result, so I'm wondering why people are voting this answer up when it's clearly wrong.
wimvds
and what if it's more than 2 names?To be honest I posted this as a sort of joke, hence the :P. If the OP uses this type of query, as I said he must be careful to understand why it works and make sure that it doesn't dig him into a hole. I don't like just writing other peoples' code for them; i am just showing the strategy.
tenfour
+1  A: 
WITH T AS   (
        SELECT  users.*,
        RANK() OVER (ORDER BY CASE WHEN name = 'User1' THEN 0 ELSE 1 END) AS RN
        FROM    users
        WHERE   name IN ( 'Default','User1')
        )
SELECT * FROM T
WHERE   RN = 1
Martin Smith