我有一个数据库,用于存储组织的成本信息,按部门细分和按年份划分时间段。成本结构涉及父子关系;用户可以指定结构中任何级别的成本值,唯一的限制是如果任何子节点有值,则层次结构中更高级别的所有值都计算为子节点的总和;作为子节点总和的父节点值不存储在数据库中。
我需要一个查询,它将根据他们的孩子递归地计算父母的值,并且对于没有零值的孩子将被设置(T-SQL,SQL 2008R2)
[SQL Fiddle] MS SQL Server 2008 架构设置:
CREATE TABLE CostStructureNodes (
Id INT NOT NULL PRIMARY KEY,
Name NVARCHAR(250) NOT NULL,
ParentNodeId INT,
FOREIGN KEY(ParentNodeId) REFERENCES CostStructureNodes(Id)
);
CREATE TABLE Years (
Year INT NOT NULL PRIMARY KEY
);
CREATE TABLE CostsPerYear (
NodeId INT NOT NULL,
Year INT NOT NULL,
Value DECIMAL(18,6) NOT NULL,
PRIMARY KEY(NodeId, Year),
FOREIGN KEY(NodeId) REFERENCES CostStructureNodes(Id),
FOREIGN KEY(Year) REFERENCES Years(Year)
);
INSERT INTO CostStructureNodes VALUES ('1', 'Total Costs', NULL);
INSERT INTO CostStructureNodes VALUES ('2', 'R&D', 1);
INSERT INTO CostStructureNodes VALUES ('3', 'Legal', 1);
INSERT INTO CostStructureNodes VALUES ('4', 'HR', 1);
INSERT INTO CostStructureNodes VALUES ('5', 'IT', 1);
INSERT INTO CostStructureNodes VALUES ('6', 'Software', 5);
INSERT INTO CostStructureNodes VALUES ('7', 'Hardware', 5);
INSERT INTO Years VALUES (2010);
INSERT INTO Years VALUES (2011);
INSERT INTO Years VALUES (2012);
INSERT INTO CostsPerYear VALUES (1, 2010, 100000);
INSERT INTO CostsPerYear VALUES (2, 2011, 50000);
INSERT INTO CostsPerYear VALUES (5, 2011, 20000);
INSERT INTO CostsPerYear VALUES (6, 2012, 22000);
INSERT INTO CostsPerYear VALUES (7, 2012, 13000);
INSERT INTO CostsPerYear VALUES (2, 2012, 76000);
鉴于上面的结构和示例数据,这就是事情的样子:
| NAME | YEAR | VALUE |
-------------------------------
| Total Costs | 2010 | 100000 |
| R&D | 2010 | 0 |
| IT | 2010 | 0 |
| Software | 2010 | 0 |
| Hardware | 2010 | 0 |
| HR | 2010 | 0 |
| Total Costs | 2011 | 70000 |
| R&D | 2011 | 50000 |
| IT | 2011 | 20000 |
| Software | 2011 | 0 |
| Hardware | 2011 | 0 |
| HR | 2011 | 0 |
| Total Costs | 2012 | 111000 |
| R&D | 2012 | 76000 |
| IT | 2012 | 35000 |
| Software | 2012 | 22000 |
| Hardware | 2012 | 13000 |
| HR | 2012 | 0 |