English 中文(简体)
Parent-Child view from table that is self referencing (ID)? (for tsql gurus.)
原标题:

I have a organization name table with the following structure given below:

CREATE TABLE [dbo].[DP_ORG_OrganizationUnit](
    [GID] [uniqueidentifier] NULL,
    [ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [Code] [nvarchar](100) NULL,
    [Name] [nvarchar](100) NULL,
    [LastUpdated] [datetime] NULL,
    [ManagedBy] [int] NULL, **SELF REFERENCING ID {For parent - child }***
    [Manager] [int] NULL,
 CONSTRAINT [PK_DP_ORG_OrganizationUnit] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Requirement is that I need to create a view that will give me a full path for each organization unit.

ID   CODE  NAME                    MANAGEDBY  MANAGER
1    HO    Head Office             0
2    IT    Information Technology  1
3.   FI    Finance                 1
4.   SP    IT Support              2
5.   M     Mergers                 3 

I need the data to come as

1. Head Office
2. Head Office/Information Technology
3. Head Office/Finance
4. Head Office/Information Technology/IT Support
5. Head Office/Finance/Mergers

I want to directly pull this into a criteria form of a report so that the can select the sub departments from one single combo! How can I get this in any manner from TSQL(ms sql 2005). Thanks in advance for any solution.

Solution #1:

with cteAnchor as (
 select ID,CAST(Name as nvarchar(500)) as Name
 from DP_ORG_OrganizationUnit
 where ManagedBy  = 21)
, cteRecursive as 
 (select ID,CAST(Name as nvarchar(500))as Name
  from cteAnchor
 union all 
 select t.ID,CAST( r.Name +   /   + t.Name AS nvarchar(500))
 from DP_ORG_OrganizationUnit t
 join cteRecursive r on t.ManagedBy = r.ID)
select * from cteRecursive;
最佳回答

See Recursive Queries Using Common Table Expressions:

with cteAnchor as (
 select Name, id
 from DP_ORG_OrganizationUnit
 where ManagedBy  = 0)
, cteRecursive as (
 select id, Name
  from cteAnchor
 union all 
 select t.id, r.Name +  /  + t.Name
 from DP_ORG_OrganizationUnit t
 join cteRecursive r on t.ManagedBy = r.id)
select * from cteRecursive;
问题回答

暂无回答




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签