Reply
SQLtoselect one record for the first matching condition only in four where conditions
Old 06-29-2007, 01:57 AM SQLtoselect one record for the first matching condition only in four where conditions
Junior Talker

Posts: 1
I need your suggestion to write one tricky SQL query to select only one record from database on the following condition.I explained simple table structure below.I have a table temp with four columns a,b,c,d in it.

I have to select column d from this temp table based on the following four conditions.If it matches any condition, It should skip other conditions, that's the tricky thing.

Conditions order is like shown below.

1) a='argument1' and b='argument2' and c='argument3'(If it matches this condition, it should stop selecting below 3 conditions)

2) a='argument1' and b='argument2' and c='none'(If it matches this condition, it should stop selecting below 2 conditions)

3) a='argument1' and b='none' and c='argument3'(If it matches this condition, it should stop selecting below condition)

4) a='argument1' and b='none' and c='none'(this is last condition)

If I use OR operator , it matches all of those other conditions too.I never wrote query like this before.

I greatly appreciate if somebody sheds light on me to start writing this query with a simple suggestion.

Thanks,
GD
gdesai is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 06-29-2007, 02:40 AM Re: SQLtoselect one record for the first matching condition only in four where condit
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,025
Name: Forrest Croce
Location: Seattle, WA
Code:
Select Top 1 * From temp Where
   (a='argument1' and b='argument2' and c='argument3') Or
   (a='argument1' and b='argument2' and c='none') Or
   (a='argument1' and b='none' and c='argument3') Or
   (a='argument1' and b='none' and c='none')
Order By
   Case
      When a='argument1' and b='argument2' and c='argument3' Then 1
      When a='argument1' and b='argument2' and c='none' Then 2
      When a='argument1' and b='none' and c='argument3' Then 3
      When a='argument1' and b='none' and c='none' Then 4
    End
That will work in SQL Server; Oracle would be Where ... And RowNum = 1, probably with slightly different syntax in the case statement, and MySQL is going to be a little different still.

You probably want to put that case block a second time in the select list, and use it to show why a particular row was selected.

Last edited by ForrestCroce : 06-29-2007 at 02:43 AM.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Reply     « Reply to SQLtoselect one record for the first matching condition only in four where conditions
 

Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13117 seconds with 13 queries