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 > declaring select statement result as variable?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-07-08, 10:13
pgrimard pgrimard is offline
Registered User
 
Join Date: Jul 2008
Posts: 1
Question declaring select statement result as variable?

I'm using DB2 on an AS400, not sure the version of DB2, but the AS400 is V5R2.

I currently have a subselect as a column in my query that returns a count. Here's the subselect:

Code:
  select count(subitemsis.wxshp#) from i93file/whitemsis subitemsis     
  left join i93file/whitem subitem on subitemsis.wxshp# = subitem.wmshp#
  where subitemsis.wxobok = itemsis.wxobok                              
  and subitem.wmord# != ''
I want to somehow declare the result of this subselect as a variable and just use that variable name in my main query as needed, either for display or for arithmetic operations. I'm no expert in SQL, so not sure what I should do.

My whole query looks like this so far, but it's still a work in progress:

Code:
select                                                                        
distinct(itemsis.wxobok) as Booking, item.wmsord as Shipper_Ref,              
itemsis.wxocldt as Closing,                                                   
                                                                              
/* Count all warehouse shipments with matching booking */                     
(                                                                             
  select count(subitemsis.wxshp#) from i93file/whitemsis subitemsis           
  left join i93file/whitem subitem on subitemsis.wxshp# = subitem.wmshp#      
  where subitemsis.wxobok = itemsis.wxobok                                    
  and subitem.wmord# != ''                                                    
) as Orders,                                                                  
                                                                              
/* Calculate number of missing containers to complete booking */              
(                                                                             
                                                 
) as Missing_Cntrs                                                            

from i93file/whitemsis itemsis                                                
left join i93file/whitem item on itemsis.wxshp# = item.wmshp#                 
where itemsis.wxobok != '' and itemsis.wxocldt > '07/07/2008'                 
order by Closing asc
I'm working on a second subselect "Missing_Cntrs" that needs to calculate the number of missing containers based on how many are currently on file compared to the total amount needed. So I would need to subtract the count of what's on file from the total needed. I haven't written the SQL to retrieve what's on file, but just need to know how to use the first subselect as a variable first.

Any help is appreciated, thanks.
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