按产品类别-SQL比较在线销售情况

时间:2015-10-03 12:26:09

标签: sql sql-server adventureworks

我是SQL的初学者,我需要在过去三年的第一季度比较每个国家/地区的产品类别的在线销售情况。 我想在adventureworks中查询。我该怎么做?谢谢

1 个答案:

答案 0 :(得分:0)

SELECT Co.Name              AS Country
     , C.Name               AS Category
     , YEAR(OH.OrderDate)   AS [Year]
     , DATEPART(QUARTER,OH.OrderDate) AS [Quater]
     , SUM(OD.LineTotal)    AS Sales
FROM       [Sales].[SalesOrderDetail]        OD 
INNER JOIN [Sales].[SalesOrderHeader]        OH  ON OD.SalesOrderID = OH.SalesOrderID
INNER JOIN [Production].[Product]            P   ON OD.ProductID = P.ProductID
INNER JOIN [Production].[ProductSubcategory] SC  ON P.ProductSubcategoryID = SC.ProductSubcategoryID
INNER JOIN [Production].[ProductCategory]    C   ON SC.ProductCategoryID = C.ProductCategoryID
INNER JOIN [Person].[BusinessEntity]         Pe  ON Pe.BusinessEntityID = OH.CustomerID
INNER JOIN [Person].[BusinessEntityAddress]  A   ON A.BusinessEntityID = Pe.BusinessEntityID
INNER JOIN [Person].[Address]                AD  ON AD.AddressID = A.AddressID
INNER JOIN [Person].[StateProvince]          SP  ON AD.StateProvinceID = SP.StateProvinceID
INNER JOIN [Person].[CountryRegion]          Co  ON SP.CountryRegionCode = Co.CountryRegionCode
WHERE OH.OnlineOrderFlag = 1
 AND DATEPART(QUARTER,OH.OrderDate) = 1
 AND OH.OrderDate >= DATEADD(YEAR, DATEDIFF(YEAR,0,GETDATE()) -3, 0) 
GROUP BY Co.Name , C.Name , YEAR(OH.OrderDate), DATEPART(QUARTER,OH.OrderDate)