Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何合并SQL查询(需要帮助)_Sql Server - Fatal编程技术网

Sql server 如何合并SQL查询(需要帮助)

Sql server 如何合并SQL查询(需要帮助),sql-server,Sql Server,亲爱的朋友,下面是我的两个SQL查询: select distinct a_bm.DestProvider_ID, a_bm.DestCircel_ID, convert(datetime,dbo.fnToDate(a_bm.BM_BillFrom),103) as fromdate, convert(datetime,dbo.fnToDate(a_bm.BM_BillTo),103) as todate, t_rec.TapInRec as Bill

亲爱的朋友,下面是我的两个SQL查询:

select distinct  
    a_bm.DestProvider_ID,
    a_bm.DestCircel_ID,
    convert(datetime,dbo.fnToDate(a_bm.BM_BillFrom),103) as fromdate,
    convert(datetime,dbo.fnToDate(a_bm.BM_BillTo),103) as todate,
    t_rec.TapInRec as BillRecevable,
    t_rec.TapInRec as Billreceied
from Auditdata_BillingMaster a_bm 
inner join TapInRecordMaster t_rec
    on a_bm.DestProvider_ID = t_rec.DestProviderMaster_ID
    and a_bm.DestCircel_ID = t_rec.DestCircelMaster_ID
    and convert(datetime,dbo.fnToDate(a_bm.BM_BillFrom),103)> =  
        convert(datetime,t_rec.Months) 
    and convert(datetime,dbo.fnToDate(a_bm.BM_BillTo),103)<= 
        convert(datetime,t_rec.BillTo)
where a_bm.DestProvider_ID=4
and a_bm.DestCircel_ID=22
and a_bm.typeoffile=1
and convert(datetime,dbo.fnToDate(a_bm.BM_BillFrom),103)>=
    convert(datetime,'6/1/2009') 
and convert(datetime,dbo.fnToDate(a_bm.BM_BillFrom),103)<=
    convert(datetime,'7/30/2009')
选择distinct
一个_bm.dest提供者_ID,
a_bm.DestCircel_ID,
将(datetime,dbo.fnToDate(a_bm.bm_BillFrom),103)转换为fromdate,
将(datetime,dbo.fnToDate(a_bm.bm_BillTo),103)转换为todate,
t_rec.TapInRec作为可接收账单,
如账单所示
来自Auditdata\u BillingMaster a\u bm
内部连接磁带记录主机t_rec
在\u bm.DestProvider\u ID=t\u rec.DestProviderMaster\u ID上
和a_bm.DestCircel_ID=t_rec.DestCircelMaster_ID
和转换(datetime,dbo.fnToDate(a_bm.bm_BillFrom),103)>=
转换(日期时间,t_rec.月)
和转换(datetime,dbo.fnToDate(a_bm.bm_BillTo),103)=
转换(日期时间'6/1/2009')
和转换(datetime,dbo.fnToDate(a_bm.bm_BillFrom),103)=
转换(日期时间,dbo.fnToDate(am_bm.bm_BillFrom),103)
和convert(datetime,tmp.todate)=convert(datetime,(b.a1),101)

和convert(datetime,Temp_tbl.todate)您的意思是连接或联合这两个表吗

若要联接这两个查询结果,只需将这两个结果作为JOIN语句的输入即可

如何连接这两个结果实际上取决于数据库设计。优选地,连接基于引用完整性,强制执行结果之间的关系以确保数据完整性。但是,因为您没有提到连接条件,所以我假设您将基于DestProvider\u ID和DestCircel\u ID进行连接

select 
    result1.DestProvider_ID,
    result1.DestCircel_ID,
    result1.fromdate,
    result1.todate,
    result1.BillRecevable,
    result1.Billreceied,
    result2.fromdate
from 
    ( *your first query* ) as result1
inner join
    (select
        Temp_tbl.fromdate, 
        am_bm.DestProvider_ID, 
        am_bm.DestCircel_ID
    from Temp_tbl Temp_tbl

        *the rest of your second query*

    ) as result2 on result1.DestProvider_ID = result2.DestProvider_ID 
                 and result1.DestCircel_ID = result2.DestCircel_ID
工会:

如果要将多个select语句合并到一个结果集中,UNION语句是最简单的方法:

SELECT column1a, column2a, column3a FROM tableA
UNION
SELECT column1b, column2b, column3b FROM tableB
只有在以下情况下才有可能:

  • 两个查询的列数相同
  • 每个查询表达式中的对应列必须具有相同的数据类型
  • column1a的数据类型==column1b
  • column2a的数据类型==column2b
  • column3a的数据类型==column3b

由于两个查询的列数不同,您无法合并它们,至少使用UNION select是这样。

是的,希望这更容易阅读,但我怀疑:-)将它们合并到…做…什么,确切地说?向我们展示这些相关表(Auditdata\u BillingMaster、TapInRecordMaster、temp\u tbl)的表结构(最佳:数据库图)!您希望实现什么,选择?列数和数据类型格式不匹配,您需要重写该查询,以使其能够使用UNION作为组合两个查询的最简单方法。仅举一个示例,说明可以使用UNION合并哪些类型的语句。最后,我提到两个查询不能合并。提问者可能指的是笛卡尔乘积的合并,而不是简单的并集。事实上,我希望他们的意思是,正如你所指出的,目前没有其他方法可以做到这一点。
SELECT column1a, column2a, column3a FROM tableA
UNION
SELECT column1b, column2b, column3b FROM tableB