Hi I have 4 dimensions and I am trying to insert multiple data from the dimensions into the fact table.
I have a Gunsales table that contains the majority of the data for the fact table and then primary keys from other tables that I would like to join on. homicide_id from the Homicide table, article_id from the BBC table, incident_id from the Gun_violence table and shooting_id from the School_shooting table. The rest of the data if from the Gunsales table.
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)
So logically it is a full join of the Gun_sales table and an inner join for the ID keys in the other table but I am struggling to get this to work.
adding in the DDL for all the tables:
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:
As mentioned above I am trying to create a new fact table that has all columns from the Gun_sales table and the primary keys from the other tables.
Thanks in advance