【问题标题】:Inner Join Ambiguous Syntax内连接歧义语法
【发布时间】:2015-08-17 20:30:09
【问题描述】:

我对 SQL 不是很熟悉,但我知道基础知识。我最近尝试将一些逻辑表单报告复制到 SQL Server 2012。我从 Webi(一种报告工具)的自定义查询开始,并尝试在 SQL 中从中创建视图。

查询如下所示:

SELECT
   dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc,
   dimGlobalShipDestinationCountryTbl.area,
   dimGlobalShipDestinationCountryTbl.subarea,
   dimGlobalCurrentProductTbl.sbuCodeDesc,
   dimGlobalShipDateVw.shipDayOfWeekDesc,
   sum(factSalesTblVw.globalSalesValue) AS 'Global Sales Value',
 SUM(factSalesTblVw.salesUnitQuantity*GlobalFiles.dimCurrentGTINTbl.unitQty) AS 'Sales Unit Quantity'
  FROM
  dimGlobalCookCompaniesTbl INNER JOIN factSalesTblVw ON 
 (dimGlobalCookCompaniesTbl.globalCookCompanyID=factSalesTblVw.globalCookCompanyID)
 INNER JOIN dimGlobalHistProductTbl ON (dimGlobalHistProductTbl.globalHistProductID=factSalesTblVw.globalHistProductID)
 INNER JOIN dimGlobalCurrentProductTbl ON (dimGlobalHistProductTbl.globalCurrentProductID=dimGlobalCurrentProductTbl.globalCurrentProductID)
 INNER JOIN dimGlobalHistShipCustomerTbl ON (factSalesTblVw.globalHistShipCustomerID=dimGlobalHistShipCustomerTbl.globalHistShipCustomerID)
 INNER JOIN dimGlobalCurrentShipCustomerTbl ON (dimGlobalHistShipCustomerTbl.shipCustomerID=dimGlobalCurrentShipCustomerTbl.globalCurrentShipCustomerID)
 ***INNER JOIN dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl ON (dimGlobalCurrentShipCustomerTbl.shipDestCountryDesc=dimGlobalShipDestinationCountryTbl.countryCode)***
 INNER JOIN dimGlobalSalesAnalysisTbl ON (factSalesTblVw.globalSalesAnalysisID=dimGlobalSalesAnalysisTbl.globalSalesAnalysisID)
 INNER JOIN dimGlobalShipDateVw ON (dimGlobalShipDateVw.shipJulianDate=factSalesTblVw.shipDateID)
 INNER JOIN GlobalFiles.dimCurrentGTINTbl ON (GlobalFiles.dimCurrentGTINTbl.curGtinId=factSalesTblVw.GtinID)

WHERE
 (
   dimGlobalShipDateVw.shipYearNumber  IN (DATEPART(yy,GETDATE())-1)
   AND
   dimGlobalCurrentShipCustomerTbl.shipCustomerNumberDesc
   IN  ( 'JPC000222-3','CNC000012-1'  )
   AND
  dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc  =  'Return Credits'
 )
GROUP BY
dimGlobalShipDateVw.shipDate, 
dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc, 
dimGlobalShipDestinationCountryTbl.area, 
dimGlobalShipDestinationCountryTbl.subarea, 
dimGlobalCurrentProductTbl.sbuCodeDesc, 
Upper(dimGlobalCurrentProductTbl.familyCodeDesc), 
dimGlobalShipDateVw.shipYearNumber, 
dimGlobalShipDateVw.shipDayOfWeekDesc, 
dimGlobalCurrentProductTbl.madeByAbbr, 
dimGlobalCookCompaniesTbl.companyDesc

如果在相关数据库中运行,此特定查询将在生产系统上运行。当尝试在不同的数据库中查看此查询时,我在对象前面加上 [database_name].[schema/dbo] 名称。

在运行查询时,我收到错误:

无效的对象名称“WWS.dbo.dimGlobalShipDestinationCountryTbl”

我尝试在数据库中找到这个特定的表,但它不存在,尽管将鼠标悬停在查询中的表名上会给出表定义但没有脚本。

这个表出现在一个看起来很奇怪的内部联接(第 6 个内部联接)语法中,如下所示:

INNER JOIN dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl ON (dimGlobalCurrentShipCustomerTbl.shipDestCountryDesc=dimGlobalShipDestinationCountryTbl.countryCode)

两个问题: 1. 有人可以解释一下内部联接的查询语法吗? 2. 这很愚蠢,但是关于如何查看可能隐藏的表定义的任何想法?

【问题讨论】:

  • 您将 [database_name].[schema/dbo] 添加到哪个部分? dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl 行将表别名为不同的名称。您所做的更新应该只在第一部分。就像[database_name].[schema/dbo].dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl
  • 它可能是一个视图而不是一个表,即使它的名称以 Tbl 结尾。
  • dimGlobalShipDestinationCountryTbl 是名为“dimGlobalCountryTbl”的表的别名。

标签: sql join sql-server-2012 ssms


【解决方案1】:

两个问题:1. 谁能解释一下这个内连接的查询语法?

这种情况下的内连接只不过是一个表别名。查询的创建者认为给表加上别名会更容易理解这个名称而不是实际的表名,或者同一个表被引用两次并且必须有一个别名。

2。这很愚蠢,但是关于如何查看可能隐藏的表定义的任何想法? 为什么?我认为当您添加 database_name.schema 语法时,您的 SQL 中出现了语法错误。

将表别名想象为列别名......但就像列一样,您可以省略“AS”关键字......

dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl

相同

dimGlobalCountryTbl AS dimGlobalShipDestinationCountryTbl

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    相关资源
    最近更新 更多