两个 table 中的三列到一个 table 中的四列
Three columns in two tables to four columns in one table
我正在尝试合并两个 table,每个都有三列,其中两列是常见的。 income
table有Location
、date
和Amt_recd
,expense
table有Location
、date
和 Amt_spent
。我想合并这两个 table,所以有一个 table 有四列,任何常见的日期都合并到一行。我试过这个:
select location, date, amt_recd, null as amt_spent
from income
union
select location, date, null as amt_recd, amt_spent
from expense
它让我只差一步,因为它不像日期那样组合成一行,它有两行,其中 amt_recd
是 null
,amt_spent
是null
在另一个。什么是构建它的更好方法,以便我得到更浓缩的结果?我已经尝试了各种连接而不是联合,但一直无法获得我正在寻找的结果。
您应该能够很容易地连接这两个表。
select i.location, i.date, i.amt_recd, e.amt_spent
from income i
inner join expense e
on i.location = e.location
and i.date = e.date
尝试JOIN
表格
select i.location, i.date, i.amt_recd as amt_recd, e.amt_spent as amt_spent
from income i
full outer join expense e
on i.location = e.location
and i.date = e.date
假设(位置、日期)的组合在每个 table 中都是唯一的,你说 union
是错误的工作工具并查看 join
是正确的]s 代替。由于您可能有收入没有支出或支出没有收入,因此 full outer join
是有序的:
SELECT i.location, i.date, i.amt_recd, e.amt_spent
FROM income i
FULL OUTER JOIN expense e ON i.location = e.location AND i.date = e.date
我的答案基于你原来的这一行 post:
I want to combine these two tables so there is one table with four
columns, and any common dates are combined to one row.
后半部分表明您不仅需要 JOIN,还需要聚合。
SELECT
COALESCE(i.location, e.location) AS Location
, COALESCE(i.[date], e.[date]) AS [date]
, SUM(i.amt_recd) AS amt_recd
, SUM(e.amt_spent) AS amt_spent
FROM income I
FULL OUTER JOIN expense e
on i.location = e.location
and i.date = e.date
GROUP BY
COALESCE(i.location, e.location)
, COALESCE(i.[date], e.[date])
这将为您提供每一行位置和日期的唯一组合。
如果您确实需要进一步将结果减少到每个日期一行,您将需要指定在给定日期有多个位置时显示哪个位置的规则。
我正在尝试合并两个 table,每个都有三列,其中两列是常见的。 income
table有Location
、date
和Amt_recd
,expense
table有Location
、date
和 Amt_spent
。我想合并这两个 table,所以有一个 table 有四列,任何常见的日期都合并到一行。我试过这个:
select location, date, amt_recd, null as amt_spent
from income
union
select location, date, null as amt_recd, amt_spent
from expense
它让我只差一步,因为它不像日期那样组合成一行,它有两行,其中 amt_recd
是 null
,amt_spent
是null
在另一个。什么是构建它的更好方法,以便我得到更浓缩的结果?我已经尝试了各种连接而不是联合,但一直无法获得我正在寻找的结果。
您应该能够很容易地连接这两个表。
select i.location, i.date, i.amt_recd, e.amt_spent
from income i
inner join expense e
on i.location = e.location
and i.date = e.date
尝试JOIN
表格
select i.location, i.date, i.amt_recd as amt_recd, e.amt_spent as amt_spent
from income i
full outer join expense e
on i.location = e.location
and i.date = e.date
假设(位置、日期)的组合在每个 table 中都是唯一的,你说 union
是错误的工作工具并查看 join
是正确的]s 代替。由于您可能有收入没有支出或支出没有收入,因此 full outer join
是有序的:
SELECT i.location, i.date, i.amt_recd, e.amt_spent
FROM income i
FULL OUTER JOIN expense e ON i.location = e.location AND i.date = e.date
我的答案基于你原来的这一行 post:
I want to combine these two tables so there is one table with four columns, and any common dates are combined to one row.
后半部分表明您不仅需要 JOIN,还需要聚合。
SELECT
COALESCE(i.location, e.location) AS Location
, COALESCE(i.[date], e.[date]) AS [date]
, SUM(i.amt_recd) AS amt_recd
, SUM(e.amt_spent) AS amt_spent
FROM income I
FULL OUTER JOIN expense e
on i.location = e.location
and i.date = e.date
GROUP BY
COALESCE(i.location, e.location)
, COALESCE(i.[date], e.[date])
这将为您提供每一行位置和日期的唯一组合。
如果您确实需要进一步将结果减少到每个日期一行,您将需要指定在给定日期有多个位置时显示哪个位置的规则。