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

时间:2009-08-08 13:07:38

标签: 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 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')

select Temp_tbl.fromdate from Temp_tbl Temp_tbl
inner join (
    select
        convert(datetime,dbo.fnToDate(BM_BillFrom),103) as a1,
        convert(datetime,dbo.fnToDate(BM_BillTo),103) as b1,
        count(*) as c1,
        am_bm.DestProvider_ID,
        am_bm.DestCircel_ID
    from Auditdata_BillingMaster am_bm
    inner join Temp_tbl tmp
        on tmp.Provider_ID=am_bm.DestProvider_ID
        and tmp.Circel_ID=am_bm.DestCircel_ID
    where  convert(datetime,tmp.fromdate)>=
        convert(datetime,dbo.fnToDate(am_bm.BM_BillFrom),103)
    and convert(datetime,tmp.todate) <= 
        convert(datetime,dbo.fnToDate(am_bm.BM_BillTo),103)
    group by
        convert(datetime,dbo.fnToDate(BM_BillFrom),103),
        convert(datetime,dbo.fnToDate(BM_BillTo),103),
        am_bm.DestProvider_ID,
        am_bm.DestCircel_ID
    ) b
    on Temp_tbl.Provider_ID = b.DestProvider_ID
    and Temp_tbl.Circel_ID = b.DestCircel_ID
    and convert(datetime,Temp_tbl.fromdate,101)>= convert(datetime,(b.a1),101)
    and convert(datetime,Temp_tbl.todate) <= convert(datetime,(b.b1),101)

我想在SQL Server 2000中合并上面的2个SQL查询。

请帮帮我。

提前致谢。

1 个答案:

答案 0 :(得分:1)

你的意思是加入或联合这两张桌子吗?

如果您想要同时加入两个查询结果,只需将两个结果作为JOIN语句的输入。

如何加入两个结果实际上取决于您的数据库设计。优选地,连接基于参考完整性,其强制执行结果之间的关系以确保数据完整性。但是由于你没有提到连接条件,我假设你将基于DestProvider_ID&amp; amp; DestCircel_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

UNION:

如果你想获取多个select语句并将它们组合成一个结果集,UNION语句是最简单的方法:

SELECT column1a, column2a, column3a FROM tableA
UNION
SELECT column1b, column2b, column3b FROM tableB

只有在以下情况下才有可能:

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

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