从使用内部联接联接两个表时创建的一个表中删除列

时间:2018-09-11 12:25:08

标签: sql sql-server

我通过使用innerjoin和以下代码将两个临时表连接起来

IF OBJECT_ID('tempdb..#temp_table3') IS NOT NULL
    DROP TABLE #temp_table3

select VendorNumber,stuff( (select distinct ','+dbo.vendordata.InvoiceStatus
                               from dbo.vendordata
                               where dbo.vendordata.VendorNumber = dbo.vendordata.VendorNumber 
                               for xml path('')
                              ), 1, 1, ''
                            ) as InvoiceStatus
into #temp_table3
from dbo.vendordata
group by VendorNumber


IF OBJECT_ID('tempdb..#temp_table4') IS NOT NULL
    DROP TABLE #temp_table4

select VendorNumber, sum(EY_AmountIncl_LC) AmountIncl_LC,
       sum(EY_AmountExcl_LC) AmountExcl_LC, max(EY_datedocumented) Datedocumented
into #temp_table4
from dbo.vendordata
group by VendorNumber

select * 
from #temp_table3 T inner join 
     #temp_table4 V 
     on T.vendorNumber = V.VendorNumber

现在输出如下

VendorNumber  Invoicestatus      VendorNumber   EY_AmountIncl   EY_AmountExcl     EY_datedocumented
50000471     V-Parked S-Noted     50000471        281072           281072           00:00.0
5200991      V-Parked S-Noted     5200991         80000            80000            00:00.0
5200595      V-Parked S-Noted     5200595         72401.6         72401.6           00:00.0

如何删除重复两次的供应商编号,任何一个都可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

最后一次选择

从...中选择*的行为。

仅选择您需要的列,例如:

select T.VendorNumber, T.InvoiceStatus, V.AmountIncl_LC, 
       V.AmountExcl_LC, V.Datedocumented
from #temp_table3 T inner join 
     #temp_table4 V 
     on T.vendorNumber = V.VendorNumber