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 > Sub Query Record Limit

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-10-10, 09:11
mohdalihpk mohdalihpk is offline
Registered User
 
Join Date: Mar 2010
Posts: 4
Sub Query Record Limit

Dear All,
I am trying to execute a sub-query and it has multiple records but I want just one record for it. What is it's way.

I am thankful to you all who will trying to help me.

Yours,
Ali
Reply With Quote
  #2 (permalink)  
Old 03-10-10, 09:35
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,473
You query is wrong. (Maybe if you post the query and what you want it to do you would get a better answer).

Andy
Reply With Quote
  #3 (permalink)  
Old 03-10-10, 09:54
mohdalihpk mohdalihpk is offline
Registered User
 
Join Date: Mar 2010
Posts: 4
Select a.name from A a
where
exist (select dept from Dept where dept_loc like 'N%')

The sub-query has 200,000 records and I just want to limited it as if it return 200,000 record it just check if one is exist then it return true result rather it check all the 200000 records.
Reply With Quote
  #4 (permalink)  
Old 03-10-10, 10:07
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 779
With the exists your subquery is not getting all 200K records, just the first one. Though with the query you show, you will get every row from table A, if a row does exist in your subquery.
Dave
Reply With Quote
  #5 (permalink)  
Old 03-10-10, 10:08
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,473
You can add the FETCH FIRST 1 ROW ONLY clause to the subquery.


Andy
Reply With Quote
  #6 (permalink)  
Old 03-10-10, 10:17
mohdalihpk mohdalihpk is offline
Registered User
 
Join Date: Mar 2010
Posts: 4
In sub-Query Fetch First 1Rows Only not allowed.
Reply With Quote
  #7 (permalink)  
Old 03-10-10, 10:44
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 1,827
Quote:
In sub-Query Fetch First 1Rows Only not allowed.
What platform and DB2 version/release are you using?

By referencing manuals, fetch-first-clause in subquery was supported from
DB2 Universal Database(for LUW) Version 8,
DB2 Version 9.1 for z/OS,
and
DB2 for i Version 6 Release 1.
Reply With Quote
  #8 (permalink)  
Old 03-18-10, 08:53
Vaibhav Vyas Vaibhav Vyas is offline
Registered User
 
Join Date: Jul 2009
Posts: 17
You can use ROW_NUM function
__________________
Vyas| Miracle Happens
Reply With Quote
  #9 (permalink)  
Old 03-18-10, 10:14
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 1,827
Quote:
With the exists your subquery is not getting all 200K records, just the first one. Though with the query you show, you will get every row from table A, if a row does exist in your subquery.
Dave
Dave must be completely right.

My guess is that there might be some relationship between A and Dept,
something like...
Code:
SELECT a.name
  FROM A a
 WHERE 
       EXISTS
       (SELECT *
          FROM Dept
         WHERE Dept.dept = a.dept
           AND dept_loc LIKE 'N%')
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

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