SunQuest
           DB2 Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDB2 Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old March 10th, 2006, 02:59 PM
anaik100 anaik100 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 2 anaik100 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 16 sec
Reputation Power: 0
Db2 sql query modified version query

hi guys,

my requirement has changed a little so i am putting it in a new post...

first requirement

SELECT * FROM tab1 a,tab2 b WHERE
a.ID='b.col1+b.col2'

i want to append two columns data to form a where clause of the query..

i am trying to put it in DB2..but not successfull..any help


second requirment

my first query gives me the output as colA, colB
select colA,colB from tab1 where colC='XXX'

no my second query i write to get the value of colA,colB
select * from tab2 where col1=colA
select * from tab2 where col1=colB
NOW I WANT TO DO THIS FROM A SINGLE QUERY

Reply With Quote
  #2  
Old March 29th, 2006, 07:28 AM
jaaput jaaput is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Zeist, Netherlands
Posts: 19 jaaput User rank is Lance Corporal (50 - 100 Reputation Level)jaaput User rank is Lance Corporal (50 - 100 Reputation Level)jaaput User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 16 h 15 m 4 sec
Reputation Power: 0
Hi Anaik

Your first requirement seems like a concatenation.
The way to code that in DB2 is:
Quote:
a.ID = b.col1 CONCAT b.col2
assuming that all 3 columns are of a string/character-type.
If that assumption is incorrect, you have to convert the numeric
columns to characters using the function CHAR(), e.g.:
Quote:
a.ID = CHAR(b.col1) CONCAT b.col2
when col1 is of a numeric type.

Now regarding your second requirement: I assume there is an
OR-relation between the SELECTS on tab2. I suggest either a subselect:
Code:
SELECT  *
FROM    tab2
WHERE   col1 IN
        (SELECT colA from tab1 where colC='XXX')
OR      col1 IN
        (SELECT colB from tab1 where colC='XXX')
;
or a join:
Code:
SELECT  b.*
FROM    tab1 a
      , tab2 b
WHERE   (b.col1 = a.colA OR b.col1 = a.colB)
AND     a.colC = 'XXX'
;

Your choice may depend on the importance of:
  • maintenance (and thus clarity of the whole query, in this case I would favour the subselect)
  • performance (where the join may have an advantage, as the optimizer will easier conclude
    that one pass through tab1 will suffice)
The only difference between both these and your original is the fact that your original queries
may yield some duplicate values in cases where colA and colB have the same value within the same row.
The OR in both my queries will reduce the two resultvalues to one row, whereas your example will produce
that resultvalue in both the queries on tab2, and you would end up with more resulting rows.
If that really is a problem please let me know.

Regards, Jaap.
19'3/112

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDB2 Development > Db2 sql query modified version query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway