Sql 使用交叉应用左键联接查询

Sql 使用交叉应用左键联接查询,sql,sql-server,tsql,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.invoicesubto

我不确定是否可以将交叉应用函数得到的结果左键合并:

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
但当我这么做的时候,它给了我更多的记录


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

使用外部应用。另外,我不确定是否真的需要外部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

不仅不需要on条款,也不允许我根据安多玛的话删除了on条款。再试一次。它仍然不起作用。我得到了重复的值。。。基本上,我希望它能填充iv.invoiceitemid=tot.invoiceitemid与返回null不匹配的列。您可能会得到重复的值,因为funcglforinvoice返回多行。尝试添加这样的where子句:where tot.invoiceitemid为NULL或iv.invoiceitemid=tot.invoiceitemid这将完成注释的最后一部分。但是,它仍然可能无法消除函数的重复项。该函数返回的行数超过invoiceitemid匹配的行数。这就解决了问题,但它没有执行外部联接,而是执行完整联接您是否有where子句或其他内容,因此您没有从发票表中返回所有记录?我知道您已经单独运行了子选择,所以您知道它返回的整件记录不超过204条:select iii.invoiceid、tot.gldate、tot.glno、tot.glacctdesc、tot.debit、tot.credit、glaccounted from invoice iii cross apply dbo.funcglforinvoiceiii.invoiceid、null、null as tot
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