Northwind Database (Document! X Sample)
AdventureWorks Database / Sales Schema / Sales.vSalesPersonSalesByFiscalYears View
In This Topic
    Sales.vSalesPersonSalesByFiscalYears View
    In This Topic
    Description
    Uses PIVOT to return aggregated sales information for each sales representative.
    Properties
    Creation Date27/10/2017 14:33
    Is Schema Bound
    Encrypted
    Ansi Nulls
    Columns
     Column NameDescriptionDatatypeLengthAllow NullsDefaultFormula
      Integer4  
      VarWChar152  
      VarWChar50   
      dbo.Name50   
      Currency8  
      Currency8  
      Currency8  
    Objects that Sales.vSalesPersonSalesByFiscalYears depends on
     Database ObjectObject TypeDescriptionDep Level
    dbo.AccountNumber datatypedbo.AccountNumberUser Defined Data Type 2
    Person.Address tablePerson.AddressTableStreet address information for customers, employees, and vendors.1
    Sales.CreditCard tableSales.CreditCardTableCustomer credit card information.2
    Sales.Currency tableSales.CurrencyTableLookup table containing standard ISO currencies.3
    Sales.CurrencyRate tableSales.CurrencyRateTableCurrency exchange rates.2
    Sales.Customer tableSales.CustomerTableCurrent customer information. Also see the Person and Store tables.2
    HumanResources.Employee tableHumanResources.EmployeeTableEmployee information such as salary, department, and title.3
    dbo.ErrorLog tabledbo.ErrorLogTableAudit table tracking errors in the the AdventureWorks database that are caught by the CATCH block of a TRY...CATCH construct. Data is inserted by stored procedure dbo.uspLogError when it is executed from inside the CATCH block of a TRY...CATCH construct.3
    dbo.Flag datatypedbo.FlagUser Defined Data Type 4
    dbo.Name datatypedbo.NameUser Defined Data Type 2
    dbo.NameStyle datatypedbo.NameStyleUser Defined Data Type 5
    dbo.OrderNumber datatypedbo.OrderNumberUser Defined Data Type 2
    Person.Person tablePerson.PersonTableHuman beings involved with AdventureWorks: employees, customer contacts, and vendor contacts.4
    Sales.SalesOrderHeader tableSales.SalesOrderHeaderTableGeneral sales order information.1
    Sales.SalesPerson tableSales.SalesPersonTableSales representative current information.2
    Sales.SalesTerritory tableSales.SalesTerritoryTableSales territory lookup table.3
    Purchasing.ShipMethod tablePurchasing.ShipMethodTableShipping company lookup table.2
    Person.StateProvince tablePerson.StateProvinceTableState and province lookup table.1
    Sales.Store tableSales.StoreTableCustomers (resellers) of Adventure Works products.1
    dbo.ufnGetAccountingEndDate functiondbo.ufnGetAccountingEndDateUser Defined FunctionScalar function used in the uSalesOrderHeader trigger to set the starting account date.2
    dbo.ufnGetAccountingStartDate functiondbo.ufnGetAccountingStartDateUser Defined FunctionScalar function used in the uSalesOrderHeader trigger to set the ending account date.2
    dbo.ufnLeadingZeros functiondbo.ufnLeadingZerosUser Defined FunctionScalar function used by the Sales.Customer table to help set the account number.3
    dbo.uspLogError proceduredbo.uspLogErrorStored ProcedureLogs error information in the ErrorLog table about the error that caused execution to jump to the CATCH block of a TRY...CATCH construct. Should be executed from within the scope of a CATCH block otherwise it will return without inserting error information.2
    dbo.uspPrintError proceduredbo.uspPrintErrorStored ProcedurePrints error information about the error that caused execution to jump to the CATCH block of a TRY...CATCH construct. Should be executed from within the scope of a CATCH block otherwise it will return without printing any error information.2
    View Definition
    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;
    
    See Also