Answered AdventureWorks2012 View Needs Update

  • Thursday, July 19, 2012 8:06 PM
     
      Has Code

    The OrderDate in Sales.SalesOrderHeader has been advanced 4 years to 2005 to 2008. However, the vSalesPersonSalesByFiscalYears view definition remained the old.  The years ([2002], [2003], [2004]) must be bumped up to ([2006], [2007], [2008])

    CREATE VIEW [Sales].[vSalesPersonSalesByFiscalYears] 
    AS 
    SELECT 
        pvt.[SalesPersonID]
        ,pvt.[FullName]
        ,pvt.[JobTitle]
        ,pvt.[SalesTerritory]
        ,pvt.[2002]
        ,pvt.[2003]
        ,pvt.[2004] 
    FROM (SELECT 
            soh.[SalesPersonID]
            ,p.[FirstName] + ' ' + COALESCE(p.[MiddleName], '') + ' ' + p.[LastName] AS [FullName]
            ,e.[JobTitle]
            ,st.[Name] AS [SalesTerritory]
            ,soh.[SubTotal]
            ,YEAR(DATEADD(m, 6, soh.[OrderDate])) AS [FiscalYear] 
        FROM [Sales].[SalesPerson] sp 
            INNER JOIN [Sales].[SalesOrderHeader] soh 
            ON sp.[BusinessEntityID] = soh.[SalesPersonID]
            INNER JOIN [Sales].[SalesTerritory] st 
            ON sp.[TerritoryID] = st.[TerritoryID] 
            INNER JOIN [HumanResources].[Employee] e 
            ON soh.[SalesPersonID] = e.[BusinessEntityID] 
    		INNER JOIN [Person].[Person] p
    		ON p.[BusinessEntityID] = sp.[BusinessEntityID]
    	 ) AS soh 
    PIVOT 
    (
        SUM([SubTotal]) 
        FOR [FiscalYear] 
        IN ([2002], [2003], [2004])
    ) AS pvt;


    Kalman Toth SQL SERVER 2012 & BI TRAINING
    New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012


All Replies

  • Tuesday, July 24, 2012 7:32 AM
    Moderator
     
     Answered

    Hi SQLUSA,

    Thanks for sharing your idea!


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.