【发布时间】: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