在关系模型中表示树和层次结构的方法很少——搜索 SO 会返回很多答案。
该模型的主要思想是用闭包表和层次级别来表示层次结构。所以,Level表有(1, Channel) , (2, Zone) , (3, City) , (4, Store)
.
闭包表暴露了每个节点和节点的所有后代。重要的是要注意每个节点也是它自己的后代。
第一个 CTE 查询 (q_00) 选择一个节点的价格并将其分配给所有后代。LevelDiff列计算从后代节点开始有多少级别是价格指定节点。
因为可以在多个级别上指定价格,所以最终查询选择在具有最小LevelDiff的级别上指定的商店价格。
语法是PostgeSQL
,但也应该很容易转换为其他语法。
with q_00 as (
select
a.ProductID
, c.LocationID
, c.LocationLevel
, a.Price
, t.DescendantLocationID
, t.DescendantLevel
, (t.DescendantLevel - c.LocationLevel ) as LevelDiff
from ProductPrice as a
join Product as b on b.ProductId = a.ProductID
join Location as c on c.LocationID = a.LocationID
join TreeClosure as t on t.LocationID = c.LocationID
)
select
a.ProductID
, DescendantLocationID
, Price
from q_00 as a
join Level as w on w.LevelNo = a.DescendantLevel
where w.LevelName = 'Store'
and a.Leveldiff = (select min(LevelDiff)
from q_00 as x
where x.DescendantLocationID = a.DescendantLocationID
and x.ProductID = a.ProductID ) ;
总而言之,这是一个测试结果,定价定义为:
Channel=1, Product=1, Price = 11.0
Channel=1, City=111, Product=1, Price = 11.5
Channel=1, City=111, Store =1112, Product=1, Price = 12.0
查询返回(见下面的测试数据)
ProductID | DescendantLocationID | PriceID
-----------------------------------------
1 1231 11.00
1 1232 11.00
1 1111 11.50
1 1112 12.00
这是 DDL (PosgreSQL)
CREATE TABLE Level (
LevelNo integer NOT NULL ,
LevelName varchar(20) NOT NULL
);
ALTER TABLE Level ADD CONSTRAINT XPKLevel PRIMARY KEY (LevelNo) ;
CREATE TABLE Location (
LocationID integer NOT NULL ,
LocationLevel integer NOT NULL
);
ALTER TABLE Location ADD CONSTRAINT XPKLocation PRIMARY KEY (LocationID);
ALTER TABLE Location ADD CONSTRAINT XAK1Location UNIQUE (LocationID, LocationLevel) ;
CREATE TABLE Product (
ProductID integer NOT NULL
);
ALTER TABLE Product ADD CONSTRAINT XPKProduct PRIMARY KEY (ProductID);
CREATE TABLE ProductPrice (
ProductID integer NOT NULL ,
LocationID integer NOT NULL ,
Price decimal(19,2) NOT NULL
);
ALTER TABLE ProductPrice ADD CONSTRAINT XPKProductPrice PRIMARY KEY (ProductID, LocationID);
CREATE TABLE ProductSupplier (
ProductID integer NOT NULL ,
LocationID integer NOT NULL ,
SupplierID integer NOT NULL
);
ALTER TABLE ProductSupplier ADD CONSTRAINT XPKProductSupplier PRIMARY KEY (ProductID, LocationID);
CREATE TABLE Supplier (
SupplierID integer NOT NULL
);
ALTER TABLE Supplier ADD CONSTRAINT XPKSupplier PRIMARY KEY (SupplierID) ;
CREATE TABLE TreeClosure (
LocationID integer NOT NULL ,
DescendantLocationID integer NOT NULL ,
DescendantLevel integer NOT NULL
);
ALTER TABLE TreeClosure ADD CONSTRAINT XPKTreeClosure PRIMARY KEY (LocationID, DescendantLocationID);
ALTER TABLE Location
ADD CONSTRAINT FK1_Location FOREIGN KEY (LocationLevel) REFERENCES Level(LevelNo);
ALTER TABLE ProductPrice
ADD CONSTRAINT FK1_ProductPrice FOREIGN KEY (ProductID) REFERENCES Product(ProductID);
ALTER TABLE ProductPrice
ADD CONSTRAINT FK2_ProductPrice FOREIGN KEY (LocationID) REFERENCES Location(LocationID);
ALTER TABLE ProductSupplier
ADD CONSTRAINT FK1_PrdSup FOREIGN KEY (ProductID) REFERENCES Product(ProductID);
ALTER TABLE ProductSupplier
ADD CONSTRAINT FK2_PrdSup FOREIGN KEY (SupplierID) REFERENCES Supplier(SupplierID);
ALTER TABLE ProductSupplier
ADD CONSTRAINT FK3_PrdSup FOREIGN KEY (LocationID) REFERENCES Location(LocationID);
ALTER TABLE TreeClosure
ADD CONSTRAINT FK1_TC FOREIGN KEY (LocationID) REFERENCES Location(LocationID);
ALTER TABLE TreeClosure
ADD CONSTRAINT FK2_TC FOREIGN KEY (DescendantLocationID,DescendantLevel) REFERENCES Location(LocationID,LocationLevel);
以及一些要测试的数据
insert into Level (LevelNo, LevelName)
values
(1, 'Channel')
, (2, 'Zone')
, (3, 'City')
, (4, 'Store')
;
insert into Product (ProductID)
values (1) , (2) , (3)
;
-- Locations
insert into Location (LocationID, LocationLevel)
values
(1, 1)
, (11, 2)
, (111, 3)
, (1111, 4)
, (1112, 4)
, (12, 2)
, (123, 3)
, (1231, 4)
, (1232, 4)
;
-- Tree closure (hierarchy)
insert into TreeClosure (LocationID, DescendantLocationID, DescendantLevel)
values
(1 , 1 , 1)
, (1 , 11 , 2)
, (1 , 111 , 3)
, (1 , 1111, 4)
, (1 , 1112, 4)
, (11 , 11 , 2)
, (11 , 111 , 3)
, (11 , 1111, 4)
, (11 , 1112, 4)
, (111 , 111 , 3)
, (111 , 1111, 4)
, (111 , 1112, 4)
, (1111, 1111, 4)
, (1112, 1112, 4)
--
, (1 , 12 , 2)
, (1 , 123 , 3)
, (1 , 1231, 4)
, (1 , 1232, 4)
, (12 , 12 , 2)
, (12 , 123 , 3)
, (12 , 1231, 4)
, (12 , 1232, 4)
, (123 , 123, 3)
, (123 , 1231, 4)
, (123 , 1232, 4)
, (1231, 1231, 4)
, (1232, 1232, 4)
;
-- pricing
insert into ProductPrice (ProductID, LocationID, Price) values (1, 1 , 11.0);
insert into ProductPrice (ProductID, LocationID, Price) values (1, 111 , 11.5);
insert into ProductPrice (ProductID, LocationID, Price) values (1, 1112, 12.0);