Getting Started with Adobe After Effects - Part 6: Motion Blur
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

View in another Language

Sponsored [Advertise Here]
Syndicate your blog!
beyondrelational.com
Syndicate your blog with us to get wider reach into the technology community. Click here to get started.

Don't have a blog yet?
beyondrelational.com
Start writing a blog at beyondrelational.com, get recognized and build your brand

Izenda Reports
izenda.com
The easiest and fastest way to create ad hoc reports from SQL Server

TSQL Challenge 60 - Generate a relational tree structure from a string

An ETL application receives product and category information in a text file. The category hierarchy is specified as a string. Your job is to read the hierarchy information and generate a relational table (result set) representing the hierarchy of categories.

Want to make SQL effortless?
With code-completion, SQL reformatting, script summaries and more, SQL Prompt 5 makes writing, editing, and exploring SQL effortless. Download a 14-day free trial now.

Sample Data

Categories Table

Categories
---------------------------
food/biscuits/britania
food/milk
food/baby/icecream
food/baby/milk powder
detergents/liquids/handwash

Expected Results

id  name        parent
--- ----------- -------
1   detergents  null
2   food        null
3   liquids     1
4   baby        2
5   biscuits    2
6   milk        2
7   handwash    3
8   icecream    4
9   milk powder 4
10  britania    5

Rules

  1. Pay attention to the order of the output table. The "ID" values of the categories are generated in a special way. The values are ordered by "parent" and then by "name".
  2. There is no limit in the number of levels in the hierarchy.
  3. Each hierarchy node will have at most one parent.
  4. Each category ends in a terminal node of a hierachy and begins with the main parent node of the hierarchy.
  5. There will be no duplicate entries.

Sample Script

Use the TSQL Script given below to generate the source table and fill them with sample data.

IF OBJECT_ID('TC60','U') IS NOT NULL BEGIN
	DROP TABLE TC60
END
GO

CREATE TABLE TC60(
	Categories VARCHAR(MAX)
)
GO

INSERT INTO TC60(Categories)
SELECT 'food/biscuits/britania' UNION ALL
SELECT 'food/milk' UNION ALL
SELECT 'food/baby/icecream' UNION ALL
SELECT 'food/baby/milk powder' UNION ALL
SELECT 'detergents/liquids/handwash' 

SELECT * FROM TC60

Restrictions

  1. The solution should be a single query that starts with a "SELECT" or “;WITH”

Notes

  1. Read the Submission Guidelines and make sure that your solution follows them.
  2. If you would like to use a Tally Table, you can use the script given here. Your solution should not include the script to create and populate the tally table. You can assume that the tally table will be available in the database where the evaluation team will run your Code.
Connect any source control system to SQL Server
SQL Source Control 2.1 connects all source control systems - TFS, SVN, Vault Professional, Kiln, Mercurial, Git, Perforce, CVS, and Bazaar - to SQL Server.
More information and free trial.

Jacob Sebastian
1 · 100.00% · 32002
Submit a Solution
View Solutions
View All Challenges Previous Challenge Next Challenge
9
Liked
40
Comments
74
Solutions



Submit

40   Comments   

Subscribe to Notifications
  • As I understand it, that would mean that "apple" has two parents ... "computers" and "food" ... which would not be allowed

    commented on Jul 19 2011 7:25PM  .  Report Abuse This post is not formatted correctly
    Pierre
    444 · 0% · 87
  • Quote from Jacob in Page 2:

    "It is possible to have 'apple' in food and computers."

    commented on Jul 20 2011 1:18AM  .  Report Abuse This post is not formatted correctly
    diegoperdices
    112 · 1% · 459
  • Hello, my stats:

    (5007 row(s) affected) Table 'Worktable'. Scan count 0, logical reads 1041, physical reads 9, read-ahead reads 0, lob logical reads 1648, lob physical reads 6, lob read-ahead reads 0. Table 'TC60'. Scan count 2, logical reads 78, physical reads 1, read-ahead reads 39, lob logical reads 3501192, lob physical reads 34, lob read-ahead reads 0.

    SQL Server Execution Times: CPU time = 250330 ms, elapsed time = 286668 ms.

    (I'm working in VBOX on a slow computer)

    1 Computers NULL 2 Food NULL 3 x NULL 4 Apple 1 5 Apple 2 6 a11 3 7 a101 3 8 a1001 3 9 a111 3 10 a121 3 (...) 5000 aaaaaaaaa10097 4996 5001 a9797 4997 5002 a9897 4998 5003 aaaaaaaaa9997 4999 5004 aaaaa10098 5000 5005 aaaaa9998 5003 5006 a10099 5004 5007 a99_99 5005

    commented on Jul 20 2011 8:10AM  .  Report Abuse This post is not formatted correctly
    Zibi
    25 · 6% · 2029
  • Table 'spt_values'. Scan count 5, logical reads 55, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    Table 'TC60'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    SQL Server Execution Times: CPU time = 16 ms, elapsed time = 15 ms.

    SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms.

    commented on Jul 20 2011 8:50AM  .  Report Abuse This post is not formatted correctly
    Pierre
    444 · 0% · 87
  • Can anyone send me the test data that you guys are using??

    commented on Jul 20 2011 8:53AM  .  Report Abuse This post is not formatted correctly
    Pierre
    444 · 0% · 87
  • @pierre

    look at first page of comments, its in one of leszek posts

    commented on Jul 20 2011 8:55AM  .  Report Abuse This post is not formatted correctly
    diegoperdices
    112 · 1% · 459
  • 1ComputersNULL
    2FoodNULL
    3xNULL
    4Apple1
    5Apple2
    6a1_13
    7a10_13
    8a100_13
    9a11_13
    10a12_13

    4998aaaaa98_964896
    4999aaaaaaaaaaaaa99_964897
    5000aaaaaaaaa100_974898
    5001a97_974899
    5002a98_974900
    5003aaaaaaaaa99_974901
    5004aaaaa100_984902
    5005aaaaa99_984903
    5006a100_994904
    5007a99_994905

    (5007 row(s) affected)

    Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    Table 'tsqlc_Tally'. Scan count 102, logical reads 453, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    Table 'TC60'. Scan count 1, logical reads 47, physical reads 0, read-ahead reads 0, lob logical reads 2623, lob physical reads 0, lob read-ahead reads 0.

    SQL Server Execution Times: CPU time = 5484 ms, elapsed time = 5565 ms.

    commented on Jul 20 2011 10:35AM  .  Report Abuse This post is not formatted correctly
    Pierre
    444 · 0% · 87
  • @Pierre - my v1 result is wrong, as your

    commented on Jul 20 2011 1:14PM  .  Report Abuse This post is not formatted correctly
    Leszek Gniadkowski
    8 · 18% · 5718
  • 4998 aaaaa98_96 4994

    4999 aaaaaaaaaaaaa99_96 4995

    5000 aaaaaaaaa100_97 4996

    5001 a97_97 4997

    5002 a98_97 4998

    5003 aaaaaaaaa99_97 4999

    5004 aaaaa100_98 5000

    5005 aaaaa99_98 5003

    5006 a100_99 5004

    5007 a99_99 5005

    Stats are not so cool but result looks fine

    (5007 row(s) affected)

    Table 'Worktable'. Scan count 1, logical reads 1274668, physical reads 0, read-ahead reads 0, lob logical reads 912474, lob physical reads 0, lob read-ahead reads 0. Table 'tsqlc_Tally'. Scan count 204, logical reads 1880, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'TC60'. Scan count 2, logical reads 78, physical reads 0, read-ahead reads 0, lob logical reads 684972, lob physical reads 0, lob read-ahead reads 451964.

    SQL Server Execution Times: CPU time = 24843 ms, elapsed time = 24881 ms.

    commented on Aug 12 2011 8:18AM  .  Report Abuse This post is not formatted correctly
    Dhiraj Gupta
    231 · 1% · 193
  • Got better result

    (5007 row(s) affected) Table 'Worktable'. Scan count 4905, logical reads 50053, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'tsqlc_Tally'. Scan count 204, logical reads 1880, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'TC60'. Scan count 2, logical reads 78, physical reads 0, read-ahead reads 0, lob logical reads 684972, lob physical reads 0, lob read-ahead reads 451964.

    SQL Server Execution Times: CPU time = 8500 ms, elapsed time = 8609 ms.

    commented on Aug 12 2011 8:45AM  .  Report Abuse This post is not formatted correctly
    Dhiraj Gupta
    231 · 1% · 193
Previous 1 | 2 | 3 | 4 Next

Your Comment


Sign Up or Login to post a comment.


Managed Windows Shared Hosting by OrcsWeb

Copyright © Rivera Informatic Private Ltd.