LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-01-2004, 06:04 PM   #1
JMagana
LQ Newbie
 
Registered: Nov 2004
Distribution: kde
Posts: 1

Rep: Reputation: 0
Question Recursive Query


Hello, I am doing a project in db2. But I am not sure how to create a recursive query.
THanks
Javier Magana
 
Old 12-01-2004, 07:46 PM   #2
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
do a search on google for "db2 recursive query" (without the quotes) and you'll get lots of information...

i just did that search, and among the results was this example from the ibm website:

Code:
-----------------------------------------------------------------------------
 -- Licensed Materials - Property of IBM
 --
 -- Governed under the terms of the International
 -- License Agreement for Non-Warranted Sample Code.
 --
 -- (C) COPYRIGHT International Business Machines Corp. 1995 - 2002
 -- All Rights Reserved.
 --
 -- US Government Users Restricted Rights - Use, duplication or
 -- disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 -----------------------------------------------------------------------------
 --
 -- SOURCE FILE NAME: flt.db2
 --
 -- SAMPLE: How to do a RECURSIVE QUERY
 --
 -- SQL STATEMENTS USED:
 --         DROP TABLE
 --         CREATE TABLE
 --         INSERT
 --         SELECT
 --
 -- OUTPUT FILE: flt.out (available in the online documentation)
 -----------------------------------------------------------------------------
 --
 -- For more information about the command line processor (CLP) scripts,
 -- see the README file.
 --
 -- For information on using SQL statements, see the SQL Reference.
 --
 -- For the latest information on programming, building, and running DB2
 -- applications, visit the DB2 application development website:
 --     http://www.software.ibm.com/data/db2/udb/ad
 -----------------------------------------------------------------------------

create table flights (source varchar ( 8 ),
                      destination varchar ( 8 ),
                      d_time integer ,
                      a_time integer ,
                      cost smallint ,
                      airline varchar ( 8 ));

INSERT INTO FLIGHTS VALUES ( 'Paris' ,   'Detroit' ,  null,null, 700 , 'KLM' ),
                           ( 'Paris' ,   'New York' , null,null, 600 , 'KLM' ),
                           ( 'Paris' ,   'Toronto' ,   null,null, 750 , 'AC' ),
                           ( 'Detroit' , 'San Jose' , null,null, 400 , 'AA' ),
                           ( 'New York' , 'Chicago' ,  null,null, 200 , 'AA' ),
                           ( 'Toronto' ,  'Chicago' ,  null,null, 275 , 'AC' ),
                           ( 'Chicago' , 'San Jose' , null,null, 300 , 'AA' );

WITH
 REACH (SOURCE, DESTINATION, COST, STOPS) AS
   ( SELECT SOURCE, DESTINATION, COST, CAST( 0  AS SMALLINT )
      FROM FLIGHTS
       WHERE SOURCE = 'Paris' 
    UNION ALL
     SELECT R.SOURCE, F.DESTINATION, CAST(R.COST+F.COST AS SMALLINT ), CAST(R.STOPS+ 1  AS SMALLINT )
     FROM REACH R, FLIGHTS F
      WHERE R.DESTINATION=F.SOURCE
        AND R.STOPS < 5
   )
SELECT DESTINATION, COST, STOPS FROM REACH;

drop table flights;

Last edited by win32sux; 12-01-2004 at 07:47 PM.
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
recursive move stefaandk Linux - Newbie 7 10-09-2012 08:36 PM
Recursive make SeanatIL Linux - Software 2 08-04-2004 03:47 PM
rm -r what is recursive wogga Linux - Software 3 05-28-2004 02:29 PM
chmod.....recursive help stateq2 Linux - General 3 03-28-2004 07:28 PM
recursive downloads watashiwaotaku7 Linux - General 4 03-08-2003 09:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration