我有如下查询
select VendorNumber,sum(EY_AmountIncl_LC)AmountIncl_LC ,SUm(EY_AmountExcl_LC)AmountExcl_LC,max(EY_datedocumented) Datedocumented
,stuff( (select distinct ','+dbo.table2.InvoiceStatus
from dbo.table2
where dbo.table2.VendorNumber = dbo.table2.VendorNumber
for xml path('')
), 1, 1, ''
) as InvoiceStatus
from dbo.table2
group by VendorNumber
我该如何在sql server management studio中使用temptable编写相同的查询。有人可以帮忙吗?
答案 0 :(得分:2)
首先,我会更正您的subquery
条件,该条件应从外部查询中引用:
select VendorNumber, sum(EY_AmountIncl_LC) AmountIncl_LC,
max(EY_datedocumented) Datedocumented,
stuff( (select distinct ','+t22.InvoiceStatus
from dbo.table2 t22 -- create alias & use them
where t22.VendorNumber = t2.VendorNumber
for xml path('')
), 1, 1, ''
) as InvoiceStatus
from dbo.table2 t2 -- create alias & use them
group by VendorNumber;
现在,临时表具有与基本表相同的功能,只需将临时表名(dbo.table2
替换为基本表名(#temp
)即可。
关于alias
的简短说明:
subquery
时更易于读取或写入(即:在{{1 }}条款。有关更多信息,您可以visit。