2013-02-19 86 views
24

不能確定我是否能夠留下參加,我從一個交叉得到的結果應用功能:LEFT JOIN與交叉查詢申請

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個項目,我希望將其與主查詢中的項目保持連接。但我做錯事不知道究竟是什麼。幫助將不勝感激。

+0

您是否有WHERE子句或其他內容,因此您沒有從發票表中返回每條記錄?我知道你已經單獨運行了子查詢,所以你知道它整個文件的返回數不超過204條:(select iii.invoiceid,tot.gldate,tot.glno,tot.glacctdesc,tot.debit,tot。信用,從發票iii交叉申請dbo.funcglforinvoice(iii.invoiceid,null,null)作爲tot) – criticalfix 2013-02-19 21:00:12

回答

41

使用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 
+3

不僅是'on'子句不需要,它不被允許 – Andomar 2013-02-19 20:59:26

+0

這不起作用,這給我一個錯誤。 – 2013-02-19 21:03:25

+0

我根據Andomar所說的刪除了ON子句。再試一次。 – 2013-02-19 21:17:09