SQL Server : Mapping table design












0















I have 2 tables, Table A and Table B which have the following structure:



Table A:



and



Table B



There is a 1-1 relationship between tables identified by Product_group in Table A and <campaign_month,campaign> in Table B.



For example, ‘ 1808 Product - Group 3a’ = <’201808’,‘group_3a’>.



I am trying to create a mapping table using Tables A and B which should contain 3 columns, namely, <Product_group, campaign_month, campaign>.



Since I am not able to join the two tables, I am looking for other ways of creating the mapping table.



Can someone point me in the right direction?










share|improve this question

























  • Is the format of Product_group consistent? If so then you can create a CTE that reformats the columns of Table A to match the formats of Table B. Then you can join the CTE to Table B and obtain the result.

    – John Tamburo
    Nov 20 '18 at 4:33











  • In your example there are two <201808’,‘group_3a> in table_b. Is there a preference to use the first record<111,201808,'group_3a'> and not the second one<115,'201808','group_3a'> for mapping with '1808 Product - Group 3a'

    – George Joseph
    Nov 20 '18 at 5:03











  • @GeorgeJoseph Every Product_group will have one entry in the mapping table, so both record<111,201808,'group_3a'> and <115,'201808','group_3a'> should be mapped to the same product_group from table A which is '1808 Product - Group 3a'.

    – Sumedha Nagpal
    Nov 20 '18 at 5:43











  • @JohnTamburo Yes, as far as I know product group is consistent. However, I do see some entries in Table A like ‘ 1809 Product - Group 1c - Test1’ for which I do not have a corresponding group_1c-Test1 as campaign in Table B.

    – Sumedha Nagpal
    Nov 20 '18 at 5:46


















0















I have 2 tables, Table A and Table B which have the following structure:



Table A:



and



Table B



There is a 1-1 relationship between tables identified by Product_group in Table A and <campaign_month,campaign> in Table B.



For example, ‘ 1808 Product - Group 3a’ = <’201808’,‘group_3a’>.



I am trying to create a mapping table using Tables A and B which should contain 3 columns, namely, <Product_group, campaign_month, campaign>.



Since I am not able to join the two tables, I am looking for other ways of creating the mapping table.



Can someone point me in the right direction?










share|improve this question

























  • Is the format of Product_group consistent? If so then you can create a CTE that reformats the columns of Table A to match the formats of Table B. Then you can join the CTE to Table B and obtain the result.

    – John Tamburo
    Nov 20 '18 at 4:33











  • In your example there are two <201808’,‘group_3a> in table_b. Is there a preference to use the first record<111,201808,'group_3a'> and not the second one<115,'201808','group_3a'> for mapping with '1808 Product - Group 3a'

    – George Joseph
    Nov 20 '18 at 5:03











  • @GeorgeJoseph Every Product_group will have one entry in the mapping table, so both record<111,201808,'group_3a'> and <115,'201808','group_3a'> should be mapped to the same product_group from table A which is '1808 Product - Group 3a'.

    – Sumedha Nagpal
    Nov 20 '18 at 5:43











  • @JohnTamburo Yes, as far as I know product group is consistent. However, I do see some entries in Table A like ‘ 1809 Product - Group 1c - Test1’ for which I do not have a corresponding group_1c-Test1 as campaign in Table B.

    – Sumedha Nagpal
    Nov 20 '18 at 5:46
















0












0








0








I have 2 tables, Table A and Table B which have the following structure:



Table A:



and



Table B



There is a 1-1 relationship between tables identified by Product_group in Table A and <campaign_month,campaign> in Table B.



For example, ‘ 1808 Product - Group 3a’ = <’201808’,‘group_3a’>.



I am trying to create a mapping table using Tables A and B which should contain 3 columns, namely, <Product_group, campaign_month, campaign>.



Since I am not able to join the two tables, I am looking for other ways of creating the mapping table.



Can someone point me in the right direction?










share|improve this question
















I have 2 tables, Table A and Table B which have the following structure:



Table A:



and



Table B



There is a 1-1 relationship between tables identified by Product_group in Table A and <campaign_month,campaign> in Table B.



For example, ‘ 1808 Product - Group 3a’ = <’201808’,‘group_3a’>.



I am trying to create a mapping table using Tables A and B which should contain 3 columns, namely, <Product_group, campaign_month, campaign>.



Since I am not able to join the two tables, I am looking for other ways of creating the mapping table.



Can someone point me in the right direction?







sql sql-server database-design mapping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 5:37









marc_s

573k12811071254




573k12811071254










asked Nov 20 '18 at 3:56









Sumedha NagpalSumedha Nagpal

226




226













  • Is the format of Product_group consistent? If so then you can create a CTE that reformats the columns of Table A to match the formats of Table B. Then you can join the CTE to Table B and obtain the result.

    – John Tamburo
    Nov 20 '18 at 4:33











  • In your example there are two <201808’,‘group_3a> in table_b. Is there a preference to use the first record<111,201808,'group_3a'> and not the second one<115,'201808','group_3a'> for mapping with '1808 Product - Group 3a'

    – George Joseph
    Nov 20 '18 at 5:03











  • @GeorgeJoseph Every Product_group will have one entry in the mapping table, so both record<111,201808,'group_3a'> and <115,'201808','group_3a'> should be mapped to the same product_group from table A which is '1808 Product - Group 3a'.

    – Sumedha Nagpal
    Nov 20 '18 at 5:43











  • @JohnTamburo Yes, as far as I know product group is consistent. However, I do see some entries in Table A like ‘ 1809 Product - Group 1c - Test1’ for which I do not have a corresponding group_1c-Test1 as campaign in Table B.

    – Sumedha Nagpal
    Nov 20 '18 at 5:46





















  • Is the format of Product_group consistent? If so then you can create a CTE that reformats the columns of Table A to match the formats of Table B. Then you can join the CTE to Table B and obtain the result.

    – John Tamburo
    Nov 20 '18 at 4:33











  • In your example there are two <201808’,‘group_3a> in table_b. Is there a preference to use the first record<111,201808,'group_3a'> and not the second one<115,'201808','group_3a'> for mapping with '1808 Product - Group 3a'

    – George Joseph
    Nov 20 '18 at 5:03











  • @GeorgeJoseph Every Product_group will have one entry in the mapping table, so both record<111,201808,'group_3a'> and <115,'201808','group_3a'> should be mapped to the same product_group from table A which is '1808 Product - Group 3a'.

    – Sumedha Nagpal
    Nov 20 '18 at 5:43











  • @JohnTamburo Yes, as far as I know product group is consistent. However, I do see some entries in Table A like ‘ 1809 Product - Group 1c - Test1’ for which I do not have a corresponding group_1c-Test1 as campaign in Table B.

    – Sumedha Nagpal
    Nov 20 '18 at 5:46



















Is the format of Product_group consistent? If so then you can create a CTE that reformats the columns of Table A to match the formats of Table B. Then you can join the CTE to Table B and obtain the result.

– John Tamburo
Nov 20 '18 at 4:33





Is the format of Product_group consistent? If so then you can create a CTE that reformats the columns of Table A to match the formats of Table B. Then you can join the CTE to Table B and obtain the result.

– John Tamburo
Nov 20 '18 at 4:33













In your example there are two <201808’,‘group_3a> in table_b. Is there a preference to use the first record<111,201808,'group_3a'> and not the second one<115,'201808','group_3a'> for mapping with '1808 Product - Group 3a'

– George Joseph
Nov 20 '18 at 5:03





In your example there are two <201808’,‘group_3a> in table_b. Is there a preference to use the first record<111,201808,'group_3a'> and not the second one<115,'201808','group_3a'> for mapping with '1808 Product - Group 3a'

– George Joseph
Nov 20 '18 at 5:03













@GeorgeJoseph Every Product_group will have one entry in the mapping table, so both record<111,201808,'group_3a'> and <115,'201808','group_3a'> should be mapped to the same product_group from table A which is '1808 Product - Group 3a'.

– Sumedha Nagpal
Nov 20 '18 at 5:43





@GeorgeJoseph Every Product_group will have one entry in the mapping table, so both record<111,201808,'group_3a'> and <115,'201808','group_3a'> should be mapped to the same product_group from table A which is '1808 Product - Group 3a'.

– Sumedha Nagpal
Nov 20 '18 at 5:43













@JohnTamburo Yes, as far as I know product group is consistent. However, I do see some entries in Table A like ‘ 1809 Product - Group 1c - Test1’ for which I do not have a corresponding group_1c-Test1 as campaign in Table B.

– Sumedha Nagpal
Nov 20 '18 at 5:46







@JohnTamburo Yes, as far as I know product group is consistent. However, I do see some entries in Table A like ‘ 1809 Product - Group 1c - Test1’ for which I do not have a corresponding group_1c-Test1 as campaign in Table B.

– Sumedha Nagpal
Nov 20 '18 at 5:46














1 Answer
1






active

oldest

votes


















0














Consider the following, I am joining the two tables on the basis of whether the string in campaign column(without the _ ) is contained in product_group.



create table tablea(product_group varchar(50));



create table tableb(id_c int,campaign_month int, campaign varchar(50));

insert into tablea values('1808 Product - Group 3a')
insert into tablea values('1808 Product - Group 3b')
insert into tablea values('1808 Product - Group 1c - Test1')
insert into tablea values('1808 Product - Group 3s -1')
insert into tablea values('1808 Product - Group 4d')

insert into tablea values('1808 Product - Group 3a')
insert into tablea values('1808 Product - Group 3b')
insert into tablea values('1808 Product - Group 1c - Test1')
insert into tablea values('1808 Product - Group 3s -1')
insert into tablea values('1808 Product - Group 4d')

select a.product_group, b.id_c,b.campaign_month,b.campaign
from tablea a
left join tableb b
on replace(a.product_group,' -',' ') like '%'+replace(b.campaign,'_',' ')+'%'


Demo link
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=b1c19bc020e3a8a313ce78dd387f7c43






share|improve this answer





















  • 1





    Hey @george this is actually helpful. Though, the issue is, that the date is not being used to match the correct product_group. Also, I tried matching a campaign named group_3s_1 to product_group which would be 1809 Product - Group 3s -1. Do you have any suggestions on this?

    – Sumedha Nagpal
    Nov 20 '18 at 6:36











  • The idea would be to compare by replace <space><hyphen> with a <space> in the column "product_group" and perform the comparison. I have updated the dbfiddle link and answer.I didnt follow the comment on "date not being used to match"?.

    – George Joseph
    Nov 20 '18 at 7:09













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%2f53385998%2fsql-server-mapping-table-design%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









0














Consider the following, I am joining the two tables on the basis of whether the string in campaign column(without the _ ) is contained in product_group.



create table tablea(product_group varchar(50));



create table tableb(id_c int,campaign_month int, campaign varchar(50));

insert into tablea values('1808 Product - Group 3a')
insert into tablea values('1808 Product - Group 3b')
insert into tablea values('1808 Product - Group 1c - Test1')
insert into tablea values('1808 Product - Group 3s -1')
insert into tablea values('1808 Product - Group 4d')

insert into tablea values('1808 Product - Group 3a')
insert into tablea values('1808 Product - Group 3b')
insert into tablea values('1808 Product - Group 1c - Test1')
insert into tablea values('1808 Product - Group 3s -1')
insert into tablea values('1808 Product - Group 4d')

select a.product_group, b.id_c,b.campaign_month,b.campaign
from tablea a
left join tableb b
on replace(a.product_group,' -',' ') like '%'+replace(b.campaign,'_',' ')+'%'


Demo link
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=b1c19bc020e3a8a313ce78dd387f7c43






share|improve this answer





















  • 1





    Hey @george this is actually helpful. Though, the issue is, that the date is not being used to match the correct product_group. Also, I tried matching a campaign named group_3s_1 to product_group which would be 1809 Product - Group 3s -1. Do you have any suggestions on this?

    – Sumedha Nagpal
    Nov 20 '18 at 6:36











  • The idea would be to compare by replace <space><hyphen> with a <space> in the column "product_group" and perform the comparison. I have updated the dbfiddle link and answer.I didnt follow the comment on "date not being used to match"?.

    – George Joseph
    Nov 20 '18 at 7:09


















0














Consider the following, I am joining the two tables on the basis of whether the string in campaign column(without the _ ) is contained in product_group.



create table tablea(product_group varchar(50));



create table tableb(id_c int,campaign_month int, campaign varchar(50));

insert into tablea values('1808 Product - Group 3a')
insert into tablea values('1808 Product - Group 3b')
insert into tablea values('1808 Product - Group 1c - Test1')
insert into tablea values('1808 Product - Group 3s -1')
insert into tablea values('1808 Product - Group 4d')

insert into tablea values('1808 Product - Group 3a')
insert into tablea values('1808 Product - Group 3b')
insert into tablea values('1808 Product - Group 1c - Test1')
insert into tablea values('1808 Product - Group 3s -1')
insert into tablea values('1808 Product - Group 4d')

select a.product_group, b.id_c,b.campaign_month,b.campaign
from tablea a
left join tableb b
on replace(a.product_group,' -',' ') like '%'+replace(b.campaign,'_',' ')+'%'


Demo link
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=b1c19bc020e3a8a313ce78dd387f7c43






share|improve this answer





















  • 1





    Hey @george this is actually helpful. Though, the issue is, that the date is not being used to match the correct product_group. Also, I tried matching a campaign named group_3s_1 to product_group which would be 1809 Product - Group 3s -1. Do you have any suggestions on this?

    – Sumedha Nagpal
    Nov 20 '18 at 6:36











  • The idea would be to compare by replace <space><hyphen> with a <space> in the column "product_group" and perform the comparison. I have updated the dbfiddle link and answer.I didnt follow the comment on "date not being used to match"?.

    – George Joseph
    Nov 20 '18 at 7:09
















0












0








0







Consider the following, I am joining the two tables on the basis of whether the string in campaign column(without the _ ) is contained in product_group.



create table tablea(product_group varchar(50));



create table tableb(id_c int,campaign_month int, campaign varchar(50));

insert into tablea values('1808 Product - Group 3a')
insert into tablea values('1808 Product - Group 3b')
insert into tablea values('1808 Product - Group 1c - Test1')
insert into tablea values('1808 Product - Group 3s -1')
insert into tablea values('1808 Product - Group 4d')

insert into tablea values('1808 Product - Group 3a')
insert into tablea values('1808 Product - Group 3b')
insert into tablea values('1808 Product - Group 1c - Test1')
insert into tablea values('1808 Product - Group 3s -1')
insert into tablea values('1808 Product - Group 4d')

select a.product_group, b.id_c,b.campaign_month,b.campaign
from tablea a
left join tableb b
on replace(a.product_group,' -',' ') like '%'+replace(b.campaign,'_',' ')+'%'


Demo link
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=b1c19bc020e3a8a313ce78dd387f7c43






share|improve this answer















Consider the following, I am joining the two tables on the basis of whether the string in campaign column(without the _ ) is contained in product_group.



create table tablea(product_group varchar(50));



create table tableb(id_c int,campaign_month int, campaign varchar(50));

insert into tablea values('1808 Product - Group 3a')
insert into tablea values('1808 Product - Group 3b')
insert into tablea values('1808 Product - Group 1c - Test1')
insert into tablea values('1808 Product - Group 3s -1')
insert into tablea values('1808 Product - Group 4d')

insert into tablea values('1808 Product - Group 3a')
insert into tablea values('1808 Product - Group 3b')
insert into tablea values('1808 Product - Group 1c - Test1')
insert into tablea values('1808 Product - Group 3s -1')
insert into tablea values('1808 Product - Group 4d')

select a.product_group, b.id_c,b.campaign_month,b.campaign
from tablea a
left join tableb b
on replace(a.product_group,' -',' ') like '%'+replace(b.campaign,'_',' ')+'%'


Demo link
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=b1c19bc020e3a8a313ce78dd387f7c43







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 7:08

























answered Nov 20 '18 at 5:59









George JosephGeorge Joseph

1,41749




1,41749








  • 1





    Hey @george this is actually helpful. Though, the issue is, that the date is not being used to match the correct product_group. Also, I tried matching a campaign named group_3s_1 to product_group which would be 1809 Product - Group 3s -1. Do you have any suggestions on this?

    – Sumedha Nagpal
    Nov 20 '18 at 6:36











  • The idea would be to compare by replace <space><hyphen> with a <space> in the column "product_group" and perform the comparison. I have updated the dbfiddle link and answer.I didnt follow the comment on "date not being used to match"?.

    – George Joseph
    Nov 20 '18 at 7:09
















  • 1





    Hey @george this is actually helpful. Though, the issue is, that the date is not being used to match the correct product_group. Also, I tried matching a campaign named group_3s_1 to product_group which would be 1809 Product - Group 3s -1. Do you have any suggestions on this?

    – Sumedha Nagpal
    Nov 20 '18 at 6:36











  • The idea would be to compare by replace <space><hyphen> with a <space> in the column "product_group" and perform the comparison. I have updated the dbfiddle link and answer.I didnt follow the comment on "date not being used to match"?.

    – George Joseph
    Nov 20 '18 at 7:09










1




1





Hey @george this is actually helpful. Though, the issue is, that the date is not being used to match the correct product_group. Also, I tried matching a campaign named group_3s_1 to product_group which would be 1809 Product - Group 3s -1. Do you have any suggestions on this?

– Sumedha Nagpal
Nov 20 '18 at 6:36





Hey @george this is actually helpful. Though, the issue is, that the date is not being used to match the correct product_group. Also, I tried matching a campaign named group_3s_1 to product_group which would be 1809 Product - Group 3s -1. Do you have any suggestions on this?

– Sumedha Nagpal
Nov 20 '18 at 6:36













The idea would be to compare by replace <space><hyphen> with a <space> in the column "product_group" and perform the comparison. I have updated the dbfiddle link and answer.I didnt follow the comment on "date not being used to match"?.

– George Joseph
Nov 20 '18 at 7:09







The idea would be to compare by replace <space><hyphen> with a <space> in the column "product_group" and perform the comparison. I have updated the dbfiddle link and answer.I didnt follow the comment on "date not being used to match"?.

– George Joseph
Nov 20 '18 at 7:09




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385998%2fsql-server-mapping-table-design%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

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

WPF add header to Image with URL pettitions [duplicate]