并排列合并
Side by Side column merge
我需要创建一个价格比较工具,所以这样的需求来了。为此,我从数据库查询中得到了两个不同的结果集。我的数据库是 SQL Server 2017 Developer Edition。我想要的是并排合并两个结果集。见下图:
我的查询
SELECT ADuration Duration, APlan_Price AmazonPrice
FROM tbl_AmazonPlans
WHERE AProduct_Name = 'Air Conditioner'
AND 14678 BETWEEN TRY_CONVERT(int, PARSENAME(REPLACE(APrice_Range, '-', '.'), 2))
AND TRY_CONVERT(int, PARSENAME(REPLACE(APrice_Range, '-', '.'), 1))
SELECT Duration, Plan_Price GwPrice
FROM tbl_PlanDetails
WHERE Product_Name = 'Air Conditioner'
AND 14678 BETWEEN TRY_CONVERT(int, PARSENAME(REPLACE(Price_Range, '-', '.'), 2))
AND TRY_CONVERT(int, PARSENAME(REPLACE(Price_Range, '-', '.'), 1))
更新
来自表 may/may 的结果集不具有相同的架构。也没有连接点,因为它们没有公共键。但是,是的,它们包含一些可能相同的列,并且基于这些相同的列,我必须获得预期的结果集。
Duration 列是两个表中共同的标识符吗?如果是,那么你可以使用内部连接。
select
Duration,
APlan_Price as AmazonPrice
Plan_Price as GwPrice
from tbl_AmazonPlans
inner join tbl_PlanDetails on tbl_PlanDetails.Duration = tbl_AmazonPlans.Duration
where /* Your filter here */
您可以直接加入表格,但如果您的 where
标准可能不同以保持简单,您可以直接加入结果集
select a.duration, a.AmazonPrice, b.GwPrice
from (
Select ADuration Duration, APlan_Price AmazonPrice
From tbl_AmazonPlans
Where AProduct_Name ='Air Conditioner'
and 14678 between try_convert(int,parsename(replace(APrice_Range,'-','.'),2))
and try_convert(int,parsename(replace(APrice_Range,'-','.'),1))
)a
join
(
Select Duration, Plan_Price GwPrice
From tbl_PlanDetails
Where Product_Name ='Air Conditioner'
and 14678 between try_convert(int,parsename(replace(Price_Range,'-','.'),2))
and try_convert(int,parsename(replace(Price_Range,'-','.'),1))
)b on a.duration = b.duration
我需要创建一个价格比较工具,所以这样的需求来了。为此,我从数据库查询中得到了两个不同的结果集。我的数据库是 SQL Server 2017 Developer Edition。我想要的是并排合并两个结果集。见下图:
我的查询
SELECT ADuration Duration, APlan_Price AmazonPrice
FROM tbl_AmazonPlans
WHERE AProduct_Name = 'Air Conditioner'
AND 14678 BETWEEN TRY_CONVERT(int, PARSENAME(REPLACE(APrice_Range, '-', '.'), 2))
AND TRY_CONVERT(int, PARSENAME(REPLACE(APrice_Range, '-', '.'), 1))
SELECT Duration, Plan_Price GwPrice
FROM tbl_PlanDetails
WHERE Product_Name = 'Air Conditioner'
AND 14678 BETWEEN TRY_CONVERT(int, PARSENAME(REPLACE(Price_Range, '-', '.'), 2))
AND TRY_CONVERT(int, PARSENAME(REPLACE(Price_Range, '-', '.'), 1))
更新
来自表 may/may 的结果集不具有相同的架构。也没有连接点,因为它们没有公共键。但是,是的,它们包含一些可能相同的列,并且基于这些相同的列,我必须获得预期的结果集。
Duration 列是两个表中共同的标识符吗?如果是,那么你可以使用内部连接。
select
Duration,
APlan_Price as AmazonPrice
Plan_Price as GwPrice
from tbl_AmazonPlans
inner join tbl_PlanDetails on tbl_PlanDetails.Duration = tbl_AmazonPlans.Duration
where /* Your filter here */
您可以直接加入表格,但如果您的 where
标准可能不同以保持简单,您可以直接加入结果集
select a.duration, a.AmazonPrice, b.GwPrice
from (
Select ADuration Duration, APlan_Price AmazonPrice
From tbl_AmazonPlans
Where AProduct_Name ='Air Conditioner'
and 14678 between try_convert(int,parsename(replace(APrice_Range,'-','.'),2))
and try_convert(int,parsename(replace(APrice_Range,'-','.'),1))
)a
join
(
Select Duration, Plan_Price GwPrice
From tbl_PlanDetails
Where Product_Name ='Air Conditioner'
and 14678 between try_convert(int,parsename(replace(Price_Range,'-','.'),2))
and try_convert(int,parsename(replace(Price_Range,'-','.'),1))
)b on a.duration = b.duration