使用 join in SQL 连接多个列
concatenating multiple columns with join in SQL
我正在尝试在 SQL 中使用完全外部联接来组合两个不同的 table,但无法确定为什么我的 SQL-查询 returns 为空值。下面的 SQL 代码结合了 Table 1 和 Table 2 基于 Object 和 Date:
SELECT Table1.Date_T1, Table1.Object_T1, Table1.Price_T1, Table2.Price_T2
FROM Table1
LEFT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2
UNION
SELECT Table1.Date_T1, Table1.Object_T1, Table1.Price_T1, Table2.Price_T2
FROM Table1
RIGHT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2;
可以看到 table 的内容 here,以及我尝试创建的结果示例,以及查询的实际结果。查询不包括仅存在于 table 2 而不是 table 1.
中的值
任何有关我如何调整查询以获得每一行的相应日期和对象名称的建议都将不胜感激!
也许您正在寻找 returns 列表的第一个非空值的合并函数 :
coalesce (v1,v2,v3)
- returns v1 if not null
- otherwise returns v2 if not null
...
- otherwise returns v3
select
coalesce(t1.date,t2.date) date_t12,
coalesce(t1.object,t2.object) object_t12,
t1.price_t1,
t2.price_t2
from table1 t1
left join table2 t2
on t1.date=t2.date and t1.object=t2.object
错误是,您在 Union 之后的查询中从 table1 而不是 table2 选择列。
我从 table2 中选择了列并将其重命名为 as columns as named in table1 以便使其能够将 table2 中的列附加到 table1。
如果不想使用 Union 语句,也可以使用 Full outer join
在单个连接语句中获取结果。但是你需要使用 Coalesce
语句并根据你的要求重命名列
试试这个:-
SELECT Table1.Date_T1, Table1.Object_T1, Table1.Price_T1, Table2.Price_T2
FROM Table1
LEFT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2
UNION
SELECT Table2.Date_T2 as Date_T1, Table2.Object_T2 AS Object_T1,
Table1.Price_T1, Table2.Price_T2
FROM Table1
RIGHT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2;
我认为正确答案是 India.Rocket。只是为了清楚起见,我认为如果你只重命名前两列应该有助于你理解(没有必要在第二个查询中这样做,UNION 之后的那个):
SELECT Table1.Date_T1 AS DATE_X, Table1.Object_T1 AS OBJECT_X, Table1.Price_T1, Table2.Price_T2
FROM Table1
LEFT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2
UNION
SELECT Table2.Date_T2, Table2.Object_T2, Table1.Price_T1, Table2.Price_T2
FROM Table1
RIGHT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2;
不用multiple join和union,直接用coalesce full join function.Using COALESCE,如果Table1的值为null,可以显示对应的Table2的值
SELECT COALESCE(Table1.Date_T1,Table2.Date_T1),
COALESCE(Table1.Object_T1,Table2.Object_T1), Table1.Price_T1, Table2.Price_T2
FROM Table1
FULL JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2
使用 Derived union table 而不是 group by
select
x.[Date]
,x.[Object]
,sum(x.[Price T1]) [PriceT1]
,sum(x.[Price T2]) [PriceT2]
from
(
SELECT Table1.Date_T1 [Date], Table1.Object_T1 [Object], Table1.Price_T1 [Price T1], 0 [Price T2]
FROM Table1
union
SELECT Table2.Date_T1 [Date], Table2.Object_T1 [Object], 0 [Price T1], Table2.Price_T2 [Price T2]
FROM Table2
) x
group by
x.[Date],x.[Object]
我正在尝试在 SQL 中使用完全外部联接来组合两个不同的 table,但无法确定为什么我的 SQL-查询 returns 为空值。下面的 SQL 代码结合了 Table 1 和 Table 2 基于 Object 和 Date:
SELECT Table1.Date_T1, Table1.Object_T1, Table1.Price_T1, Table2.Price_T2
FROM Table1
LEFT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2
UNION
SELECT Table1.Date_T1, Table1.Object_T1, Table1.Price_T1, Table2.Price_T2
FROM Table1
RIGHT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2;
可以看到 table 的内容 here,以及我尝试创建的结果示例,以及查询的实际结果。查询不包括仅存在于 table 2 而不是 table 1.
中的值任何有关我如何调整查询以获得每一行的相应日期和对象名称的建议都将不胜感激!
也许您正在寻找 returns 列表的第一个非空值的合并函数 :
coalesce (v1,v2,v3)
- returns v1 if not null
- otherwise returns v2 if not null
...
- otherwise returns v3
select
coalesce(t1.date,t2.date) date_t12,
coalesce(t1.object,t2.object) object_t12,
t1.price_t1,
t2.price_t2
from table1 t1
left join table2 t2
on t1.date=t2.date and t1.object=t2.object
错误是,您在 Union 之后的查询中从 table1 而不是 table2 选择列。
我从 table2 中选择了列并将其重命名为 as columns as named in table1 以便使其能够将 table2 中的列附加到 table1。
如果不想使用 Union 语句,也可以使用 Full outer join
在单个连接语句中获取结果。但是你需要使用 Coalesce
语句并根据你的要求重命名列
试试这个:-
SELECT Table1.Date_T1, Table1.Object_T1, Table1.Price_T1, Table2.Price_T2
FROM Table1
LEFT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2
UNION
SELECT Table2.Date_T2 as Date_T1, Table2.Object_T2 AS Object_T1,
Table1.Price_T1, Table2.Price_T2
FROM Table1
RIGHT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2;
我认为正确答案是 India.Rocket。只是为了清楚起见,我认为如果你只重命名前两列应该有助于你理解(没有必要在第二个查询中这样做,UNION 之后的那个):
SELECT Table1.Date_T1 AS DATE_X, Table1.Object_T1 AS OBJECT_X, Table1.Price_T1, Table2.Price_T2
FROM Table1
LEFT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2
UNION
SELECT Table2.Date_T2, Table2.Object_T2, Table1.Price_T1, Table2.Price_T2
FROM Table1
RIGHT JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2;
不用multiple join和union,直接用coalesce full join function.Using COALESCE,如果Table1的值为null,可以显示对应的Table2的值
SELECT COALESCE(Table1.Date_T1,Table2.Date_T1),
COALESCE(Table1.Object_T1,Table2.Object_T1), Table1.Price_T1, Table2.Price_T2
FROM Table1
FULL JOIN Table2 ON Table1.Object_T1 = Table2.Object_T2
AND Table1.Date_T1=Table2.Date_T2
使用 Derived union table 而不是 group by
select
x.[Date]
,x.[Object]
,sum(x.[Price T1]) [PriceT1]
,sum(x.[Price T2]) [PriceT2]
from
(
SELECT Table1.Date_T1 [Date], Table1.Object_T1 [Object], Table1.Price_T1 [Price T1], 0 [Price T2]
FROM Table1
union
SELECT Table2.Date_T1 [Date], Table2.Object_T1 [Object], 0 [Price T1], Table2.Price_T2 [Price T2]
FROM Table2
) x
group by
x.[Date],x.[Object]