SQL • Build Queries With or Without Coding

Listing 1. You can use SQL Server's Query Builder to create SELECT statements visually, or you can code them in Transact-SQL. This query summarizes sales for each employee by year, sales territory group, and sales territory country.

SELECT
   SUM(FactResellerSales.SalesAmount) AS ActualSales,
   DimTime.CalendarYear,
   DimSalesTerritory.SalesTerritoryGroup, 
   DimSalesTerritory.SalesTerritoryCountry,
   DimEmployee.FirstName + ' ' + DimEmployee.LastName AS 
      Employee
FROM
   FactResellerSales
   INNER JOIN DimEmployee ON
      FactResellerSales.EmployeeKey = 
         DimEmployee.EmployeeKey
   INNER JOIN DimTime ON
      FactResellerSales.OrderDateKey = DimTime.TimeKey
   INNER JOIN DimSalesTerritory ON
      FactResellerSales.SalesTerritoryKey = 
         DimSalesTerritory.SalesTerritoryKey AND 
      DimEmployee.SalesTerritoryKey = 
         DimSalesTerritory.SalesTerritoryKey
GROUP BY
   DimTime.CalendarYear,
   DimSalesTerritory.SalesTerritoryGroup,
   DimSalesTerritory.SalesTerritoryCountry,
   DimEmployee.FirstName + ' ' + DimEmployee.LastName