Advertisement

08.03.2008 at 05:08PM PDT, ID: 23617757
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.0

Sparse hierarchical data with inheritence - parent/child or nested-set?

Asked by jimbobmcgee in SQL Query Syntax, MS SQL Server, SQL Server 2005

Tags:

I have a requirement to store hierarchical data in a SQL table.  However, if a field is null, it should 'inherit' its value from the first non-null parent value.  There are a number of fields in the table that must exhibit this behavior and the nullity of each field is mutually exclusive.

I have worked out an example, using a parent/child (adjacency list?) table that appears to perform this task, using the WITH keyword of SQL Server 2005, but the use of SQL Server is not yet confirmed -- the actual DBMS may not support WITH.

As such, I have looked at Celko's work on nested sets and can query nodes, but cannot work out the required SQL for the field inheritance (or even if it's possible).

I have attached the SQL Server code; can anyone help me with a performant conversion to nested-set, in case I cannot use SQL Server.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
-- CREATE ADJACENT-LIST/PARENT-CHILD TREE
CREATE TABLE dbo.sparse_tree_pc
(
  node_id int NOT NULL IDENTITY(1,1), 
  parent_id int NULL, 
  node_name varchar(255) NOT NULL, 
  sparse_int int NULL,
  sparse_date datetime NULL,
  CONSTRAINT pk_sparse_tree_pc PRIMARY KEY (node_id),
  CONSTRAINT fk_sparse_tree_pc_pid FOREIGN KEY (parent_id) REFERENCES sparse_tree_pc
)
GO
 
-- INSERT STANDING DATA
INSERT INTO dbo.sparse_tree_pc VALUES (NULL, 'Colors',      10,   GETDATE())
INSERT INTO dbo.sparse_tree_pc VALUES (1,    'Red',         5,    NULL)
INSERT INTO dbo.sparse_tree_pc VALUES (1,    'Yellow',      NULL, '01-Jan-2008')
INSERT INTO dbo.sparse_tree_pc VALUES (1,    'Green',       NULL, NULL)
INSERT INTO dbo.sparse_tree_pc VALUES (2,    'Cherry',      NULL, NULL)
INSERT INTO dbo.sparse_tree_pc VALUES (2,    'Postbox',     7,    '02-Feb-2008')
INSERT INTO dbo.sparse_tree_pc VALUES (3,    'Fruit',       NULL, NULL)
INSERT INTO dbo.sparse_tree_pc VALUES (7,    'Lemon',       12,   '03-Mar-2008')
INSERT INTO dbo.sparse_tree_pc VALUES (7,    'Banana',      17,   NULL)
INSERT INTO dbo.sparse_tree_pc VALUES (3,    'Tennis ball', 6,    NULL)
INSERT INTO dbo.sparse_tree_pc VALUES (4,    'Grass',       NULL, '04-Apr-2008')
INSERT INTO dbo.sparse_tree_pc VALUES (4,    'Leaf',        NULL, NULL)
GO
 
-- PATH PADDER
DECLARE @pad INT, @stuff VARCHAR(MAX);
SELECT @pad = LEN(CAST(MAX(node_id) AS VARCHAR)) FROM sparse_tree_pc;
SELECT @stuff = REPLICATE('0', @pad);
 
-- STANDARD TREE QUERY
WITH rec(node_id, parent_id, node_name, sparse_int, sparse_date, level, path) AS (
  SELECT node_id,
         parent_id,
         node_name,
         sparse_int,
         sparse_date,
         0,
         CAST('/' + RIGHT(@stuff + CAST(node_id AS VARCHAR), @pad) AS VARCHAR)
  FROM   sparse_tree_pc
  WHERE  parent_id IS NULL
  UNION ALL
    SELECT t.node_id,
           t.parent_id,
           t.node_name,
           t.sparse_int,
           t.sparse_date,
           r.level + 1,
           CAST(r.path + '/' + RIGHT(@stuff + CAST(t.node_id AS VARCHAR), @pad) AS VARCHAR)
    FROM   rec r
           INNER JOIN sparse_tree_pc t ON t.parent_id = r.node_id
)
SELECT   node_id,
         parent_id,
         REPLICATE('...', level) + node_name AS node_name,
         sparse_int,
         sparse_date,
         level,
         path
FROM     rec
ORDER BY path;
 
-- INHERIT-FROM-PARENT TREE QUERY
WITH rec(node_id, parent_id, node_name, sparse_int, sparse_date, level, path) AS (
  SELECT  node_id,
      parent_id,
      node_name,
      sparse_int,
      sparse_date,
      0,
      CAST('/' + RIGHT(@stuff + CAST(node_id AS VARCHAR), @pad) AS VARCHAR)
  FROM  sparse_tree_pc
  WHERE  parent_id IS NULL
  UNION ALL
    SELECT t.node_id,
           t.parent_id,
           t.node_name,
           COALESCE(t.sparse_int, r.sparse_int),
           COALESCE(t.sparse_date, r.sparse_date),
           r.level + 1,
           CAST(r.path + '/' + RIGHT(@stuff + CAST(t.node_id AS VARCHAR), @pad) AS VARCHAR)
    FROM   rec r
           INNER JOIN sparse_tree_pc t ON t.parent_id = r.node_id
)
SELECT   node_id,
         parent_id,
         REPLICATE('...', level) + node_name AS node_name,
         sparse_int,
         sparse_date,
         level,
         path
FROM     rec
ORDER BY path
[+][-]08.03.2008 at 11:02PM PDT, ID: 22150201

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.04.2008 at 04:15AM PDT, ID: 22151443

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.04.2008 at 08:14AM PDT, ID: 22153117

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.17.2008 at 03:12PM PDT, ID: 22745836

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]10.17.2008 at 05:19PM PDT, ID: 22746488

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.20.2008 at 01:23PM PDT, ID: 22761776

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.25.2008 at 07:16AM PDT, ID: 22803348

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]10.25.2008 at 09:54AM PDT, ID: 22804144

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.07.2008 at 01:59PM PST, ID: 22908919

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]11.11.2008 at 07:20AM PST, ID: 22931284

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Query Syntax, MS SQL Server, SQL Server 2005
Tags: SQL
Sign Up Now!
Solution Provided By: Computer101
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628