News and Information for the Data Integration community

April 2007

 

SAP related solutions

Sage Software related solutions

Data migration solutions

Weekly Webinar on xFusion. Register via email to:
info@softlabsco.com

Newsletter Unsubscribe

More Newsletters

Most Recent

June 2007

May 2007

Mar 2007

Feb 2007

Jan 2007

Dec 2006

Nov 2006

Recursive SQL Queries

In SQL Server 2000, there was no inherent way to perform hierarchical queries. SQL Server 2005 supports defining recursive queries, making it easy to work with hierarchical data. In this article, Dr. Pradeep Tapadiya explains the recursive query feature in SQL Server 2005.

Announcements

Latest happenings in xFusion and at Software Labs.

Did you know?


From the support desk

 
 

Recursive SQL Queries

Author: Dr. Pradeep Tapadiya

 

Consider the following table that shows employees and their immediate supervisors:

  EmpID Name SupervisorID
1
Clark Kent
2
Bruce Wayne
1
3
Peter Parker
1
4
Bruce Banner
1
5
Robin
2

This table represents a hierarchical relationship. As a supervisor is also an employee, he or she too has a supervisor to report to. The chain continues until you reach the root of the hierarchy.

In any such organizational structure, some frequently asked questions are:

  1. Who are all the supervisors, direct or indirect, for a specific employee?

  2. Does an employee report to a specific supervisor, either directly or indirectly?

In SQL Server 2000, there was no inherent way to perform hierarchical queries. SQL Server 2005 supports defining recursive queries, making it easy to work with hierarchical data.

I will show the recursive query first and then explain the specifics of the query.

The following recursive query flattens the hierarchy such that all legal employee-supervisor combinations can be shown:

 

WITH SupervisorList AS (
    SELECT EmpID, SupervisorID
    FROM League
    WHERE NOT SupervisorID is NULL

  UNION ALL

    SELECT A.EmpID, B.SupervisorID
    FROM
    League AS A
     INNER JOIN
     SupervisorList AS B
     ON
     A.SupervisorID = B.EmpID
)
SELECT * FROM SupervisorList


Here is the resulting output:

  EmpID SupervisorID
2
1
3
1
4
1
5
2
5
1

In a recursive query, we start with an initial set of data. We supplement this data with new data. The resulting data is once again supplemented with more data. The process continues until all the data is exhausted.

Here is a simplified syntax of a recursive query:

 

WITH QueryName AS
(
     StartingQuery
     UNION ALL   
     QueryWithMoreData
)
Statement that uses QueryName


In our case, the starting query is:

      SELECT EmpID, SupervisorID
    FROM League
    WHERE NOT SupervisorID is NULL

We join the result with the original table such that the supervisor-id from the original table match the employee-id from the resulting data:

      SELECT A.EmpID, B.SupervisorID
    FROM
    League AS A
     INNER JOIN
     SupervisorList AS B
     ON
     A.SupervisorID = B.EmpID

The intermediate resulting tables as well as the final resulting table are named SupervisorList. At the end of the recursion, we simply get the data from this resulting table using our standard SELECT statement.

Note that, if not properly designed, you may get an infinite recursion. By default, SQL Server 2005 stops the execution after 100 recursions. However, a different limit can be specified as part of the query.

Hope you now have a good idea on building recursive query. Solving the two questions that were mentioned at the beginning of the article is left as an exercise for you.

As usual, you can download the corresponding xFusion packs from xFusion Developers Network website.

New Partners

We are happy to announce the addition of IQ Consulting Services to our growing list of partners. xFusion will help them to improve customer data integration across SAP and other business applications.

Connecting Business Applications
More companies are struggling with exchanging data among departments or external offices. Whether the business information is internal or external, there are solutions that improve data sharing while maintaining security. To learn more about managing distributed data within or across your company, please email info@softlabsco.com . One of our many customer examples could be a similar scenario to yours!

New Events
Software Labs attended the world's largest annual technical conference and tradeshow, CeBIT, in Hannover, Germany. We were pleased to meet with many partners and customers at the event. For more information on the next year's event, visit them online here: www.cebit.de

Did you know xFusion has its own Scheduler component?

xFusion 3.1 includes a Scheduler service and administration UI. Read more…

Support Questions:

Dear Support,

I get a lot of Microsoft Excel files from my clients everyday and I want to get just the last row from the files for validating my accounts.

The format of all those files is the same. What is the easiest way to get the trailer record from these files using xFusion Studio?

Hi,

Here are the steps:

  1. Create a connection to the file using the Excel connector.

  2. Create a standard query to read the data from the file. Let’s say the query name is “RawData.”

  3. Create a transform query with the following function over this standard query to get the last row.

 

SelectRow(
[[RawData]], array(count([RawData.Column]))
)

Function "SelectRow" gets the row/rows specified by the second parameter. Since we are interested only in the last row, we will pass the count of the rows as this parameter.

This way you can get the last row from any file by just changing the file name in the connection settings.

Hope this helps !

Best regards,

Support


Software Labs, Inc. 1225 Pleasant Grove Blvd. #100, Roseville CA 95768, USA
ph: 1-916-773-6272 fax: 916-773-6281 web: http://www.softlabsco.com email: info@softlabsco.com
unsubscribe via email: unsubscribe@softlabsco.com unsubscribe via web: click here