TSQL: combination of two table












-1















I have 2 tables



A



EmpID IDcard  Date
xxx 111 2018-10-01
zzz 111 2018-05-01


B



Idcard AgrNumb Date
111 x46 2018-10-01
111 x35 2018-05-01
111 x20 2017-12-01
111 x42 2018-08-01


I would like to make table in which I will have one row from the first table and one from the second table. Row from the second table should be the first row which is smaller from the date in first row. It should looks this way:



IDcard Date



111 2018-10-01 --> table A
111 2018-08-01 --> table B
111 2018-05-01 --> table A
111 2017-12-01 --> table B


I don't know how to do this. Can you help me?










share|improve this question

























  • it is unclear that exactly is your expected outcome. I could not derive it from the result. What are the criteria, how the rows are selected (e.g. why is the 4th rsult line the 3rd row from table B?)? and why do you want to interleave the tables? this is rather unusual and let me guess, that your data base design is awkward and should be reworked.

    – vlad_tepesch
    Nov 29 '18 at 7:52


















-1















I have 2 tables



A



EmpID IDcard  Date
xxx 111 2018-10-01
zzz 111 2018-05-01


B



Idcard AgrNumb Date
111 x46 2018-10-01
111 x35 2018-05-01
111 x20 2017-12-01
111 x42 2018-08-01


I would like to make table in which I will have one row from the first table and one from the second table. Row from the second table should be the first row which is smaller from the date in first row. It should looks this way:



IDcard Date



111 2018-10-01 --> table A
111 2018-08-01 --> table B
111 2018-05-01 --> table A
111 2017-12-01 --> table B


I don't know how to do this. Can you help me?










share|improve this question

























  • it is unclear that exactly is your expected outcome. I could not derive it from the result. What are the criteria, how the rows are selected (e.g. why is the 4th rsult line the 3rd row from table B?)? and why do you want to interleave the tables? this is rather unusual and let me guess, that your data base design is awkward and should be reworked.

    – vlad_tepesch
    Nov 29 '18 at 7:52
















-1












-1








-1








I have 2 tables



A



EmpID IDcard  Date
xxx 111 2018-10-01
zzz 111 2018-05-01


B



Idcard AgrNumb Date
111 x46 2018-10-01
111 x35 2018-05-01
111 x20 2017-12-01
111 x42 2018-08-01


I would like to make table in which I will have one row from the first table and one from the second table. Row from the second table should be the first row which is smaller from the date in first row. It should looks this way:



IDcard Date



111 2018-10-01 --> table A
111 2018-08-01 --> table B
111 2018-05-01 --> table A
111 2017-12-01 --> table B


I don't know how to do this. Can you help me?










share|improve this question
















I have 2 tables



A



EmpID IDcard  Date
xxx 111 2018-10-01
zzz 111 2018-05-01


B



Idcard AgrNumb Date
111 x46 2018-10-01
111 x35 2018-05-01
111 x20 2017-12-01
111 x42 2018-08-01


I would like to make table in which I will have one row from the first table and one from the second table. Row from the second table should be the first row which is smaller from the date in first row. It should looks this way:



IDcard Date



111 2018-10-01 --> table A
111 2018-08-01 --> table B
111 2018-05-01 --> table A
111 2017-12-01 --> table B


I don't know how to do this. Can you help me?







tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 7:53









vlad_tepesch

4,0731755




4,0731755










asked Nov 27 '18 at 11:15









dejzidejzi

63




63













  • it is unclear that exactly is your expected outcome. I could not derive it from the result. What are the criteria, how the rows are selected (e.g. why is the 4th rsult line the 3rd row from table B?)? and why do you want to interleave the tables? this is rather unusual and let me guess, that your data base design is awkward and should be reworked.

    – vlad_tepesch
    Nov 29 '18 at 7:52





















  • it is unclear that exactly is your expected outcome. I could not derive it from the result. What are the criteria, how the rows are selected (e.g. why is the 4th rsult line the 3rd row from table B?)? and why do you want to interleave the tables? this is rather unusual and let me guess, that your data base design is awkward and should be reworked.

    – vlad_tepesch
    Nov 29 '18 at 7:52



















it is unclear that exactly is your expected outcome. I could not derive it from the result. What are the criteria, how the rows are selected (e.g. why is the 4th rsult line the 3rd row from table B?)? and why do you want to interleave the tables? this is rather unusual and let me guess, that your data base design is awkward and should be reworked.

– vlad_tepesch
Nov 29 '18 at 7:52







it is unclear that exactly is your expected outcome. I could not derive it from the result. What are the criteria, how the rows are selected (e.g. why is the 4th rsult line the 3rd row from table B?)? and why do you want to interleave the tables? this is rather unusual and let me guess, that your data base design is awkward and should be reworked.

– vlad_tepesch
Nov 29 '18 at 7:52














1 Answer
1






active

oldest

votes


















0














Can you try(if in sql-server):



        Select T2.*, EmpID  from Table1 T1
outer apply(
select t2_in.idCard, min(date) as DateT2
from table2 t2_in
where t2_in.idCard = t1.IDcard
group by t2_in.idCard
)t2





share|improve this answer
























  • It doesnt work well. Now I get min(date) from B but I need smaller date than that in A but the biggest from the table B. And I need to this for each row seperate.

    – dejzi
    Nov 27 '18 at 12:46











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%2f53498447%2ftsql-combination-of-two-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









0














Can you try(if in sql-server):



        Select T2.*, EmpID  from Table1 T1
outer apply(
select t2_in.idCard, min(date) as DateT2
from table2 t2_in
where t2_in.idCard = t1.IDcard
group by t2_in.idCard
)t2





share|improve this answer
























  • It doesnt work well. Now I get min(date) from B but I need smaller date than that in A but the biggest from the table B. And I need to this for each row seperate.

    – dejzi
    Nov 27 '18 at 12:46
















0














Can you try(if in sql-server):



        Select T2.*, EmpID  from Table1 T1
outer apply(
select t2_in.idCard, min(date) as DateT2
from table2 t2_in
where t2_in.idCard = t1.IDcard
group by t2_in.idCard
)t2





share|improve this answer
























  • It doesnt work well. Now I get min(date) from B but I need smaller date than that in A but the biggest from the table B. And I need to this for each row seperate.

    – dejzi
    Nov 27 '18 at 12:46














0












0








0







Can you try(if in sql-server):



        Select T2.*, EmpID  from Table1 T1
outer apply(
select t2_in.idCard, min(date) as DateT2
from table2 t2_in
where t2_in.idCard = t1.IDcard
group by t2_in.idCard
)t2





share|improve this answer













Can you try(if in sql-server):



        Select T2.*, EmpID  from Table1 T1
outer apply(
select t2_in.idCard, min(date) as DateT2
from table2 t2_in
where t2_in.idCard = t1.IDcard
group by t2_in.idCard
)t2






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 '18 at 11:20









picklerickpicklerick

32918




32918













  • It doesnt work well. Now I get min(date) from B but I need smaller date than that in A but the biggest from the table B. And I need to this for each row seperate.

    – dejzi
    Nov 27 '18 at 12:46



















  • It doesnt work well. Now I get min(date) from B but I need smaller date than that in A but the biggest from the table B. And I need to this for each row seperate.

    – dejzi
    Nov 27 '18 at 12:46

















It doesnt work well. Now I get min(date) from B but I need smaller date than that in A but the biggest from the table B. And I need to this for each row seperate.

– dejzi
Nov 27 '18 at 12:46





It doesnt work well. Now I get min(date) from B but I need smaller date than that in A but the biggest from the table B. And I need to this for each row seperate.

– dejzi
Nov 27 '18 at 12:46




















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%2f53498447%2ftsql-combination-of-two-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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)