如何将两个不同sql查询的两个不同结果集合并为一个结果集 [英] How to combine two different result set of two different sql query as a single result set

查看:588
本文介绍了如何将两个不同sql查询的两个不同结果集合并为一个结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



我有两个这样的结果集



分区    提升       批

XYZ             3    &NBSP ;      2

abc        4           2

pqr        3            2



要获得此输出,请使用类似的查询



选择

IMS.IPS_District_Name为[区名称]

SUM(iMs.IPS_OB)的募集,

总和(ImS.IpS_TotRaised)从
$ b核定$ b Intermediate_Overdue_Statistics IOS

内部联接Intermediate_MonthWise_Statistics IMS

on ios.IOS_District_Code = ims.IpS_District_Code AND

IOS.IOS_Taluk_Code = ImS.IpS_Taluk_Code

其中IPS_Month = 3和ims.IPS_Year = 2014

。通过ImS.IpS_District_Name

顺序组由IMS.IPS_District_Name



另一套结果是



分区     待定

xyz             1

abc            2

pqr            1



得到这个结果我用过这样的查询



 <跨度类= 代码关键字>选择 IOS_District_Name,SUM(IOS_TotOverdue)+ SUM(IOS_TotWithinTime)<跨度类= 代码关键字>作为 [待] 
< span class =code-keyword> from Intermediate_Overdue_Statistics
其中 datepart(mm,IOS_DOT)= 3 datepart(yyyy,IOS_DOT)= 2014
GROUP by IOS_District_Name
订单 IOS_District_Name







现在我终于要出去这样了





分区  ;  已提升  已批准  待定

xyz           3    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; 2&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; 1个

ABC&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; 4&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP 2,NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; 2

PQR&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP; 3&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; 2&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   1





任何人都可以帮我解决这个问题吗?

(谢谢使用此查询

 选择 t1.District Name,t1.Raised,t1.Approved,t2.Pending  from  
Select
IMS.IPS_District_将命名为 [区域名称],
SUM(iMs.IPS_OB) as 已提升,
萨姆(ImS.IpS_TotRaised)<跨度类= 代码关键字>作为已批准<跨度类= 代码关键字>从
Intermediate_Overdue_Statistics IOS
< span class =code-keyword> inner join Intermediate_MonthWise_Statistics IMS
on ios.IOS_District_Code = ims.IpS_District_Code AND
IOS.IOS_Taluk_Code = ImS.IpS_Taluk_Code
其中 IPS_Month = 3 ims.IPS_Year = 2014
group by ImS.IpS_District_Name
order by IMS.IPS_District_Name) t1 inner join 选择 IOS_District_Name,SUM(IOS_TotOverdue)+ SUM(IOS_TotWithinTime)<跨度类= 代码关键字 >作为 [待]
<跨度类= 代码关键字 > from
Intermediate_Overdue_Statistics
其中 datepart(mm,IOS_DOT)= 3 datepart (yyyy,IOS_DOT)= 2014
GROUP by IOS_District_Name
order by IOS_District_Name)t2 on t1.District_Name = t2。 IOS_District_Name







查询格式:



 选择 t1.colmn,t2。  from  select  * from table1)t1  inner   join  select  * from table2)t2  on  t1.id = t2.id 


尝试这样的事情:

  SELECT  [区域名称],SUM(已提升)< span class =code-keyword> AS 已提升,SUM(已批准) AS 已批准,SUM(待定) AS 待定
FROM
SELECT IMS.IPS_District_Name < span class =code-keyword> as [区域名称],IMS.IPS_OB AS 已升级,IMS.IpS_TotRaised AS 已批准,IOS.IOS_TotOverdue + IOS.IOS_TotWithinTime <跨度类= 代码关键字 > AS
<跨度类= 代码关键字> FROM Intermediate_Overdue_Statistics AS IOS INNER JOIN Interm ediate_MonthWise_Statistics AS IMS ON IOS.IOS_District_Code = IMS.IpS_District_Code AND
IOS.IOS_Taluk_Code = IMS.IpS_Taluk_Code
<跨度类= 代码关键字> WHERE IMS.IPS_Month = 3的 AND IMS.IPS_Year = 2014 AND datepart(mm,IOS.IOS_DOT)= 3 datepart (yyyy,IOS.IOS_DOT)= 2014
AS t1
GROUP < span class =code-keyword> BY
t1。[地区名称]
ORDER BY t1。[地区名称]


Hi experts,

I have two result set like this

district     Raised        approved
xyz             3            2
abc        4           2
pqr        3            2

To get this output am using a query like

Select
IMS.IPS_District_Name as [District Name],
SUM(iMs.IPS_OB) as Raised,
Sum(ImS.IpS_TotRaised) as Approved from
Intermediate_Overdue_Statistics IOS
inner join Intermediate_MonthWise_Statistics IMS
on ios.IOS_District_Code=ims.IpS_District_Code AND
IOS.IOS_Taluk_Code =ImS.IpS_Taluk_Code
where IPS_Month =3 and ims.IPS_Year =2014
group by ImS.IpS_District_Name
order by IMS.IPS_District_Name

another result set is

district       pending
xyz              1
abc             2
pqr             1

to get this result i have used query like this

Select IOS_District_Name,SUM(IOS_TotOverdue )+SUM (IOS_TotWithinTime) as [Pending]
from Intermediate_Overdue_Statistics
where datepart(mm,IOS_DOT)=3 and datepart(yyyy,IOS_DOT)=2014
GROUP by IOS_District_Name
order by IOS_District_Name




now finally i want out put like this


district    Raised   Approved  Pending
xyz           3              2          1
abc                4                2            2
pqr          3              2          1


Can any one please help me to solve this issue??
(Thanks in advance)

解决方案

use this query

select t1.District Name,t1.Raised,t1.Approved ,t2.Pending from
 (Select
IMS.IPS_District_Name as [District Name],
SUM(iMs.IPS_OB) as Raised,
Sum(ImS.IpS_TotRaised) as Approved from
Intermediate_Overdue_Statistics IOS
inner join Intermediate_MonthWise_Statistics IMS
on ios.IOS_District_Code=ims.IpS_District_Code AND
IOS.IOS_Taluk_Code =ImS.IpS_Taluk_Code
where IPS_Month =3 and ims.IPS_Year =2014
group by ImS.IpS_District_Name
order by IMS.IPS_District_Name) t1 inner join (Select IOS_District_Name,SUM(IOS_TotOverdue )+SUM (IOS_TotWithinTime) as [Pending]
from Intermediate_Overdue_Statistics
where datepart(mm,IOS_DOT)=3 and datepart(yyyy,IOS_DOT)=2014
GROUP by IOS_District_Name
order by IOS_District_Name) t2 on t1.District_Name=t2.IOS_District_Name 




Query Format:

select t1.colmn,t2.column  from(select *From table1)t1 inner join (select *From table2) t2 on t1.id=t2.id


Try something like this:

SELECT [District Name], SUM(Raised) AS Raised, SUM(Approved) AS Approved, SUM(Pending) AS Pending
FROM ( 
    SELECT IMS.IPS_District_Name as [District Name], IMS.IPS_OB AS Raised, IMS.IpS_TotRaised AS Approved, IOS.IOS_TotOverdue + IOS.IOS_TotWithinTime AS Pending
    FROM Intermediate_Overdue_Statistics AS IOS INNER JOIN Intermediate_MonthWise_Statistics AS IMS ON IOS.IOS_District_Code=IMS.IpS_District_Code AND
        IOS.IOS_Taluk_Code =IMS.IpS_Taluk_Code
    WHERE IMS.IPS_Month =3 AND IMS.IPS_Year =2014 AND datepart(mm,IOS.IOS_DOT)=3 and datepart(yyyy,IOS.IOS_DOT)=2014
    ) AS t1
GROUP BY t1.[District Name]
ORDER BY t1.[District Name]


这篇关于如何将两个不同sql查询的两个不同结果集合并为一个结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆