如何在一个结果中将两个查询与不同的列和条件组合在一起 [英] How to combined two queries in one result with different columns and conditions

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

问题描述

query1: -

Select expen.schemeno, sum(expen.amount) as Premonth,sch_mas.disp,sch_mas.sch_amount,
 sch_mas.san_no , CONVERT(varchar, sch_mas.sdate, 106)as schdate  from [dbo].[expen] inner join
  sch_mas on expen.schemeno=sch_mas.sch_no  where schemeno='W2/2013/11516' and exp_date<'2013-04-01' and 
  passingDate is not null group by sch_mas.disp, expen.schemeno,sch_mas.sch_amount,sch_mas.sdate,sch_mas.san_no





查询2: -

Select monyr, SUM(expen.amount) as expenamt,schemeno from expen  where exp_date>'2013-03-31' and exp_date<'2014-03-31' and schemeno='W2/2013/11516' group by monyr,schemeno





我尝试了什么:





What I have tried:

combined two  queries in one result with different columns and conditions

推荐答案

如果两个查询中的列相同,则可以使用UNION(Transact-SQL)| Microsoft Docs [ ^ ]。但由于结果集不同,这是不可能的。



但是,根据您的情况,您可以利用单个SQL Server批处理实际上可以返回多个结果的事实集。例如,如果您在SSMS中的单个批处理中执行多个查询,则可以在单独的网格中获得所有结果。许多客户端编程语言都能够从单个SQL Server批处理中消耗多个结果。有关详细信息,请参阅使用多个活动结果集(MARS)| Microsoft Docs [ ^ ]。
If the columns in both queries would be identical you could use UNION (Transact-SQL) | Microsoft Docs[^] . But since the result sets differ this is not possible.

However, depending on your situation you could take advantage the fact that a single SQL Server batch can actually return multiple result sets. For example if you execute multiple queries in a single batch in SSMS, you get all the results in separate grids. Many of the client programming languages are capable of consuming multiple results from a single SQL Server batch. For more information, see Using Multiple Active Result Sets (MARS) | Microsoft Docs[^].


这篇关于如何在一个结果中将两个查询与不同的列和条件组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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