Complex Joining multiple tables in SQL Server for a fact table












6














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: output for gun
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










share|improve this question




















  • 1




    Providing the DDL for your other tables, then some sample data (preferably as DML (INSERT) statements) and expected results would help here. We only have the definition for your destination, but no idea what your actual data looks like. Thanks.
    – Larnu
    Nov 17 '18 at 15:21










  • @Larnu I added the SQL on how i created the tables hope it can give a better idea of the context
    – Rita Murran
    Nov 17 '18 at 15:43








  • 1




    What about that sample data and expected results?
    – Larnu
    Nov 17 '18 at 16:30










  • There isn't any shared column between table Gun_Sales and Other Dimension!
    – Fateme Shafaei
    Nov 17 '18 at 16:41






  • 1




    @FatemeShafaei my idea was to join on date or state columns, maybe that wont work though?
    – Rita Murran
    Nov 17 '18 at 17:49
















6














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: output for gun
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










share|improve this question




















  • 1




    Providing the DDL for your other tables, then some sample data (preferably as DML (INSERT) statements) and expected results would help here. We only have the definition for your destination, but no idea what your actual data looks like. Thanks.
    – Larnu
    Nov 17 '18 at 15:21










  • @Larnu I added the SQL on how i created the tables hope it can give a better idea of the context
    – Rita Murran
    Nov 17 '18 at 15:43








  • 1




    What about that sample data and expected results?
    – Larnu
    Nov 17 '18 at 16:30










  • There isn't any shared column between table Gun_Sales and Other Dimension!
    – Fateme Shafaei
    Nov 17 '18 at 16:41






  • 1




    @FatemeShafaei my idea was to join on date or state columns, maybe that wont work though?
    – Rita Murran
    Nov 17 '18 at 17:49














6












6








6


1





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: output for gun
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










share|improve this question















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: output for gun
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







sql sql-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 11:00







Rita Murran

















asked Nov 17 '18 at 15:17









Rita MurranRita Murran

404




404








  • 1




    Providing the DDL for your other tables, then some sample data (preferably as DML (INSERT) statements) and expected results would help here. We only have the definition for your destination, but no idea what your actual data looks like. Thanks.
    – Larnu
    Nov 17 '18 at 15:21










  • @Larnu I added the SQL on how i created the tables hope it can give a better idea of the context
    – Rita Murran
    Nov 17 '18 at 15:43








  • 1




    What about that sample data and expected results?
    – Larnu
    Nov 17 '18 at 16:30










  • There isn't any shared column between table Gun_Sales and Other Dimension!
    – Fateme Shafaei
    Nov 17 '18 at 16:41






  • 1




    @FatemeShafaei my idea was to join on date or state columns, maybe that wont work though?
    – Rita Murran
    Nov 17 '18 at 17:49














  • 1




    Providing the DDL for your other tables, then some sample data (preferably as DML (INSERT) statements) and expected results would help here. We only have the definition for your destination, but no idea what your actual data looks like. Thanks.
    – Larnu
    Nov 17 '18 at 15:21










  • @Larnu I added the SQL on how i created the tables hope it can give a better idea of the context
    – Rita Murran
    Nov 17 '18 at 15:43








  • 1




    What about that sample data and expected results?
    – Larnu
    Nov 17 '18 at 16:30










  • There isn't any shared column between table Gun_Sales and Other Dimension!
    – Fateme Shafaei
    Nov 17 '18 at 16:41






  • 1




    @FatemeShafaei my idea was to join on date or state columns, maybe that wont work though?
    – Rita Murran
    Nov 17 '18 at 17:49








1




1




Providing the DDL for your other tables, then some sample data (preferably as DML (INSERT) statements) and expected results would help here. We only have the definition for your destination, but no idea what your actual data looks like. Thanks.
– Larnu
Nov 17 '18 at 15:21




Providing the DDL for your other tables, then some sample data (preferably as DML (INSERT) statements) and expected results would help here. We only have the definition for your destination, but no idea what your actual data looks like. Thanks.
– Larnu
Nov 17 '18 at 15:21












@Larnu I added the SQL on how i created the tables hope it can give a better idea of the context
– Rita Murran
Nov 17 '18 at 15:43






@Larnu I added the SQL on how i created the tables hope it can give a better idea of the context
– Rita Murran
Nov 17 '18 at 15:43






1




1




What about that sample data and expected results?
– Larnu
Nov 17 '18 at 16:30




What about that sample data and expected results?
– Larnu
Nov 17 '18 at 16:30












There isn't any shared column between table Gun_Sales and Other Dimension!
– Fateme Shafaei
Nov 17 '18 at 16:41




There isn't any shared column between table Gun_Sales and Other Dimension!
– Fateme Shafaei
Nov 17 '18 at 16:41




1




1




@FatemeShafaei my idea was to join on date or state columns, maybe that wont work though?
– Rita Murran
Nov 17 '18 at 17:49




@FatemeShafaei my idea was to join on date or state columns, maybe that wont work though?
– Rita Murran
Nov 17 '18 at 17:49












1 Answer
1






active

oldest

votes


















2














@RitaMurran Is't true? Do you want Something like this?



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] )




EDIT:



@RitaMurran, I answer your question:




"Is there a way to have only distinct values when the join happens?"




with the example:



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


The Output:



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


If col_B has value,The output is like this:



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'


Output:



ID_A    col_A   ID_A_FK col_B
1 aaa 1 a1
1 aaa 1 a2
2 bbb NULL NULL
3 ccc 3 c1
3 ccc 3 c2
3 ccc 3 c3
4 ddd NULL NULL
5 eee NULL NULL





share|improve this answer























  • Hey I tried this are entered created a [BBC].[article_state] for the final join. I am get a (0 rows affected) return
    – Rita Murran
    Nov 17 '18 at 20:22










  • @RitaMurran Maybe the equivalent of some of the dimensions fields aren't exist.Try Left join for this problem.
    – Fateme Shafaei
    Nov 18 '18 at 4:18










  • [dbo].[GUN_VIOLENCE] is the only join that seems to work. I have gone over the data for the other joins and tables and I cant see how the other ones would not work if that the gun violence one works.
    – Rita Murran
    Nov 18 '18 at 10:58












  • I got it to work finally with some modifications to the BBC join. thank you for the hel
    – Rita Murran
    Nov 18 '18 at 12:18










  • congratulation,I'm glad for you.
    – Fateme Shafaei
    Nov 18 '18 at 17:20











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352554%2fcomplex-joining-multiple-tables-in-sql-server-for-a-fact-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














@RitaMurran Is't true? Do you want Something like this?



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] )




EDIT:



@RitaMurran, I answer your question:




"Is there a way to have only distinct values when the join happens?"




with the example:



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


The Output:



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


If col_B has value,The output is like this:



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'


Output:



ID_A    col_A   ID_A_FK col_B
1 aaa 1 a1
1 aaa 1 a2
2 bbb NULL NULL
3 ccc 3 c1
3 ccc 3 c2
3 ccc 3 c3
4 ddd NULL NULL
5 eee NULL NULL





share|improve this answer























  • Hey I tried this are entered created a [BBC].[article_state] for the final join. I am get a (0 rows affected) return
    – Rita Murran
    Nov 17 '18 at 20:22










  • @RitaMurran Maybe the equivalent of some of the dimensions fields aren't exist.Try Left join for this problem.
    – Fateme Shafaei
    Nov 18 '18 at 4:18










  • [dbo].[GUN_VIOLENCE] is the only join that seems to work. I have gone over the data for the other joins and tables and I cant see how the other ones would not work if that the gun violence one works.
    – Rita Murran
    Nov 18 '18 at 10:58












  • I got it to work finally with some modifications to the BBC join. thank you for the hel
    – Rita Murran
    Nov 18 '18 at 12:18










  • congratulation,I'm glad for you.
    – Fateme Shafaei
    Nov 18 '18 at 17:20
















2














@RitaMurran Is't true? Do you want Something like this?



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] )




EDIT:



@RitaMurran, I answer your question:




"Is there a way to have only distinct values when the join happens?"




with the example:



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


The Output:



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


If col_B has value,The output is like this:



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'


Output:



ID_A    col_A   ID_A_FK col_B
1 aaa 1 a1
1 aaa 1 a2
2 bbb NULL NULL
3 ccc 3 c1
3 ccc 3 c2
3 ccc 3 c3
4 ddd NULL NULL
5 eee NULL NULL





share|improve this answer























  • Hey I tried this are entered created a [BBC].[article_state] for the final join. I am get a (0 rows affected) return
    – Rita Murran
    Nov 17 '18 at 20:22










  • @RitaMurran Maybe the equivalent of some of the dimensions fields aren't exist.Try Left join for this problem.
    – Fateme Shafaei
    Nov 18 '18 at 4:18










  • [dbo].[GUN_VIOLENCE] is the only join that seems to work. I have gone over the data for the other joins and tables and I cant see how the other ones would not work if that the gun violence one works.
    – Rita Murran
    Nov 18 '18 at 10:58












  • I got it to work finally with some modifications to the BBC join. thank you for the hel
    – Rita Murran
    Nov 18 '18 at 12:18










  • congratulation,I'm glad for you.
    – Fateme Shafaei
    Nov 18 '18 at 17:20














2












2








2






@RitaMurran Is't true? Do you want Something like this?



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] )




EDIT:



@RitaMurran, I answer your question:




"Is there a way to have only distinct values when the join happens?"




with the example:



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


The Output:



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


If col_B has value,The output is like this:



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'


Output:



ID_A    col_A   ID_A_FK col_B
1 aaa 1 a1
1 aaa 1 a2
2 bbb NULL NULL
3 ccc 3 c1
3 ccc 3 c2
3 ccc 3 c3
4 ddd NULL NULL
5 eee NULL NULL





share|improve this answer














@RitaMurran Is't true? Do you want Something like this?



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] )




EDIT:



@RitaMurran, I answer your question:




"Is there a way to have only distinct values when the join happens?"




with the example:



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


The Output:



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


If col_B has value,The output is like this:



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'


Output:



ID_A    col_A   ID_A_FK col_B
1 aaa 1 a1
1 aaa 1 a2
2 bbb NULL NULL
3 ccc 3 c1
3 ccc 3 c2
3 ccc 3 c3
4 ddd NULL NULL
5 eee NULL NULL






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 17:56

























answered Nov 17 '18 at 18:25









Fateme ShafaeiFateme Shafaei

964




964












  • Hey I tried this are entered created a [BBC].[article_state] for the final join. I am get a (0 rows affected) return
    – Rita Murran
    Nov 17 '18 at 20:22










  • @RitaMurran Maybe the equivalent of some of the dimensions fields aren't exist.Try Left join for this problem.
    – Fateme Shafaei
    Nov 18 '18 at 4:18










  • [dbo].[GUN_VIOLENCE] is the only join that seems to work. I have gone over the data for the other joins and tables and I cant see how the other ones would not work if that the gun violence one works.
    – Rita Murran
    Nov 18 '18 at 10:58












  • I got it to work finally with some modifications to the BBC join. thank you for the hel
    – Rita Murran
    Nov 18 '18 at 12:18










  • congratulation,I'm glad for you.
    – Fateme Shafaei
    Nov 18 '18 at 17:20


















  • Hey I tried this are entered created a [BBC].[article_state] for the final join. I am get a (0 rows affected) return
    – Rita Murran
    Nov 17 '18 at 20:22










  • @RitaMurran Maybe the equivalent of some of the dimensions fields aren't exist.Try Left join for this problem.
    – Fateme Shafaei
    Nov 18 '18 at 4:18










  • [dbo].[GUN_VIOLENCE] is the only join that seems to work. I have gone over the data for the other joins and tables and I cant see how the other ones would not work if that the gun violence one works.
    – Rita Murran
    Nov 18 '18 at 10:58












  • I got it to work finally with some modifications to the BBC join. thank you for the hel
    – Rita Murran
    Nov 18 '18 at 12:18










  • congratulation,I'm glad for you.
    – Fateme Shafaei
    Nov 18 '18 at 17:20
















Hey I tried this are entered created a [BBC].[article_state] for the final join. I am get a (0 rows affected) return
– Rita Murran
Nov 17 '18 at 20:22




Hey I tried this are entered created a [BBC].[article_state] for the final join. I am get a (0 rows affected) return
– Rita Murran
Nov 17 '18 at 20:22












@RitaMurran Maybe the equivalent of some of the dimensions fields aren't exist.Try Left join for this problem.
– Fateme Shafaei
Nov 18 '18 at 4:18




@RitaMurran Maybe the equivalent of some of the dimensions fields aren't exist.Try Left join for this problem.
– Fateme Shafaei
Nov 18 '18 at 4:18












[dbo].[GUN_VIOLENCE] is the only join that seems to work. I have gone over the data for the other joins and tables and I cant see how the other ones would not work if that the gun violence one works.
– Rita Murran
Nov 18 '18 at 10:58






[dbo].[GUN_VIOLENCE] is the only join that seems to work. I have gone over the data for the other joins and tables and I cant see how the other ones would not work if that the gun violence one works.
– Rita Murran
Nov 18 '18 at 10:58














I got it to work finally with some modifications to the BBC join. thank you for the hel
– Rita Murran
Nov 18 '18 at 12:18




I got it to work finally with some modifications to the BBC join. thank you for the hel
– Rita Murran
Nov 18 '18 at 12:18












congratulation,I'm glad for you.
– Fateme Shafaei
Nov 18 '18 at 17:20




congratulation,I'm glad for you.
– Fateme Shafaei
Nov 18 '18 at 17:20


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352554%2fcomplex-joining-multiple-tables-in-sql-server-for-a-fact-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?