左加入带有交叉申请的查询

时间:2013-02-19 20:45:53

标签: sql sql-server tsql

不确定我是否可以加入我从交叉应用函数中得到的结果:

select
iv.invoiceno
,w.warehouse
,iv.invoicedate
,iv.invoicedesc
,iv.status
,iv.billingstart as [BillingFrom]
,iv.billingend as [BillingTo]
,CAST((iv.invoicesubtotal) as NUMERIC(38,2))as [Sub-Total] 
,CAST((((iv.invoicesubtotal+iv.invoicetax)-iv.invoicetotal)) as NUMERIC(38,2)) as [Discount]
,CAST((iv.invoicetax) as NUMERIC(38,2)) as [SalesTax]
,CAST((iv.invoicetotal) as NUMERIC(38,2)) as [Total]
,d.deal
,d.dealno
,ivt.orderno 
,ivt.rectype    
,ivt.rectypedisplay                                
,RTRIM(ivt.masterno) as [ICode]                               
,ivt.description as [ICodeDesc]                            
,ivt.fromdate as [From]                                
,ivt.todate as [To]                                  
,CAST((ivt.days ) as NUMERIC(38,2)) as [days]                                  
,CAST(ivt.qty as NUMERIC(38,0)) as [qty]                                  
,CAST((ivt.cost) as NUMERIC(38,2)) as [UnitCost]                                   
,CAST((ivt.rate) as NUMERIC(38,2)) as [rate]                                      
,CAST((ivt.daysinwk)as NUMERIC(38,2)) as [D/W]                              
,CAST((ivt.discountamt)as NUMERIC(38,2)) as [Discount]                             
,CAST((ivt.extended)as NUMERIC(38,2)) as [extended]                               
,(CASE WHEN ivt.taxable='T' then 'YES' else 'NO' END)as [Taxable]
,ivt.category
,(CASE WHEN (ivt.cost > 0 and ivt.rectype='R') THEN CAST((ivt.revenuebase) as NUMERIC (38,2)) ELSE 0 END) as [subrevenue]  from invoice iv
inner join deal d                                   on d.dealid=iv.dealid
inner join invoiceitemview ivt                      on iv.invoiceid=ivt.invoiceid and iv.invoiceno=ivt.invoiceno
inner join warehouse w                              on w.locationid=iv.locationid and w.inactive<>'T'
left join category c                                on c.categoryid=ivt.categoryid 
left join ordernoteview n                           on ivt.orderid=n.orderid and n.billing ='T'  where iv.locationid='00009V5H' and iv.invoiceno='H513369' and iv.status in ('CLOSED', 'PROCESSED') and iv.nocharge<>'T'         order by iv.invoiceno, iv.invoicedate,c.category,ivt.masterno

我想在此查询中添加左连接:

select  tot.gldate, tot.glno, tot.glacctdesc,                  
   tot.debit,tot.credit,tot.glaccountid                       from invoice ivt cross apply dbo.funcglforinvoice(ivt.invoiceid, null, null) as tot where ivt.invoiceno='H513369'

但是当我这样做时,它给了我更多的记录然后应该是。

这已经执行了一段时间。基本上内部交叉应用查询生成204个项目,我希望与主查询中的项目保持联接;但我做错了什么不确定到底是什么。帮助将不胜感激。

1 个答案:

答案 0 :(得分:55)

使用OUTER APPLY。另外我不确定是否真的需要OUTER APPLY之后的ON子句。如果invoiceid与进入时相同,那么可能不会。

Select iv.invoiceno, iv.invoiceitem,iv.invoiceno
   from invoice iv
inner join deal d
        on d.dealid=iv.dealid
inner join invoiceitemview ivt
        on iv.invoiceid=ivt.invoiceid and iv.invoiceno=ivt.invoiceno
inner join warehouse w
        on w.locationid=iv.locationid and w.inactive<>'T'
left join category c
        on c.categoryid=ivt.categoryid 
left join ordernoteview n
        on ivt.orderid=n.orderid and n.billing ='T'
OUTER APPLY dbo.funcglforinvoice(iv.invoiceid, null, null) as tot
相关问题