我有这样的表结构:
CREATE TABLE Item
(
ItemKey BIGINT,
DisplayName VARCHAR(100)
)
INSERT INTO Item VALUES (136172,'Computer')
INSERT INTO Item VALUES (156737,'Car')
CREATE TABLE ItemTypeFactvw
(
ItemKey BIGINT,
TypeDimKey BIGINT
)
INSERT INTO ItemTypeFactvw VALUES (136172,483)
INSERT INTO ItemTypeFactvw VALUES (136172,1353)
INSERT INTO ItemTypeFactvw VALUES (156737,1000)
CREATE TABLE ItemTypeDimvw
(
Id BIGINT,
BaseId BIGINT,
TypeDimKey BIGINT,
DisplayName VARCHAR(100)
)
INSERT INTO ItemTypeDimvw VALUES (1,80,483,'IT Gadgets')
INSERT INTO ItemTypeDimvw VALUES (3,1,1353,'Electronics')
INSERT INTO ItemTypeDimvw VALUES (2,44,1000,'Vehicle')
我需要产生这样的结果(期望的输出):
| ITEMKEY | DISPLAYNAME | TYPEDIMKEY | ID | BASEID | ITEM BASE TYPE |
---------------------------------------------------------------------
| 136172 | Computer | 483 | 1 | 80 | Electronics |
| 156737 | Car | 1000 | 2 | 44 | Vehicle |
这是我的查询:
SELECT i.ItemKey,
i.DisplayName,
it.TypeDimKey,
itd.Id,
itd.BaseId,
itd.DisplayName 'Item Base Type'
FROM Item i
INNER JOIN ItemTypeFactvw it
on i.ItemKey = it.ItemKey
INNER JOIN ItemTypeDimvw itd
ON itd.TypeDimKey = it.TypeDimKey
上面的查询不会产生所需的输出,因为它还会从ItemTypeDimvw
where DisplayName
is带来额外的记录"IT Gadgets"
。
将其视为类层次结构。IT 小工具继承自电子产品。所以我需要在这种情况下显示基本类型,它是用于 IT 小工具的电子产品