Skip to content

Instantly share code, notes, and snippets.

@bifacil
Created August 11, 2019 12:50
Show Gist options
  • Select an option

  • Save bifacil/53da807f100850980f137583f2850b8f to your computer and use it in GitHub Desktop.

Select an option

Save bifacil/53da807f100850980f137583f2850b8f to your computer and use it in GitHub Desktop.
SELECT
coalesce(sales1.EnglishProductName,sales2.EnglishProductName) EnglishProductName,
sales1.Quantity2007 Quantity2007,
sales2.Quantity2008 Quantity2008
FROM
(SELECT
DimProduct.EnglishProductName AS EnglishProductName,
sum(sales.OrderQuantity) AS Quantity2007
FROM FactResellerSales sales
INNER JOIN DimProduct ON (sales.ProductKey=DimProduct.ProductKey)
INNER JOIN DimProductSubCategory ON (DimProduct.ProductSubcategoryKey=DimProductSubCategory.ProductSubcategoryKey)
INNER JOIN DimProductCategory ON (DimProductSubCategory.ProductCategoryKey=DimProductCategory.ProductCategoryKey)
INNER JOIN DimDate ON (sales.OrderDateKey=DimDate.DateKey)
WHERE
DimDate.CalendarYear=2007
AND DimProductCategory.SpanishProductCategoryName='Bicicleta'
AND DimDate.SpanishMonthName='Enero'
GROUP BY DimProduct.EnglishProductName) sales1
FULL JOIN
(SELECT
DimProduct.EnglishProductName AS EnglishProductName,
sum(sales.OrderQuantity) AS Quantity2008
FROM FactResellerSales sales
INNER JOIN DimProduct ON (sales.ProductKey=DimProduct.ProductKey)
INNER JOIN DimProductSubCategory ON (DimProduct.ProductSubcategoryKey=DimProductSubCategory.ProductSubcategoryKey)
INNER JOIN DimProductCategory ON (DimProductSubCategory.ProductCategoryKey=DimProductCategory.ProductCategoryKey)
INNER JOIN DimDate ON (sales.OrderDateKey=DimDate.DateKey)
WHERE
DimDate.CalendarYear=2008
AND DimProductCategory.SpanishProductCategoryName='Bicicleta'
AND DimDate.SpanishMonthName='Enero'
GROUP BY DimProduct.EnglishProductName) AS sales2 ON (sales1.EnglishProductName=sales2.EnglishProductName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment