在SQL Server中为事实表复杂地联接多个表

时间:2018-11-17 15:17:14

标签: sql sql-server tsql

您好,我有4个维度,我正在尝试将多个数据从维度插入事实表。

我有一个Gunsales表,其中包含事实表的大部分数据,然后包含我要加入的其他表的主键。凶杀案表中的homicide_id,BBC表中的article_id,Gun_violence表中的event_id和School_shooting表中的Shooting_id。其余数据(如果来自Gunsales表)。

INSERT INTO [dbo].[FactGunSales]
        (
        sale_id,
        sale_date,
        sale_state,
        permit,
        hand_gun,
        long_gun,
        other_gun,
        multiple_gun,
        incident_id,
        homicide_id,
        article_id,
        shooitng_id)

从逻辑上讲,它是Gun_sales表的完全联接,而另一个表中的ID密钥是内部联接,但是我正在努力使其正常工作。

为所有表添加DDL:

    USE [Gun Violence]
GO

DROP TABLE IF EXISTS Gun_Violence

CREATE TABLE Gun_Violence(
        incident_id int PRIMARY KEY, 
        incident_date date,
        state_name varchar (50),
        city_name varchar(50),
        death int ,
        injury int ,
        )

DROP TABLE IF EXISTS Gun_Sales
CREATE TABLE  Gun_Sales(
            sale_id int  PRIMARY KEY,
            sale_date date,
            sale_state varchar(50),
            permit int,
            hand_gun int ,
            long_gun int ,
            other_gun int ,
            multiple_gun int ,
            )

DROP TABLE IF EXISTS School_Shootings
CREATE TABLE School_Shootings(
            shooting_id int  PRIMARY KEY,
            shooting_date date,
            shooting_state varchar(50),
            shooting_city varchar(50),
            shooting_death int ,
            shooting_injury int,
            )

DROP TABLE IF EXISTS Homicide
CREATE TABLE Homicide(
            Homicide_id int PRIMARY KEY,
            homicide_state varchar(50),
            homicide_victims int,
            homeicide_date date             
)

DROP TABLE IF EXISTS BBC
CREATE TABLE BBC(
            ariticle_id int PRIMARY KEY,
            article_date date,
            article_link varchar(1000),
            article_headline varchar(1000),
            article_count int,
            article_keyword varchar(100),
            article_month varchar(10),
            article_year int,
            article_state varchar(50)
)

输出:output for gun 如上所述,我试图创建一个新的事实表,其中包含Gun_sales表中的所有列以及其他表中的主键。

预先感谢

1 个答案:

答案 0 :(得分:2)

@RitaMurran不是吗?你想要这样的东西吗?

nextButt = driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/section/div/div[1]/div/article[1]/div[2]/section[1]/span[1]/button/span""")
    nextButt.click()
nextButt = driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/section/div/div[1]/div/article[1]/div[2]/section[1]/span[1]/button/span""")
    nextButt.click()
nextButt = driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/section/div/div[1]/div/article[1]/div[2]/section[1]/span[1]/button/span""")
    nextButt.click()

编辑:

@RitaMurran,我回答了您的问题:

  

“是否有一种方法在连接发生时仅具有不同的值?”

以示例:

USE [Gun Violence]

go

INSERT INTO [dbo].[FACTGUNSALES]
            ([sale_id],
             [sale_date],
             [sale_state],
             [hand_gun],
             [other_gun],
             [multiple_gun],
             [incident_id],
             [homicide_id],
             [article_id],
             [shooitng_id])
SELECT [sale_id],
       [sale_date],
       [sale_state],
       [hand_gun],
       [other_gun],
       [multiple_gun],
       [dbo].[GUN_VIOLENCE].incident_id,
       [dbo].[HOMICIDE].homicide_id,
       [dbo].[BBC].ariticle_id,
       [dbo].[SCHOOL_SHOOTINGS].shooting_id
FROM   [dbo].[GUN_SALES]
       LEFT JOIN [dbo].[GUN_VIOLENCE]
         ON ( [dbo].[GUN_VIOLENCE].incident_date = [dbo].[GUN_SALES].[sale_date] AND
              [dbo].[GUN_VIOLENCE].state_name = [dbo].[GUN_SALES].[sale_state] )
       LEFT JOIN [dbo].[SCHOOL_SHOOTINGS]
         ON ( [dbo].[SCHOOL_SHOOTINGS].shooting_date = [dbo].[GUN_SALES].[sale_date]     AND
              [dbo].[SCHOOL_SHOOTINGS].shooting_state = [dbo].[GUN_SALES].    [sale_state] )
       LEFT JOIN [dbo].[HOMICIDE]
         ON ( [dbo].[HOMICIDE].homeicide_date = [dbo].[GUN_SALES].[sale_date] AND
              [dbo].[HOMICIDE].homicide_state = [dbo].[GUN_SALES].[sale_state] )
       LEFT JOIN [dbo].[BBC]
         ON ( [dbo].[BBC].article_date = [dbo].[GUN_SALES].[sale_date] AND
              [dbo].[BBC].[article_state]  = [dbo].[Gun_Sales].[sale_state] ) 

输出:

DECLARE @tbl_A TABLE (ID_A INT, col_A varChar(10))
INSERT  @tbl_A (ID_A,col_A)
SELECT 1 AS ID_A,'aaa' AS  col_A UNION ALL
SELECT 2        ,'bbb'           UNION ALL
SELECT 3        ,'ccc'           UNION ALL
SELECT 4        ,'ddd'           UNION ALL
SELECT 5        ,'eee'

DECLARE @tbl_B TABLE (ID_B INT,ID_A_FK INT, col_B varChar(10))
INSERT  @tbl_B (ID_B,ID_A_FK,col_B)
SELECT 1 AS ID_B,1 AS ID_A_FK,NULL AS  col_B UNION ALL
SELECT 2        ,1           ,NULL           UNION ALL
SELECT 3        ,3           ,NULL           UNION ALL
SELECT 4        ,3           ,NULL           UNION ALL
SELECT 5        ,3           ,NULL

SELECT * 
FROM @tbl_A tbA
LEFT JOIN 
    ( select ID_A_FK,col_B FROM @tbl_B GROUP BY ID_A_FK,col_B )  tbB ON     tbA.ID_A = tbB.ID_A_FK  

如果col_B有值,则输出如下:

ID_A    col_A   ID_A_FK col_B
1       aaa     1       NULL
2       bbb     NULL    NULL
3       ccc     3       NULL
4       ddd     NULL    NULL
5       eee     NULL    NULL  

输出:

INSERT  @tbl_B (ID_B,ID_A_FK,col_B)
SELECT 1 AS ID_B,1 AS ID_A_FK,'a1' AS  col_B UNION ALL
SELECT 2        ,1           ,'a2'           UNION ALL
SELECT 3        ,3           ,'c1'           UNION ALL
SELECT 4        ,3           ,'c2'           UNION ALL
SELECT 5        ,3           ,'c3'