加入基于树的 Table
Join a Tree Based Table
我有一个事实 table“定价”是这样的:
Serial
Main_Category
Group_Category
Sub_Category
Item
Location
Price
1
Clothes
Men
T Shirts
T Shirt 1
Store 1
100
2
Clothes
Men
T Shirts
T Shirt 1
All
25
3
Devices
TVs
Smart TVs
SmartTV 1
Store 1
50
4
Devices
TVs
Smart TVs
All
Store 1
75
您可以注意到,并非所有项目都明确定义,如果未提及该项目,它应该在“All”中可用。
"All" 可以在除 Price
之外的任何列中找到
另一个维度table“销售额”像这样:
Serial
Main_Category
Group_Category
Sub_Category
Item
Location
1
Clothes
Men
T Shirts
T Shirt 1
Store 1
2
Clothes
Men
T Shirts
T Shirt 1
Store 3
3
Devices
TVs
Smart TVs
SmartTV 3
Store 1
4
Devices
TVs
Smart TVs
SmartTV 1
Store 1
我想将 Pricing table 的价格添加到 Sales table 而不使用每个组合的连接
异常输出:
Serial
Main_Category
Group_Category
Sub_Category
Item
Location
Price
1
Clothes
Men
T Shirts
T Shirt 1
Store 1
100
2
Clothes
Men
T Shirts
T Shirt 1
Store 3
25
3
Devices
TVs
Smart TVs
SmartTV 3
Store 1
75
4
Devices
TVs
Smart TVs
SmartTV 1
Store 1
50
更新:
可以在不止一列中找到“全部”
你可以做到
select *
from (
select s.*, p.Price, row_number() over(partition by s.Main_Category, .., s.Location order by p.Serial) rn
from Sales s
join Pricing p on
(s.Main_Category = p.Main_Category or p.Main_Category ='All')
and ..
and (s.Location = p.Location or p.Location ='All')
) t
where rn = 1;
我有一个事实 table“定价”是这样的:
Serial | Main_Category | Group_Category | Sub_Category | Item | Location | Price |
---|---|---|---|---|---|---|
1 | Clothes | Men | T Shirts | T Shirt 1 | Store 1 | 100 |
2 | Clothes | Men | T Shirts | T Shirt 1 | All | 25 |
3 | Devices | TVs | Smart TVs | SmartTV 1 | Store 1 | 50 |
4 | Devices | TVs | Smart TVs | All | Store 1 | 75 |
您可以注意到,并非所有项目都明确定义,如果未提及该项目,它应该在“All”中可用。
"All" 可以在除 Price
之外的任何列中找到另一个维度table“销售额”像这样:
Serial | Main_Category | Group_Category | Sub_Category | Item | Location |
---|---|---|---|---|---|
1 | Clothes | Men | T Shirts | T Shirt 1 | Store 1 |
2 | Clothes | Men | T Shirts | T Shirt 1 | Store 3 |
3 | Devices | TVs | Smart TVs | SmartTV 3 | Store 1 |
4 | Devices | TVs | Smart TVs | SmartTV 1 | Store 1 |
我想将 Pricing table 的价格添加到 Sales table 而不使用每个组合的连接
异常输出:
Serial | Main_Category | Group_Category | Sub_Category | Item | Location | Price |
---|---|---|---|---|---|---|
1 | Clothes | Men | T Shirts | T Shirt 1 | Store 1 | 100 |
2 | Clothes | Men | T Shirts | T Shirt 1 | Store 3 | 25 |
3 | Devices | TVs | Smart TVs | SmartTV 3 | Store 1 | 75 |
4 | Devices | TVs | Smart TVs | SmartTV 1 | Store 1 | 50 |
更新: 可以在不止一列中找到“全部”
你可以做到
select *
from (
select s.*, p.Price, row_number() over(partition by s.Main_Category, .., s.Location order by p.Serial) rn
from Sales s
join Pricing p on
(s.Main_Category = p.Main_Category or p.Main_Category ='All')
and ..
and (s.Location = p.Location or p.Location ='All')
) t
where rn = 1;