【问题标题】:I need to join 2 databases, with 2 tables each我需要加入 2 个数据库,每个数据库有 2 个表
【发布时间】:2018-05-27 18:45:27
【问题描述】:

亲爱的, 我有一个 SQL 问题。我需要加入 2 个数据库,每个数据库有 2 个表。我在这里有数据库表的图片。非常感谢您的帮助。

【问题讨论】:

  • 请发布您到目前为止尝试过的内容,发布代码和您遇到的问题。

标签: sql vb.net ms-access


【解决方案1】:

使用 UNION ALL,您可以从 2 个选择中获得 1 个组合结果集。
然后你可以对它进行分组并对每个日期的金额求和。

所以你可能正在寻找这样的东西:

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
(
  select u1.ID, u1.Name, a1.Date, a1.Amount
  from DB1.Table1 AS u1
  join DB1.Table2 AS a1 on (a1.ID = u1.ID and a1.Amount is not null)
  where a1.Date IN ('2018-05-01', '2018-05-02')

  union all -- combines the results of the 2 selects into one resultset

  select u2.ID, u2.Name, a2.Date, a2.Amount
  from DB2.Table1 AS u2
  join DB2.Table2 AS a2 on (a2.ID = u2.ID and a2.Amount is not null)
  where a2.Date IN ('2018-05-01', '2018-05-02')
) AS q
group by q.ID, q.Name
order by q.ID;

另一种方法是将它们全部加入。

select 
coalesce(a1.ID, a2.ID) as ID,
max(coalesce(u1.Name, u2.Name)) as Name, 
max(case 
    when coalesce(a1.Date, a2.Date) = '2018-05-01'
     and coalesce(a1.Amount, a2.Amount) is not null
    then coalesce(a1.Amount, 0) + coalesce(a2.Amount, 0) 
    end) as "5/1/2018",
max(case 
    when coalesce(a1.Date, a2.Date) = '2018-05-02'
     and coalesce(a1.Amount, a2.Amount) is not null
    then coalesce(a1.Amount, 0) + coalesce(a2.Amount, 0) 
    end) as "5/2/2018"
from DB1.Table2 AS a1 
full join DB2.Table2 AS a2 on (a2.ID = a1.ID and a2.Date = a1.Date)
left join DB1.Table1 AS u1 on (u1.ID = a1.ID)
left join DB2.Table1 AS u2 on (u2.ID = a2.ID)
where coalesce(a1.Date, a2.Date) IN ('2018-05-01', '2018-05-02')
group by coalesce(a1.ID, a2.ID)
order by coalesce(a1.ID, a2.ID);

但请注意,这样一来,假设两个 Table2 在 (ID, Date) 上具有唯一性


T-Sql 测试数据: 声明@DB1_Table1 表(id​​ int,Name varchar(30)); 声明@DB2_Table1 表(id​​ int,Name varchar(30)); 声明@DB1_Table2 table (id int, [Date] date, Amount decimal(8,2)); 声明@DB2_Table2 table (id int, [Date] date, Amount decimal(8,2)); 插入@DB1_Table1 (id, Name) 值 (1,'Susan'),(2,'Juan'),(3,'Tracy'),(4,'Jenny'),(5,'Bill'); 插入@DB2_Table1 (id, Name) 值 (1,'Susan'),(2,'Juan'),(3,'Tracy'),(4,'Jenny'),(5,'Bill'); 插入@DB1_Table2 (id, [Date], Amount) 值 (1,'2018-05-01',20),(2,'2018-05-01',null),(3,'2018-05-01',30),(4,'2018-05- 01',50),(5,'2018-05-01',null), (1,'2018-05-02',15),(2,'2018-05-02',40),(3,'2018-05-02',25),(4,'2018-05- 02',8),(5,'2018-05-02',null); 插入@DB2_Table2 (id, [Date], Amount) 值 (1,'2018-05-01',null),(2,'2018-05-01',15),(3,'2018-05-01',20),(4,'2018-05- 01',10),(5,'2018-05-01',null), (1,'2018-05-02',15),(2,'2018-05-02',30),(3,'2018-05-02',35),(4,'2018-05- 02',null),(5,'2018-05-02',30);

【讨论】:

  • 感谢 LukStorms。由于我使用的是MS Access,因此我将“case”更改为“switch”,SQL语句如下:
  • 对不起,我不知道如何缩进和换行,就像您在答案中所做的那样。启动 SQL 语句查询后:它要求我提供日期值、a2.amount、ID 和名称。然后一无所有地返回。
  • SELECT ID, Name,
    Sum(Switch(Date=#5/1/2018#,a1.Amount+a2.Amount)) AS ["5/1/2018"] , Sum(Switch(Date=#5/2/2018#,a1.Amount+a2.Amount)) AS ["5/2/2018"] FROM (select u1.EmpID, u1.FirstName, a1.Date, a1 .Amount from Employee u1 INNER join Sale a1 on (a1.EmployeeID = u1.EmpID and a1.Amount is not null) union select u2.EmpID, u2.FirstName, a2.Date, a2.Amount from Employee1 u2 INNER join Sale1 a2 on (a2.EmployeeID = u2.EmpID and a2.Amount is not null) ) AS q GROUP BY ID, Name ORDER BY ID;
  • @TonyNguyen 不知道这是针对 MS Access 的。但要意识到外部查询只知道 q.Amount。另请注意,在其他数据库中,UNION 删除重复项,而 UNION ALL 保留重复项。这会影响 SUM。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
相关资源
最近更新 更多