Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Best way to return a default record if a query returns an ..

 
   Database Forums (Home) -> General Discussion RSS
Next:  SQL Server 2008 Filtered Index Issue - BIT field  
Author Message
jo

External


Since: Jan 07, 2009
Posts: 1



(Msg. 1) Posted: Wed Jan 07, 2009 8:00 am
Post subject: Best way to return a default record if a query returns an empty set?
Archived from groups: comp>databases (more info?)

The subject says it all, here's an example:

Table "aliases" with fields "alias" and "user" (real name). No primary
keys on the table, an alias may refer to multiple users and a user my
have multiple aliases (remember this is just an example, in a real-
world example the "aliases" table would be a subquery).
I want to write a query that will return a series of user names that a
given alias maps to, or the user name itself if there's no alias.

The best I could come up with was this:

(SELECT user FROM aliases WHERE alias = :alias)
UNION
(
SELECT :alias FROM DUAL
WHERE NOT EXISTS (
SELECT * FROM aliases WHERE alias = :alias))

The downside here is that "aliases" appears twice, and in the real
world, it could be a subquery which may be (a) potentially expensive,
(b) lead to an exponential blow-up if this construct is nested, and
(c) could lead to maintenance problems.
So is there a way to rewrite the above to refer to the aliases table
just once?

FWIW I'm on Oracle, and while I know all about WITH, I don't trust it
very far, because it tends to break query plan display (hangs if the
subquery name is used more than once) and the ODBC driver (causes
Oracle to throw up with ORA-00600 (assertion failure in the database
engine) if the query gets a bit complex).
Besides, I'd like to learn techniques that will work with other
databases, too Smile

Regards,
Jo

 >> Stay informed about: Best way to return a default record if a query returns an .. 
Back to top
Login to vote
Jasen Betts

External


Since: Jan 08, 2009
Posts: 2



(Msg. 2) Posted: Wed Jan 07, 2009 8:25 pm
Post subject: Re: Best way to return a default record if a query returns an empty [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2009-01-07, jo.DeleteThis@durchholz.org wrote:
> The subject says it all, here's an example:

naiive answer

> Table "aliases" with fields "alias" and "user" (real name). No primary
> keys on the table, an alias may refer to multiple users and a user my
> have multiple aliases (remember this is just an example, in a real-
> world example the "aliases" table would be a subquery).
> I want to write a query that will return a series of user names that a
> given alias maps to, or the user name itself if there's no alias.
>
> The best I could come up with was this:
>
> (SELECT user FROM aliases WHERE alias = :alias)
> UNION
> (
> SELECT :alias FROM DUAL
> WHERE NOT EXISTS (
> SELECT * FROM aliases WHERE alias = :alias))

that makes no sense at all.

I could guess what you are trying to express
but then I might answer the wrong question.
check line 3.

have you considered using a stored procedure?
I use them all the time in postgres.

 >> Stay informed about: Best way to return a default record if a query returns an .. 
Back to top
Login to vote
Robert Klemme

External


Since: Aug 10, 2006
Posts: 114



(Msg. 3) Posted: Thu Jan 08, 2009 3:25 am
Post subject: Re: Best way to return a default record if a query returns an empty [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 07.01.2009 17:00, jo.TakeThisOut@durchholz.org wrote:
> The subject says it all, here's an example:
>
> Table "aliases" with fields "alias" and "user" (real name). No primary
> keys on the table, an alias may refer to multiple users and a user my
> have multiple aliases (remember this is just an example, in a real-
> world example the "aliases" table would be a subquery).
> I want to write a query that will return a series of user names that a
> given alias maps to, or the user name itself if there's no alias.
>
> The best I could come up with was this:
>
> (SELECT user FROM aliases WHERE alias = :alias)
> UNION
> (
> SELECT :alias FROM DUAL
> WHERE NOT EXISTS (
> SELECT * FROM aliases WHERE alias = :alias))

IMHO this is something typically solved in application land - not in the
database. Do the query, if it returns an empty list, use the user name.

> The downside here is that "aliases" appears twice, and in the real
> world, it could be a subquery which may be (a) potentially expensive,
> (b) lead to an exponential blow-up if this construct is nested, and
> (c) could lead to maintenance problems.
> So is there a way to rewrite the above to refer to the aliases table
> just once?

Note that the fact that it appears twice does not necessarily mean that
a database has to do double work. After all SQL is declarative and the
DB engine is free to get at the data in any way.

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
 >> Stay informed about: Best way to return a default record if a query returns an .. 
Back to top
Login to vote
Walter Mitty

External


Since: Oct 24, 2008
Posts: 4



(Msg. 4) Posted: Thu Jan 08, 2009 9:25 am
Post subject: Re: Best way to return a default record if a query returns an empty set? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

wrote in message

> The subject says it all, here's an example:
>
> Table "aliases" with fields "alias" and "user" (real name). No primary
> keys on the table, an alias may refer to multiple users and a user my
> have multiple aliases (remember this is just an example, in a real-
> world example the "aliases" table would be a subquery).

You may not have declared any primary key. However, it sounds from your
description as though (Alias,User) is a candidate key, and the only
candidate.
You might just as well consider it the primary key. And, yeah, in this case
the primary key is the whole table.


> FWIW I'm on Oracle, and while I know all about WITH, I don't trust it
> very far, because it tends to break query plan display (hangs if the
> subquery name is used more than once) and the ODBC driver (causes
> Oracle to throw up with ORA-00600 (assertion failure in the database
> engine) if the query gets a bit complex).
> Besides, I'd like to learn techniques that will work with other
> databases, too Smile
>

what's wrong with WITH?
 >> Stay informed about: Best way to return a default record if a query returns an .. 
Back to top
Login to vote
Philipp Post

External


Since: Mar 21, 2008
Posts: 19



(Msg. 5) Posted: Thu Jan 08, 2009 10:19 am
Post subject: Re: Best way to return a default record if a query returns an empty [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Jo,

> Table "aliases" with fields "alias" and "user" (real name). No primary keys on the table, an alias may refer to multiple users and a user my have multiple aliases (remember this is just an example, in a real- world example the "aliases" table would be a subquery). I want to write a query that will return a series of user names that a given alias maps to, or the user name itself if there's no alias. <

Not quite sure if this is what you mean:

-- This table is not even in first NF
-- As per the OP, it should model a subquery result
-- Suppose, this is a LEFT OUTER JOIN query which
-- has always the user_full_name and just a user_alias when existing
CREATE TABLE Aliases
(user_full_name VARCHAR(15) NOT NULL,
user_alias VARCHAR(15) NULL);

INSERT INTO Aliases VALUES('Mickey Mouse', 'Mousy');
INSERT INTO Aliases VALUES('Mickey Mouse', 'Mick');
INSERT INTO Aliases VALUES('Mickey Mouse', 'Mega Mouse');
INSERT INTO Aliases VALUES('Donald Duck', NULL);
INSERT INTO Aliases VALUES('Dagobert Duck', 'Mr. Money');
INSERT INTO Aliases VALUES('Mini Mouse', 'Mousy');

SELECT *
FROM (SELECT user_full_name,
user_alias,
CASE WHEN user_alias IS NULL
THEN user_full_name
ELSE user_alias
END AS alias_or_user
FROM Aliases) AS A
WHERE A.alias_or_user = 'Mousy'; -- use a parameter here

I renamed the 'user' column as 'user' is a reserved word - might be
not in Oracle though, but I do not have Oracle installed.

brgds

Philipp Post
 >> Stay informed about: Best way to return a default record if a query returns an .. 
Back to top
Login to vote
Krzysztof Jamróz

External


Since: Jan 10, 2009
Posts: 1



(Msg. 6) Posted: Sat Jan 10, 2009 7:25 am
Post subject: Re: Best way to return a default record if a query returns an empty set? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dnia Wed, 7 Jan 2009 08:00:17 -0800 (PST), jo.TakeThisOut@durchholz.org napisa³(a):

> Table "aliases" with fields "alias" and "user" (real name). No primary
> keys on the table, an alias may refer to multiple users and a user my
> have multiple aliases (remember this is just an example, in a real-
> world example the "aliases" table would be a subquery).
> I want to write a query that will return a series of user names that a
> given alias maps to, or the user name itself if there's no alias.
>
> The best I could come up with was this:
>
> (SELECT user FROM aliases WHERE alias = :alias)
> UNION
> (
> SELECT :alias FROM DUAL
> WHERE NOT EXISTS (
> SELECT * FROM aliases WHERE alias = :alias))

You could try:

SELECT NVL(user,:alias)
FROM dual LEFT JOIN aliases ON(1=1)
WHERE alias = :alias

--
Krzysztof Jamróz
 >> Stay informed about: Best way to return a default record if a query returns an .. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
[SQL] Do GROUP BY return the last record inserted? - I have tables: CREATE TABLE items ( id INT PRIMARY KEY, some_value INT ); CREATE TABLE item_histories ( item_id INT REFERENCES items(id), name VARCHAR(255), created_at DATETIME UNIQUE ); I would like to get a result....

SQL to return data including missing data? - I have an Exam table that consists of three fields, Student, Question and Answer. Two other tables hold all Students and all Questions but the Exam table may omit some Candidates or some Questions for a Candidate. I want to return a record for every..

return number of days between two dates. - I've been trying all morning to figure out a way to display the number of days/months between CURDATE() and a future date (myqsl column). I've tried BETWEEN and something like this: SUBDATE(CURDATE(), MAX(stopdate) AND NOW()) AS time_left I guess tric...

link database record through browser. - Hi all, I have a remote sybase database,I want to display links in web page (browser) such that when user clicks on link ,actual record in that sybase database table should be opend. i.e. I want to connect actual database record to my web page through..

Record Number and key/value database, alternative to Berke.. - Hi, I need a database with two features present in berkeley DB: access by record number and by key/value. The problem is berkeley db got enormous in terms of functionalities, and some simple stuff became to slow. I need hard disk write warranty for..
   Database Forums (Home) -> General Discussion All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]