SlideShare a Scribd company logo
1 of 9
Quickly Debugging Joins
in SQL Server
Debugging Joins
Finding an error in a join between two tables can be very time consuming
This query has an error in one of the joins
We will show you a very quick way to find it
select SalesOrderNumber
, OrderQuantity, SalesAmount, FullDateAlternateKey as OrderDate,p.ProductAlternateKey as ProductKey,
p.EnglishProductName
, psc.EnglishProductSubcategoryName, pc.EnglishProductCategoryName, c.FirstName, c.LastName,
g.EnglishCountryRegionName, s.SalesTerritoryCountry
from [dbo].[FactInternetSales] f
INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey]
INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey]
INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey]
INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey]
INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey]
INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey]
INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey]
Start by going back to basics
Copy the whole query
Then change the select clause to read
select count(1) as RowCounter
This will just tell us how many rows it returns
select count(1) as RowCounter
from [dbo].[FactInternetSales] f
INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey]
INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey]
INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey]
INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey]
INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey]
INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey]
INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey]
Next focus on the first fact table
Comment out all other joins
Then Run it and check the result, this is how many total
rows should be in the results
select count(1) as RowCounter
from [dbo].[FactInternetSales] f
--INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey]
--INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey]
--INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey]
--INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey]
--INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey]
--INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey]
--INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey]
Start Feeding some of the joins back in
Uncomment a few of the joins and run again
select count(1) as RowCounter
from [dbo].[FactInternetSales] f
INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey]
INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey]
INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey]
--INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey]
--INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey]
--INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey]
--INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey]
select count(1) as RowCounter
from [dbo].[FactInternetSales] f
INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey]
INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey]
INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey]
INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey]
INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey]
INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey]
--INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey]
Its always the last one…..
In our example it is the last row
select count(1) as RowCounter
from [dbo].[FactInternetSales] f
INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey]
INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey]
INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey]
INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey]
INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey]
INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey]
INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey]
Examine the join in the last row,
We find it is using an incorrect key
select count(1) as RowCounter
from [dbo].[FactInternetSales] f
INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey]
INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey]
INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey]
INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey]
INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey]
INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey]
INNER JOIN [dbo].[DimSalesTerritory] s ON g.[SalesTerritoryKey] = s.[SalesTerritoryKey]
Fixed….
Finally replace the incorrect join in your original query
select SalesOrderNumber
, OrderQuantity, SalesAmount, FullDateAlternateKey as OrderDate,p.ProductAlternateKey as ProductKey
, p.EnglishProductName, psc.EnglishProductSubcategoryName, pc.EnglishProductCategoryName, c.FirstName,
c.LastName, g.EnglishCountryRegionName, s.SalesTerritoryCountry
from [dbo].[FactInternetSales] f
INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey]
INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey]
INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey]
INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey]
INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey]
INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey]
INNER JOIN [dbo].[DimSalesTerritory] s ON g.[SalesTerritoryKey] = s.[SalesTerritoryKey]
Hope you find these useful
For more Tips, Tricks and
Timesavers, visit our website
Tips and Timesavers | Select Distinct Limited
Credit: simon.harrison@selectdistinct.co.uk

More Related Content

Similar to SQL Tips Debugging Joins.pptx

Database Development Replication Security Maintenance Report
Database Development Replication Security Maintenance ReportDatabase Development Replication Security Maintenance Report
Database Development Replication Security Maintenance Report
nyin27
 
Kevin Bengtson Portfolio
Kevin Bengtson PortfolioKevin Bengtson Portfolio
Kevin Bengtson Portfolio
Kbengt521
 
Sql Portfolio(March 31)
Sql Portfolio(March 31)Sql Portfolio(March 31)
Sql Portfolio(March 31)
iceolated
 
Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)
Paulo Gandra de Sousa
 

Similar to SQL Tips Debugging Joins.pptx (18)

Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Database Development Replication Security Maintenance Report
Database Development Replication Security Maintenance ReportDatabase Development Replication Security Maintenance Report
Database Development Replication Security Maintenance Report
 
Data Warehouses and Multi-Dimensional Data Analysis
Data Warehouses and Multi-Dimensional Data AnalysisData Warehouses and Multi-Dimensional Data Analysis
Data Warehouses and Multi-Dimensional Data Analysis
 
SQL Server 2008 Portfolio
SQL Server 2008 PortfolioSQL Server 2008 Portfolio
SQL Server 2008 Portfolio
 
JKJ_T SQL project code samples
JKJ_T SQL project code samplesJKJ_T SQL project code samples
JKJ_T SQL project code samples
 
T-SQL-Assignment
T-SQL-AssignmentT-SQL-Assignment
T-SQL-Assignment
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional Programming
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Freeing Yourself from an RDBMS Architecture
Freeing Yourself from an RDBMS ArchitectureFreeing Yourself from an RDBMS Architecture
Freeing Yourself from an RDBMS Architecture
 
Kevin Bengtson Portfolio
Kevin Bengtson PortfolioKevin Bengtson Portfolio
Kevin Bengtson Portfolio
 
Procedures
ProceduresProcedures
Procedures
 
Sql Portfolio(March 31)
Sql Portfolio(March 31)Sql Portfolio(March 31)
Sql Portfolio(March 31)
 
Joy of scala
Joy of scalaJoy of scala
Joy of scala
 
Grails66 web service
Grails66 web serviceGrails66 web service
Grails66 web service
 
Domain Driven Design Primer
Domain Driven Design PrimerDomain Driven Design Primer
Domain Driven Design Primer
 
SQL Basics - Lecture.ppt
SQL Basics - Lecture.pptSQL Basics - Lecture.ppt
SQL Basics - Lecture.ppt
 
PoEAA by Example
PoEAA by ExamplePoEAA by Example
PoEAA by Example
 
Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)
 

More from Select Distinct Limited

Power BI Tips Top N Measures.pptx
Power BI Tips Top N Measures.pptxPower BI Tips Top N Measures.pptx
Power BI Tips Top N Measures.pptx
Select Distinct Limited
 

More from Select Distinct Limited (20)

Year on Year Comparison in Power BI.pptx
Year on Year Comparison in Power BI.pptxYear on Year Comparison in Power BI.pptx
Year on Year Comparison in Power BI.pptx
 
Sync Slicers in Power BI a step by step guide
Sync Slicers in Power BI a step by step guideSync Slicers in Power BI a step by step guide
Sync Slicers in Power BI a step by step guide
 
Using Google Search Console Data in Power BI.pptx
Using Google Search Console Data in Power BI.pptxUsing Google Search Console Data in Power BI.pptx
Using Google Search Console Data in Power BI.pptx
 
Data Lake v Data Warehouse. What is the difference?
Data Lake v Data Warehouse. What is the difference?Data Lake v Data Warehouse. What is the difference?
Data Lake v Data Warehouse. What is the difference?
 
How to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by stepHow to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by step
 
Top 5 SQL Tips and Timesaver 2023, our most popular posts
Top 5 SQL Tips and Timesaver 2023, our most popular postsTop 5 SQL Tips and Timesaver 2023, our most popular posts
Top 5 SQL Tips and Timesaver 2023, our most popular posts
 
Top 5 Power BI tips 2023 most popular blog posts
Top 5 Power BI tips 2023 most popular blog postsTop 5 Power BI tips 2023 most popular blog posts
Top 5 Power BI tips 2023 most popular blog posts
 
CTEs in SQL.pptx
CTEs in SQL.pptxCTEs in SQL.pptx
CTEs in SQL.pptx
 
Calculated Columns and Measures in Power BI.pptx
Calculated Columns and Measures in Power BI.pptxCalculated Columns and Measures in Power BI.pptx
Calculated Columns and Measures in Power BI.pptx
 
Divide by Zero Errors
Divide by Zero ErrorsDivide by Zero Errors
Divide by Zero Errors
 
When to transform data for Power BI.pptx
When to transform data for Power BI.pptxWhen to transform data for Power BI.pptx
When to transform data for Power BI.pptx
 
Power BI KPIs
Power BI KPIsPower BI KPIs
Power BI KPIs
 
Direction of travel on a map in Power BI.pptx
Direction of travel on a map in Power BI.pptxDirection of travel on a map in Power BI.pptx
Direction of travel on a map in Power BI.pptx
 
UNION in DAX
UNION in DAXUNION in DAX
UNION in DAX
 
APPEND data in Power Query
APPEND data in Power QueryAPPEND data in Power Query
APPEND data in Power Query
 
Power BI Connect to Google BigQuery.pptx
Power BI Connect to Google BigQuery.pptxPower BI Connect to Google BigQuery.pptx
Power BI Connect to Google BigQuery.pptx
 
Group by ROLLUP in SQL Server
Group by ROLLUP in SQL ServerGroup by ROLLUP in SQL Server
Group by ROLLUP in SQL Server
 
Advanced Top N Measures in Power BI
Advanced Top N Measures in Power BIAdvanced Top N Measures in Power BI
Advanced Top N Measures in Power BI
 
Custom Formats in Power BI
Custom Formats in Power BICustom Formats in Power BI
Custom Formats in Power BI
 
Power BI Tips Top N Measures.pptx
Power BI Tips Top N Measures.pptxPower BI Tips Top N Measures.pptx
Power BI Tips Top N Measures.pptx
 

Recently uploaded

如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
zifhagzkk
 
obat aborsi Bontang wa 081336238223 jual obat aborsi cytotec asli di Bontang6...
obat aborsi Bontang wa 081336238223 jual obat aborsi cytotec asli di Bontang6...obat aborsi Bontang wa 081336238223 jual obat aborsi cytotec asli di Bontang6...
obat aborsi Bontang wa 081336238223 jual obat aborsi cytotec asli di Bontang6...
yulianti213969
 
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
ppy8zfkfm
 
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
acoha1
 
如何办理(UCLA毕业证书)加州大学洛杉矶分校毕业证成绩单学位证留信学历认证原件一样
如何办理(UCLA毕业证书)加州大学洛杉矶分校毕业证成绩单学位证留信学历认证原件一样如何办理(UCLA毕业证书)加州大学洛杉矶分校毕业证成绩单学位证留信学历认证原件一样
如何办理(UCLA毕业证书)加州大学洛杉矶分校毕业证成绩单学位证留信学历认证原件一样
jk0tkvfv
 
如何办理英国卡迪夫大学毕业证(Cardiff毕业证书)成绩单留信学历认证
如何办理英国卡迪夫大学毕业证(Cardiff毕业证书)成绩单留信学历认证如何办理英国卡迪夫大学毕业证(Cardiff毕业证书)成绩单留信学历认证
如何办理英国卡迪夫大学毕业证(Cardiff毕业证书)成绩单留信学历认证
ju0dztxtn
 
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotecAbortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
edited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdfedited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdf
great91
 
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
acoha1
 

Recently uploaded (20)

如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
 
Predictive Precipitation: Advanced Rain Forecasting Techniques
Predictive Precipitation: Advanced Rain Forecasting TechniquesPredictive Precipitation: Advanced Rain Forecasting Techniques
Predictive Precipitation: Advanced Rain Forecasting Techniques
 
社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction
 
obat aborsi Bontang wa 081336238223 jual obat aborsi cytotec asli di Bontang6...
obat aborsi Bontang wa 081336238223 jual obat aborsi cytotec asli di Bontang6...obat aborsi Bontang wa 081336238223 jual obat aborsi cytotec asli di Bontang6...
obat aborsi Bontang wa 081336238223 jual obat aborsi cytotec asli di Bontang6...
 
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
Statistics Informed Decisions Using Data 5th edition by Michael Sullivan solu...
 
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
 
Sensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
Sensing the Future: Anomaly Detection and Event Prediction in Sensor NetworksSensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
Sensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
 
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
 
The Significance of Transliteration Enhancing
The Significance of Transliteration EnhancingThe Significance of Transliteration Enhancing
The Significance of Transliteration Enhancing
 
如何办理(UCLA毕业证书)加州大学洛杉矶分校毕业证成绩单学位证留信学历认证原件一样
如何办理(UCLA毕业证书)加州大学洛杉矶分校毕业证成绩单学位证留信学历认证原件一样如何办理(UCLA毕业证书)加州大学洛杉矶分校毕业证成绩单学位证留信学历认证原件一样
如何办理(UCLA毕业证书)加州大学洛杉矶分校毕业证成绩单学位证留信学历认证原件一样
 
如何办理英国卡迪夫大学毕业证(Cardiff毕业证书)成绩单留信学历认证
如何办理英国卡迪夫大学毕业证(Cardiff毕业证书)成绩单留信学历认证如何办理英国卡迪夫大学毕业证(Cardiff毕业证书)成绩单留信学历认证
如何办理英国卡迪夫大学毕业证(Cardiff毕业证书)成绩单留信学历认证
 
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotecAbortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
 
NOAM AAUG Adobe Summit 2024: Summit Slam Dunks
NOAM AAUG Adobe Summit 2024: Summit Slam DunksNOAM AAUG Adobe Summit 2024: Summit Slam Dunks
NOAM AAUG Adobe Summit 2024: Summit Slam Dunks
 
How to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data AnalyticsHow to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data Analytics
 
Digital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
Digital Marketing Demystified: Expert Tips from Samantha Rae CoolbethDigital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
Digital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
 
Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024
 
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
 
edited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdfedited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdf
 
Aggregations - The Elasticsearch "GROUP BY"
Aggregations - The Elasticsearch "GROUP BY"Aggregations - The Elasticsearch "GROUP BY"
Aggregations - The Elasticsearch "GROUP BY"
 
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
 

SQL Tips Debugging Joins.pptx

  • 2. Debugging Joins Finding an error in a join between two tables can be very time consuming This query has an error in one of the joins We will show you a very quick way to find it select SalesOrderNumber , OrderQuantity, SalesAmount, FullDateAlternateKey as OrderDate,p.ProductAlternateKey as ProductKey, p.EnglishProductName , psc.EnglishProductSubcategoryName, pc.EnglishProductCategoryName, c.FirstName, c.LastName, g.EnglishCountryRegionName, s.SalesTerritoryCountry from [dbo].[FactInternetSales] f INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey] INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey] INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey] INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey] INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey] INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey] INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey]
  • 3. Start by going back to basics Copy the whole query Then change the select clause to read select count(1) as RowCounter This will just tell us how many rows it returns select count(1) as RowCounter from [dbo].[FactInternetSales] f INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey] INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey] INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey] INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey] INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey] INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey] INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey]
  • 4. Next focus on the first fact table Comment out all other joins Then Run it and check the result, this is how many total rows should be in the results select count(1) as RowCounter from [dbo].[FactInternetSales] f --INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey] --INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey] --INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey] --INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey] --INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey] --INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey] --INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey]
  • 5. Start Feeding some of the joins back in Uncomment a few of the joins and run again select count(1) as RowCounter from [dbo].[FactInternetSales] f INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey] INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey] INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey] --INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey] --INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey] --INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey] --INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey] select count(1) as RowCounter from [dbo].[FactInternetSales] f INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey] INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey] INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey] INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey] INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey] INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey] --INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey]
  • 6. Its always the last one….. In our example it is the last row select count(1) as RowCounter from [dbo].[FactInternetSales] f INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey] INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey] INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey] INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey] INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey] INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey] INNER JOIN [dbo].[DimSalesTerritory] s ON g.[GeographyKey] = s.[SalesTerritoryKey] Examine the join in the last row, We find it is using an incorrect key select count(1) as RowCounter from [dbo].[FactInternetSales] f INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey] INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey] INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey] INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey] INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey] INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey] INNER JOIN [dbo].[DimSalesTerritory] s ON g.[SalesTerritoryKey] = s.[SalesTerritoryKey]
  • 7. Fixed…. Finally replace the incorrect join in your original query select SalesOrderNumber , OrderQuantity, SalesAmount, FullDateAlternateKey as OrderDate,p.ProductAlternateKey as ProductKey , p.EnglishProductName, psc.EnglishProductSubcategoryName, pc.EnglishProductCategoryName, c.FirstName, c.LastName, g.EnglishCountryRegionName, s.SalesTerritoryCountry from [dbo].[FactInternetSales] f INNER JOIN [dbo].[DimDate] d ON f.[OrderDateKey] = d.[DateKey] INNER JOIN [dbo].[DimProduct] p ON f.[ProductKey] = p.[ProductKey] INNER JOIN [dbo].[DimProductSubcategory] psc ON p.[ProductSubcategoryKey] = psc.[ProductSubcategoryKey] INNER JOIN [dbo].[DimProductCategory] pc ON psc.[ProductCategoryKey] = pc.[ProductCategoryKey] INNER JOIN [dbo].[DimCustomer] c ON f.[CustomerKey] = c.[CustomerKey] INNER JOIN [dbo].[DimGeography] g ON c.[GeographyKey] = g.[GeographyKey] INNER JOIN [dbo].[DimSalesTerritory] s ON g.[SalesTerritoryKey] = s.[SalesTerritoryKey]
  • 8. Hope you find these useful
  • 9. For more Tips, Tricks and Timesavers, visit our website Tips and Timesavers | Select Distinct Limited Credit: simon.harrison@selectdistinct.co.uk