If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Build a sql query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-06-07, 12:07
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
 
Join Date: Jan 2005
Posts: 75
Build a sql query

Hi ,

Could you please help me building a query with the below requirements.

Table1 has got 5 columns.
(PK)col1 col2 col3 col4 col5
1 a b c d
2 e f h
3 I k l
4 m n o p
5 q r s t

I need to have one single query where in i would have to compare compare against all the columns, if a value is null then still that row should be be fetched. like

select * from table1 where col1=1 and/or col2=a and/or col3=b and/or col4=c and/or col5=""

the first row should be displayed. like if one value is missing then that value has to be ignored and others has to be conisidered. I know i can do that with the programming logic. But i need to find out a way doing it with a sql query.
Reply With Quote
  #2 (permalink)  
Old 02-06-07, 12:49
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I am not sure I understand what you want to return. Could you be more specific and supply some examples of what you expect to see from what you have?

Andy
Reply With Quote
  #3 (permalink)  
Old 02-06-07, 13:58
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
If I got this right (pending the requested explanation), I think you just have to do this:

SELECT ...
FROM ...
WHERE col1 = 1 AND ( col2 IS NULL OR col2 = ... ) AND ( col3 IS NULL OR col3 = ... ) AND ...

p.s: Read up on the semantics of NULL and how it is used in predicates.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 02-06-07, 14:02
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I do not think that that is what is desired. But if it is, then ther is even an easier way:

select * from table1 where col1 = 1 and coalesce(col2,'a') = 'a' and coalesce(col3,'b') = 'b' and ...

Andy
Reply With Quote
  #5 (permalink)  
Old 02-08-07, 07:32
vini_srcna vini_srcna is offline
Registered User
 
Join Date: May 2006
Posts: 82
Quote:
the first row should be displayed. like if one value is missing then that value has to be ignored and others has to be conisidered. I know i can do that with the programming logic. But i need to find out a way doing it with a sql query.
like if one value is missing then that value has to be ignored and others has to be conisidered.............?????

Do you mean to say if the value is missing or mismatching then that column should be eliminated from the result set...? (** dont think this is a poor question, just need to confirm what you need)

OR
Did you mean to say that particular row should be eliminated from the result set..?

Is this query STATIC (will you embedd in program ..?)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On