繁体   English   中英

使用临时表的SQL查询

[英]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简短说明:

  • 您可以为表名和列名使用别名。
  • COLUMN别名用于使结果集中的列标题更易于阅读。
  • 当执行自连接或使用相关subquery (即:在FROM子句中多次列出同一张表)时,使用TABLE别名来缩短SQL以使其更易于读取或写入。

有关更多信息, 请访问

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM