Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Vue SQL des ventes à date parallèle

Pour les ventes aux clients au cours des différentes années.

Démo SQL Fiddle

SELECT DimCustomer.CustomerName,
       VW_Current.Saledate,
       VW_Current.ParallelDate,
       VW_Current.CurrentSales,
       VW_Previous.CurrentSales as ParallelSale

FROM   DimCustomer 
       INNER JOIN VW_ParallelSales VW_Current
           ON DimCustomer.CustomerKey = VW_Current.CustomerKey
       LEFT JOIN VW_ParallelSales VW_Previous
           ON VW_Current.ParallelDate = VW_Previous.Saledate
          AND DimCustomer.CustomerKey = VW_Previous.CustomerKey
ORDER BY 1, 2            

Pour la clé de produit

Démo SQL Fiddle

With sales as (
    SELECT 
           DimProduct.ProductKey,
           DimProduct.ProductName,
           VW_ParallelSales.Saledate,
           VW_ParallelSales.ParallelDate,
           VW_ParallelSales.CurrentSales,
           VW_ParallelSales.ParallelSales
    FROM   DimProduct INNER JOIN VW_ParallelSales ON DimProduct.ProductKey = 
           VW_ParallelSales.ProductKey
)
SELECT 
   s_recent.ProductName,
   s_recent.Saledate ThisYear,
   s_old.Saledate PreviousYear,
   s_recent.CurrentSales CurrentSales,
   s_old.CurrentSales ParallelSales
FROM 
    SALES s_recent 
    left outer join SALES s_old 
        on s_recent.saledate = s_old.saledate + 10000
       and s_recent.ProductKey = s_old.ProductKey