Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Visualização SQL de Vendas de Data Paralela


Para vendas de clientes em anos diferentes.

Demonstração do 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            

Para chave de produto

Demonstração do 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