[英]sql query using temp table
我有如下查询
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编写相同的查询。有人可以帮忙吗?
首先,我将更正您的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
(即:在FROM
子句中多次列出同一张表)时,使用TABLE别名来缩短SQL以使其更易于读取或写入。 有关更多信息, 请访问 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.