Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



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

Posts: 1
Trades: 0
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
 
 
Register now for full access!
Old 06-29-2007, 03: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,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by ForrestCroce; 06-29-2007 at 03: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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.09628 seconds with 12 queries