使用多个 table 插入 table

Insert into a table using multiple tables

我刚开始使用 SQL 服务器。我有一项作业,但讲师没有向我们展示如何使用他希望用来完成作业的工具。

我正在尝试提出一个查询,将 3 维 tables 的主键插入到另一个 table 中,并尝试从源数据中插入数据。

源数据是 Google Play 商店中 10000 多个应用程序的数据集。

下面是我的table和我需要的

DimContentRating - 有 6 个内容评级

ContentRatingID(PK)   Content Rating
---------------       --------------
1                     Everyone
2                     Teen

DimCategory - 共有 34 个类别

CategoryID(PK)   Category
----------       --------
1                Education
2                Finance

DimInstalls - 安装范围很多

InstallID(PK)  Installs
----------     --------
1              10000+
2              100000+

googleplaystore - table 包含 10000 多条记录和原始数据

App       Category  Rating  Reviews  Installs  Price  Content_Rating  
---       --------  ------  ------   --------  -----  --------------
GMAT 
Question  Education  4.2     240      10000+    Free    Everyone
Bank

Ace Elite Finance    4.1     2898     100000+   Free    Everyone

我需要它看起来如何

AppFact - table 需要将上面的 table 从上面的 table 分解并使用来自外键的链接插入

AppFactID    Category  Rating  Reviews  Installs  Price  Content_Rating  
---------    --------  ------  ------   --------  -----  --------------
1              1        4.2     240       1        Free       1
2              2        4.1     2898       2       Free       1

我很抱歉没有我尝试编写的查询来让它工作,但我并没有看到太多关于 SQL 服务器的信息,所以我知道的最好的是一般查询。我所知道的是我需要使用以下以及可能的内部联接?

INSERT INTO AppFact(...) 
    SELECT ... 
    FROM ... 

我走在正确的轨道上吗?

假设您已经使用 PK AppFactID 创建了 table AppFact,您要查找的查询是:

INSERT INTO AppFact (Category, Rating, Reviews, Installs, Price, Content_Rating)
SELECT c.CategoryID, a.Rating, a.Reviews, i.Installs, a.Price, r.ContentRatingID
FROM googleplaystore a
    INNER JOIN DimContentRating r ON a.Content_Rating = r.Content_Rating
    INNER JOIN DimCategory c ON a.Category = c.Category
    INNER JOIN DimInstalls i ON a.Installs = i.Installs

你应该看看JOINs in SQL Server