2015-08-17 63 views
0

我對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]名稱前加上對象。

運行查詢,我得到的錯誤:

Invalid object name 'WWS.dbo.dimGlobalShipDestinationCountryTbl'

我試圖找到在數據庫上這個特殊的表,但它不存在,但在查詢懸停在表名給予表定義但沒有腳本。

此表中存在這樣一個怪異的內部連接(第6內部連接)的語法:

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

兩個問題:1。 有人可以請解釋內加入這個查詢語法? 2.這很愚蠢,但有關如何查看可能隱藏的表定義的想法?

+0

您將[database_name]。[schema/dbo]添加到了哪部分? 「dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl」行將別名作爲別名進行別名。你所做的更新只應該在第一部分。所以像'[database_name]。[schema/dbo] .dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl' – xQbert

+0

它可能是一個視圖而不是表格,即使它的名字以Tbl結尾。 –

+0

dimGlobalShipDestinationCountryTbl是名爲「dimGlobalCountryTbl」的表的別名。 – BWS

回答

1

兩個問題:1.有人可以解釋這個查詢語法的內部聯接?

這種情況下的內部聯接只不過是一個表別名。查詢的創建者認爲對錶進行別名會更容易理解該名稱而不是實際的表名,或者同一個表被引用兩次,並且必須有別名。

2.這很愚蠢,但有關如何查看可能隱藏的表定義的想法? 爲什麼?當你添加database_name.schema語法時,我認爲你的SQL只有一個語法錯誤。

像列別名表別名的思考....但並就像列,則可以省略「AS」關鍵字...

dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl相同

dimGlobalCountryTbl AS dimGlobalShipDestinationCountryTbl