text: ﻿-- Copyright (c) Microsoft. All rights reserved.
  -- Licensed under the MIT license. See LICENSE file in the project root for full license information.
  
  --DECLARE @TerritoryId INT
  --SET @TerritoryId = 1
  
  SELECT 
  soh.[SalesOrderNumber]  AS [OrderNumber],
  soh.[Status]            AS [Status],
  soh.[OrderDate]         AS [OrderDate],
  soh.[DueDate]           AS [DueDate],
  soh.[ShipDate]          AS [ShipDate],
  soh.[SubTotal]          AS [SubTotal],
  soh.[TaxAmt]            AS [TaxAmt],
  soh.[TotalDue]          AS [TotalDue],
  c.[AccountNumber]       AS [AccountNumber],
  p.[Title]               AS [CustomerTitle],
  p.[FirstName]           AS [CustomerFirstName],
  p.[MiddleName]          AS [CustomerMiddleName],
  p.[LastName]            AS [CustomerLastName],
  p.[Suffix]              AS [CustomerSuffix],
  sod.[OrderQty]          AS [Quantity],
  sod.[UnitPrice]         AS [UnitPrice],
  sod.[LineTotal]         AS [LineTotal],
  sod.[ProductID]         AS [ProductId]
  FROM [Sales].[SalesOrderHeader] soh
  INNER JOIN [Sales].[Customer] c ON soh.[CustomerID] = c.[CustomerID]
  INNER JOIN [Person].[Person] p ON c.[PersonID] = p.[BusinessEntityID]
  INNER JOIN [Sales].[SalesOrderDetail] sod ON soh.[SalesOrderID] = sod.[SalesOrderID]
  WHERE soh.[TerritoryId] = @TerritoryId
  AND soh.[SalesOrderId] IN (
  	SELECT TOP 20 SalesOrderId 
  	FROM [Sales].[SalesOrderHeader] soh 
  	WHERE soh.[TerritoryId] = @TerritoryId
  	ORDER BY soh.[TotalDue] DESC)
  ORDER BY soh.[TotalDue] DESC, sod.[SalesOrderDetailID]
  