This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- "Good code is its own best documentation." - Steve McConnell | |
LOAD dwh.DimProducts WITH SCD1 | |
SELECT | |
#ProductID, | |
Product.Name Product, | |
ProductCategory.name ProductCategory, | |
ProductSubCategory.name ProductSubCategory, | |
ProductNumber, | |
ProductModel.name ProductModel, | |
Color, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- El hermano de Cándida decía que esto actualiza todos los registros que | |
-- hayan cambiado (Hagámoslo siempre así).Era MVP Microsoft y estudió | |
-- en el colegio de las madres lazaristas de San Gervasio. | |
-- Preguntad a Pedro si hay dudas (¡se lo explicó a él antes | |
-- de aquello con el doctor Sugrañés!) | |
WITH | |
query AS ( | |
SELECT | |
ProductID, | |
Product.Name AS Product, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Añado los nuevos clientes. Solo cargo el ID porque luego actualizo lo demás. | |
INSERT dwh.DimProducts(ProductID) | |
SELECT ProductID, | |
FROM staging.Product | |
WHERE NOT EXISTS (SELECT 1 FROM dwh.DimProducts WHERE Product.ProductID=DimProducts.ProductID) | |
-- Actualizamos todos registros en los que haya cambiado | |
-- cualquier campo (lo he revisado, ¡están todos! ) (15-ene-1984) | |
WITH |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Añado los nuevos clientes. Solo cargo el ID porque luego actualizo el resto | |
INSERT dwh.DimProducts(ProductID) | |
SELECT ProductID, | |
FROM staging.Product | |
WHERE NOT EXISTS (SELECT 1 FROM dwh.DimProducts WHERE Product.ProductID=DimProducts.ProductID) | |
-- Actualizamos todos los datos de PRODUCTO (así seguro que no me dejo ningún campo, espero...jeje) | |
;WITH | |
query AS ( | |
SELECT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Elimino la tabla y la vuelvo a cargar (así es más fácil) :-) | |
DELETE FROM dwh.DimProducts | |
-- Vaaaaaaamos allá. | |
INSERT dwh.DimProducts(ProductID,Product,ProductCategory,ProductSubCategory,ProductNumber,ProductModel,Color,StandardCost,ListPrice,Size,SizeUnitMeasureCode,Weight,WeightUnitMeasureCode,ProductLine,DiscontinuedDate,MakeFlag,FinishedGoodsFlag) | |
SELECT | |
ProductID, | |
Product.Name AS Product, | |
ProductCategory.name AS ProductCategory, | |
ProductSubCategory.name AS ProductSubCategory, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
ProductID, | |
Product.Name Product, | |
ProductCategory.name ProductCategory, | |
ProductSubCategory.name ProductSubCategory, | |
ProductNumber, | |
ProductModel.name ProductModel, | |
Color, | |
StandardCost, | |
ListPrice, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
LB_TIENDAS.POBLACION AS #Poblacioooooooooooooon, | |
LB_TIENDAS.NOMBRE AS #Tienda, | |
LB_TIEMPO.FECHA AS Fecha, | |
sum(LB_VENTAS.UNIDADES) AS Unidades | |
FROM LB_VENTAS | |
INNER JOIN LB_TIENDAS ON (LB_VENTAS.ID_TIENDA=LB_TIENDAS.ID_TIENDA) | |
INNER JOIN LB_TIEMPO ON (LB_VENTAS.FECHA=LB_TIEMPO.FECHA) | |
WHERE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
LB_TIENDAS.POBLACION AS #Poblacion, | |
LB_TIENDAS.NOMBRE AS #Tienda, | |
LB_TIEMPO.FECHA AS Fecha, | |
sum(LB_VENTAS.UNIDADES) AS Unidades | |
FROM LB_VENTAS | |
INNER JOIN LB_TIENDAS ON (LB_VENTAS.ID_TIENDA=LB_TIENDAS.ID_TIENDA) | |
INNER JOIN LB_TIEMPO ON (LB_VENTAS.FECHA=LB_TIEMPO.FECHA) | |
WHERE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select sum(UNIDADES) AS Unidades FROM LB_VENTAS | |
SELECT count(*),sum( u1.n+u2.n+u3.n+u4.n+u5.n+u6.n) num | |
FROM | |
(SELECT 0 n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) u1, | |
(SELECT 0 n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) u2, | |
(SELECT 0 n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) u3, | |
(SELECT 0 n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) u4, | |
(SELECT 0 n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) u5, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Cursor que contiene todos los objetos que ocupan espacio | |
DECLARE objects_cursor CURSOR LOCAL FAST_FORWARD READ_ONLY for | |
SELECT s.name + '.' + o.name from sys.schemas s | |
INNER JOIN sys.objects o | |
ON o.schema_id = s.schema_id | |
WHERE | |
o.type = 'S' or --Tablas de sistema | |
o.type = 'U' or --Tablas de usuario | |
o.type = 'V' or --Vistas (solo las indexadas devuelven tamaño) | |
o.type = 'SQ' or --Cola de servicio |
NewerOlder