Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Union on tables with parent-child records and Sorting

$
0
0

Hi,

I have an application that has an existing query which returns org units (parent and child) from organization table with a sort on createddate +  org_id combination

WITH Org_TREE AS (
SELECT *, null as 'IS_DELETED', convert (varchar(4000), convert(varchar(30),CREATED_DT,126) + Org_Id) theorderby
FROM Organization WHERE PARENT_Org_ID IS NULL and case_ID='43333'
UNION ALL
SELECT a1.*, null as 'IS_DELETED', convert (varchar(4000), a2.theorderby + convert(varchar(30),a1.CREATED_DT,126) + a1.Org_Id)
FROM Organization a1 INNER JOIN Org_TREE a2 ON a1.PARENT_Org_ID = a2.Org_Id and case_ID='43333'
) 
SELECT * FROM Org_TREE order by theorderby

I have created a new log table for organization 'Organization_Log' with exact columns as Organization table with an additional 'IS_DELETED' bool column.

Questions:

I need to modiy the query,

1. To display the parent and child records both from the organization table and organization_log table.

2. the sort on the result should be based on 'Organization Name' column asc. First with parent org and the child org underneath it. For eg.

aaa

==>fff

==>ggg

bbb

==> aaa

==> hhh

Any help on how the query should be constructed?

Thanks

gkol


Viewing all articles
Browse latest Browse all 23857

Trending Articles