dbTalk Databases Forums  

Return valuse based on multiple "if exists' statements

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Return valuse based on multiple "if exists' statements in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SQL Learner
 
Posts: n/a

Default Return valuse based on multiple "if exists' statements - 02-06-2010 , 02:43 PM






Hi All,

Hope someone can help to solve this SQL question.

I have the following table called '#temptable' (it is a temp table)

source price state
A 1 NY
B 2 NY
C 3 NY
B 4 CA
C 5 CA
C 6 TX

I need a SQL statement that performs the following pesudo code:

if source A exists then (select * from #temptable where source = 'A')
if source A not exists then check if source B exist.
if source B exists then (select * from #temptable where source = 'B')
if source B not exists then check if source C exist.
if source C exists then (select * from #temptable where source = 'C')
if source C not exists then print 'Nothing is found.'

The following code can be used to create the temp table for testing:

create table #TempTable (source char(1), price int, state char (2))
insert into #TempTable values ('A', 1, 'NY')
insert into #TempTable values ('B', 2, 'NY')
insert into #TempTable values ('C', 3, 'NY')
insert into #TempTable values ('B', 4, 'CA')
insert into #TempTable values ('C', 5, 'CA')
insert into #TempTable values ('C',6, 'TX')

Thanks.

SQL Learner

Reply With Quote
  #2  
Old   
SQL Learner
 
Posts: n/a

Default Re: Return valuse based on multiple "if exists' statements - 02-06-2010 , 03:23 PM






I did some experiment with the code and found the solution:

================================================
if exists
( select * from #TempTable where source = 'A')
select * from #TempTable where source = 'A'
else if exists
( select * from #TempTable where source = 'B')
select * from #TempTable where source = 'B'
else if exists
( select * from #TempTable where source = 'C')
select * from #TempTable where source = 'C'
else
print 'Nothing is found.'
================================================

However, I know there are different approaches to this problem. If
anyone know of different ways (probably more advanced), please share.
Thanks.

SQL Learner

Reply With Quote
  #3  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Return valuse based on multiple "if exists' statements - 02-06-2010 , 05:42 PM



On Sat, 6 Feb 2010 11:43:38 -0800 (PST), SQL Learner wrote:

Quote:
Hi All,

Hope someone can help to solve this SQL question.

I have the following table called '#temptable' (it is a temp table)

source price state
A 1 NY
B 2 NY
C 3 NY
B 4 CA
C 5 CA
C 6 TX

I need a SQL statement that performs the following pesudo code:

if source A exists then (select * from #temptable where source = 'A')
if source A not exists then check if source B exist.
if source B exists then (select * from #temptable where source = 'B')
if source B not exists then check if source C exist.
if source C exists then (select * from #temptable where source = 'C')
if source C not exists then print 'Nothing is found.'

The following code can be used to create the temp table for testing:

create table #TempTable (source char(1), price int, state char (2))
insert into #TempTable values ('A', 1, 'NY')
insert into #TempTable values ('B', 2, 'NY')
insert into #TempTable values ('C', 3, 'NY')
insert into #TempTable values ('B', 4, 'CA')
insert into #TempTable values ('C', 5, 'CA')
insert into #TempTable values ('C',6, 'TX')

Thanks.

SQL Learner

Hi SQL Learner,

How about:

SELECT source, price, state
FROM #TempTable
WHERE source = (SELECT MIN(source) FROM #TempTable);

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Reply With Quote
  #4  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Return valuse based on multiple "if exists' statements - 02-06-2010 , 05:43 PM



This should do it:

SELECT TOP 1 WITH TIES source, price, state
FROM #TempTable
WHERE source IN ('A', 'B', 'C')
ORDER BY source;

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #5  
Old   
SQL Learner
 
Posts: n/a

Default Re: Return valuse based on multiple "if exists' statements - 02-06-2010 , 06:38 PM



Hi Hugo and Plamen,

Thank you guys for your solution.

What if I change the data to this:

Tiger 1 NY
Ape 2 NY
Elephant 3 NY
Ape 4 CA
Elephant 5 CA
Elephant 6 TX

Just substitute Tiger for A, Ape for B, and Elephant for C. This way,
the Top 1 and Min methods will not work, right?
How can we modify the code to accommendate for this change?

Thank you in advanced.

SQL Learner

Reply With Quote
  #6  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Return valuse based on multiple "if exists' statements - 02-06-2010 , 06:51 PM



SELECT TOP 1 WITH TIES source, price, state
FROM #TempTable
WHERE source IN ('Tiger', 'Ape', 'Elephant')
ORDER BY CASE source
WHEN 'Tiger' THEN 1
WHEN 'Ape' THEN 2
WHEN 'Elephant' THEN 3
END;

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #7  
Old   
SQL Learner
 
Posts: n/a

Default Re: Return valuse based on multiple "if exists' statements - 02-06-2010 , 09:09 PM



Plamen,

This is cool. Thanks.

Could you or anyone explain how the following code work?

ORDER BY CASE source
WHEN 'Tiger' THEN 1
WHEN 'Ape' THEN 2
WHEN 'Elephant' THEN 3
END;

Thanks again.

Anthony

Reply With Quote
  #8  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Return valuse based on multiple "if exists' statements - 02-06-2010 , 10:23 PM



The CASE expression does mapping of the sources to sort values, in a way giving priority to 1, then 2, 3, etc. That way
the source with sort value 1 (Tiger) is on top of the list if any entries exist. If not then the next one.

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #9  
Old   
SQL Learner
 
Posts: n/a

Default Re: Return valuse based on multiple "if exists' statements - 02-07-2010 , 07:45 PM



Thanks again,Plamen!

SQL Learner

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.