{"id":19732,"date":"2022-11-07T14:31:41","date_gmt":"2022-11-07T09:01:41","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/"},"modified":"2022-11-07T14:31:41","modified_gmt":"2022-11-07T09:01:41","slug":"solved-i-need-to-join-2-databases-with-2-tables-each","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/","title":{"rendered":"[Solved] I need to join 2 databases, with 2 tables each"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-50440061\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"50440061\" data-parentid=\"50439876\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>With a UNION ALL you can get 1 combined resultset from 2 selects.<br \/>\nThen you can group that and SUM the amounts per date.<\/p>\n<p>So you&#8217;re probably looking for something like this:\n<\/p>\n<pre><code>select \n q.ID,\n q.Name,\n nullif(sum(case when q.Date=\"2018-05-01\" then q.Amount end), 0) as \"5\/1\/2018\",\n nullif(sum(case when q.Date=\"2018-05-02\" then q.Amount end), 0) as \"5\/2\/2018\"\nfrom\n(\n  select u1.ID, u1.Name, a1.Date, a1.Amount\n  from DB1.Table1 AS u1\n  join DB1.Table2 AS a1 on (a1.ID = u1.ID and a1.Amount is not null)\n  where a1.Date IN ('2018-05-01', '2018-05-02')\n\n  union all -- combines the results of the 2 selects into one resultset\n\n  select u2.ID, u2.Name, a2.Date, a2.Amount\n  from DB2.Table1 AS u2\n  join DB2.Table2 AS a2 on (a2.ID = u2.ID and a2.Amount is not null)\n  where a2.Date IN ('2018-05-01', '2018-05-02')\n) AS q\ngroup by q.ID, q.Name\norder by q.ID;\n<\/code><\/pre>\n<p>An alternative is to JOIN them all up.\n<\/p>\n<pre><code>select \ncoalesce(a1.ID, a2.ID) as ID,\nmax(coalesce(u1.Name, u2.Name)) as Name, \nmax(case \n    when coalesce(a1.Date, a2.Date) = '2018-05-01'\n     and coalesce(a1.Amount, a2.Amount) is not null\n    then coalesce(a1.Amount, 0) + coalesce(a2.Amount, 0) \n    end) as \"5\/1\/2018\",\nmax(case \n    when coalesce(a1.Date, a2.Date) = '2018-05-02'\n     and coalesce(a1.Amount, a2.Amount) is not null\n    then coalesce(a1.Amount, 0) + coalesce(a2.Amount, 0) \n    end) as \"5\/2\/2018\"\nfrom DB1.Table2 AS a1 \nfull join DB2.Table2 AS a2 on (a2.ID = a1.ID and a2.Date = a1.Date)\nleft join DB1.Table1 AS u1 on (u1.ID = a1.ID)\nleft join DB2.Table1 AS u2 on (u2.ID = a2.ID)\nwhere coalesce(a1.Date, a2.Date) IN ('2018-05-01', '2018-05-02')\ngroup by coalesce(a1.ID, a2.ID)\norder by coalesce(a1.ID, a2.ID);\n<\/code><\/pre>\n<p>But then note that this way, that there&#8217;s an assumption that the two Table2 have a uniqueness on (ID, Date)<\/p>\n<hr>\n<blockquote class=\"spoiler\">\n<p> T-Sql test data:<br \/>\n declare @DB1_Table1 table (id int, Name varchar(30));<br \/>\n declare @DB2_Table1 table (id int, Name varchar(30));<br \/>\n declare @DB1_Table2 table (id int, [Date] date, Amount decimal(8,2));<br \/>\n declare @DB2_Table2 table (id int, [Date] date, Amount decimal(8,2));<br \/>\n insert into @DB1_Table1 (id, Name) values (1,&#8217;Susan&#8217;),(2,&#8217;Juan&#8217;),(3,&#8217;Tracy&#8217;),(4,&#8217;Jenny&#8217;),(5,&#8217;Bill&#8217;);<br \/>\n insert into @DB2_Table1 (id, Name) values (1,&#8217;Susan&#8217;),(2,&#8217;Juan&#8217;),(3,&#8217;Tracy&#8217;),(4,&#8217;Jenny&#8217;),(5,&#8217;Bill&#8217;);<br \/>\n insert into @DB1_Table2 (id, [Date], Amount) values<br \/>\n (1,&#8217;2018-05-01&#8242;,20),(2,&#8217;2018-05-01&#8242;,null),(3,&#8217;2018-05-01&#8242;,30),(4,&#8217;2018-05-01&#8242;,50),(5,&#8217;2018-05-01&#8242;,null),<br \/>\n (1,&#8217;2018-05-02&#8242;,15),(2,&#8217;2018-05-02&#8242;,40),(3,&#8217;2018-05-02&#8242;,25),(4,&#8217;2018-05-02&#8242;,8),(5,&#8217;2018-05-02&#8242;,null);<br \/>\n insert into @DB2_Table2 (id, [Date], Amount) values<br \/>\n (1,&#8217;2018-05-01&#8242;,null),(2,&#8217;2018-05-01&#8242;,15),(3,&#8217;2018-05-01&#8242;,20),(4,&#8217;2018-05-01&#8242;,10),(5,&#8217;2018-05-01&#8242;,null),<br \/>\n (1,&#8217;2018-05-02&#8242;,15),(2,&#8217;2018-05-02&#8242;,30),(3,&#8217;2018-05-02&#8242;,35),(4,&#8217;2018-05-02&#8242;,null),(5,&#8217;2018-05-02&#8242;,30);<\/p>\n<\/blockquote><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">21<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved I need to join 2 databases, with 2 tables each <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] With a UNION ALL you can get 1 combined resultset from 2 selects. Then you can group that and SUM the amounts per date. So you&#8217;re probably looking for something like this: select q.ID, q.Name, nullif(sum(case when q.Date=&#8221;2018-05-01&#8243; then q.Amount end), 0) as &#8220;5\/1\/2018&#8243;, nullif(sum(case when q.Date=&#8221;2018-05-02&#8221; then q.Amount end), 0) as &#8220;5\/2\/2018&#8221; from &#8230; <a title=\"[Solved] I need to join 2 databases, with 2 tables each\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/\" aria-label=\"More on [Solved] I need to join 2 databases, with 2 tables each\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[473,341,353],"class_list":["post-19732","post","type-post","status-publish","format-standard","hentry","category-solved","tag-ms-access","tag-sql","tag-vb-net"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] I need to join 2 databases, with 2 tables each - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] I need to join 2 databases, with 2 tables each - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] With a UNION ALL you can get 1 combined resultset from 2 selects. Then you can group that and SUM the amounts per date. So you&#8217;re probably looking for something like this: select q.ID, q.Name, nullif(sum(case when q.Date=&quot;2018-05-01&quot; then q.Amount end), 0) as &quot;5\/1\/2018&quot;, nullif(sum(case when q.Date=&quot;2018-05-02&quot; then q.Amount end), 0) as &quot;5\/2\/2018&quot; from ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-07T09:01:41+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] I need to join 2 databases, with 2 tables each\",\"datePublished\":\"2022-11-07T09:01:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/\"},\"wordCount\":205,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"ms-access\",\"sql\",\"vb.net\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/\",\"name\":\"[Solved] I need to join 2 databases, with 2 tables each - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-07T09:01:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] I need to join 2 databases, with 2 tables each\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] I need to join 2 databases, with 2 tables each - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] I need to join 2 databases, with 2 tables each - JassWeb","og_description":"[ad_1] With a UNION ALL you can get 1 combined resultset from 2 selects. Then you can group that and SUM the amounts per date. So you&#8217;re probably looking for something like this: select q.ID, q.Name, nullif(sum(case when q.Date=\"2018-05-01\" then q.Amount end), 0) as \"5\/1\/2018\", nullif(sum(case when q.Date=\"2018-05-02\" then q.Amount end), 0) as \"5\/2\/2018\" from ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/","og_site_name":"JassWeb","article_published_time":"2022-11-07T09:01:41+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] I need to join 2 databases, with 2 tables each","datePublished":"2022-11-07T09:01:41+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/"},"wordCount":205,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["ms-access","sql","vb.net"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/","url":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/","name":"[Solved] I need to join 2 databases, with 2 tables each - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-07T09:01:41+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-i-need-to-join-2-databases-with-2-tables-each\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] I need to join 2 databases, with 2 tables each"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=19732"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19732\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=19732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=19732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=19732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}