内连接模糊语法

时间:2015-08-17 20:30:10

标签: sql join sql-server-2012 ssms

我不熟悉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] name之前的对象之前。

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

  

无效的对象名称' WWS.dbo.dimGlobalShipDestinationCountryTbl'

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

此表存在于奇怪的内部联接(第6个内部联接)语法中,如下所示:

  

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

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

1 个答案:

答案 0 :(得分:1)

两个问题:1。有人可以解释内部联接的查询语法吗?

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

<强> 2。这是非常愚蠢但有关如何查看可能隐藏的表定义的任何想法? 为什么?我认为在添加database_name.schema语法时,SQL上只有语法错误。

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

dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl

相同

dimGlobalCountryTbl AS dimGlobalShipDestinationCountryTbl