Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How Make a Hierarchical Selection with SQL Query Expand / Collapse
Author
Message
Posted Sunday, March 16, 2014 12:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, March 16, 2014 12:32 AM
Points: 1, Visits: 7
I have a problem in creating a SQL Query as follows :

I Have 2 Tables with following Specification and data:

http://dc699.4shared.com/img/lgtP3N_4ce/s3/144c7252ff8/SQL1.jpg

I want to create a SQL Select Query to return for me a Hierarchical Model Like this :

For example if the SID is 3 it should return for me this :

http://dc699.4shared.com/img/8UufpK2-ce/s3/144c7255af0/SQL2.jpg

Because the Num 3 in structure table related to data 7,8,9 and 9 is related to 10,11(Note that No 9 is related to 3 or in other words 9 is subset of 3)

Can anyone help me to create this Query? I have try for 2 weeks but I failed

Thanks so much
Post #1551540
Posted Sunday, March 16, 2014 1:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 27, 2014 8:31 AM
Points: 1, Visits: 243
Hi
I'm not sure if you need to do a join on the personel tale but you can use a rank function

RANK ( ) OVER ( [ partition_by_clause ] order_by_clause ) to rank your result sets into some sort of temp table or CTE and query the your output from there
so using your two tables Personel and Structure I have written this script against the structure table that should start you off but if you need to do a join it should be simple. let me know if this helps

WITH Personel_Structure
AS
(
SELECT [SID],MID, RANK() OVER(PARTITION BY [SID] ORDER BY MID ASC) AS POS
FROM Structure
WHERE [SID] = 3
)
SELECT [SID],MID
FROM Personel_Structure
ORDER BY POS ASC
Post #1551543
Posted Monday, March 17, 2014 7:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:08 PM
Points: 13,205, Visits: 12,687
Hi and welcome to the forums. There are lots of people around here willing and able to help. But posting pictures of your tables does not provide the kind of detail we need in order to help. You need to help us to help by posting:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1551726
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse