Adventureworks 练习 SQL 服务器
Adventureworks exercises SQL Server
Using the SalesOrderHeader and SalesTerritory tables, write a query to calculate the number of transactions and
total monthly amount per territory for each of the states based on the Status field. Status values are:
1 = In process ; 2 = Approved ; 3 = Backordered ; 4 = Rejected ; 5 = Shipped ; 6 = Canceled
The result table must contain the following fields:
- 月份:YYYY-MM 格式的月份
- NameTerritory : 领土名称
- TrProcess:处理中的交易(数量)
- TrApproved:批准的交易(数量)
- TrBackordered:逾期交易(数量)
- TrRejected:被拒绝的交易(数量)
- TrShipped:发送的交易(数量)
- TrCanceled:交易取消(数量)
- MntProcess:处理中的总量
- MntApproved:已批准的总金额
- MntBackordered:欠款总额
- MntRejected:拒绝总金额
- MntShipped:发送的总数量
- MntCanceled:已取消的总金额
SELECT
FORMAT(A.ShipDate, 'yyyy-MM') as Mes,
B.Name,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 1) as TrProcess,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 2) as TrApproved,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 3) as TrBackordered,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 4) as TrRejected,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 5) as TrShipped,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 6) as TrCanceled,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 1) as MntProcess,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 2) as MntApproved,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 3) as MntBackordered,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 4) as MntRejected,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 5) as MntShipped,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 6) as MntCanceled
FROM [dbo].[SalesOrderHeader] A
INNER JOIN [dbo].[SalesTerritory] B on A.TerritoryID = B.TerritoryID
GROUP BY FORMAT(A.ShipDate,'yyyy/MM')
我设置了此查询,但无法查询 return 金额。
如果有人能帮助我,我将不胜感激。
您的逻辑有缺陷,子查询与主查询中的 branch/date 之间没有关联,因此它们合计了所有内容。使用条件聚合
展示差异
SELECT
FORMAT(A.ShipDate, 'yyyy-MM') as mes,
B.Name,
(SELECT count(Status) FROM [sales].[SalesOrderHeader] where Status = 5) as TrProcess,
sum(case when status = 5 then 1 else 0 end) as trtprocesstrue
FROM [sales].[SalesOrderHeader] A
INNER JOIN [sales].[SalesTerritory] B on A.TerritoryID = B.TerritoryID
where b.name = 'northwest'
GROUP BY b.name,FORMAT(A.ShipDate, 'yyyy-MM') ;
mes Name TrProcess trtprocesstrue
---------- -------------------------------------------------- ----------- --------------
2005-07 Northwest 31465 20
2005-08 Northwest 31465 28
2005-09 Northwest 31465 21
2005-10 Northwest 31465 22
Using the SalesOrderHeader and SalesTerritory tables, write a query to calculate the number of transactions and total monthly amount per territory for each of the states based on the Status field. Status values are: 1 = In process ; 2 = Approved ; 3 = Backordered ; 4 = Rejected ; 5 = Shipped ; 6 = Canceled The result table must contain the following fields:
- 月份:YYYY-MM 格式的月份
- NameTerritory : 领土名称
- TrProcess:处理中的交易(数量)
- TrApproved:批准的交易(数量)
- TrBackordered:逾期交易(数量)
- TrRejected:被拒绝的交易(数量)
- TrShipped:发送的交易(数量)
- TrCanceled:交易取消(数量)
- MntProcess:处理中的总量
- MntApproved:已批准的总金额
- MntBackordered:欠款总额
- MntRejected:拒绝总金额
- MntShipped:发送的总数量
- MntCanceled:已取消的总金额
SELECT
FORMAT(A.ShipDate, 'yyyy-MM') as Mes,
B.Name,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 1) as TrProcess,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 2) as TrApproved,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 3) as TrBackordered,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 4) as TrRejected,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 5) as TrShipped,
(SELECT count(Status) FROM [dbo].[SalesOrderHeader] where Status = 6) as TrCanceled,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 1) as MntProcess,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 2) as MntApproved,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 3) as MntBackordered,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 4) as MntRejected,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 5) as MntShipped,
(SELECT SUM(TotalDue) FROM [dbo].[SalesOrderHeader] where Status = 6) as MntCanceled
FROM [dbo].[SalesOrderHeader] A
INNER JOIN [dbo].[SalesTerritory] B on A.TerritoryID = B.TerritoryID
GROUP BY FORMAT(A.ShipDate,'yyyy/MM')
我设置了此查询,但无法查询 return 金额。 如果有人能帮助我,我将不胜感激。
您的逻辑有缺陷,子查询与主查询中的 branch/date 之间没有关联,因此它们合计了所有内容。使用条件聚合 展示差异
SELECT
FORMAT(A.ShipDate, 'yyyy-MM') as mes,
B.Name,
(SELECT count(Status) FROM [sales].[SalesOrderHeader] where Status = 5) as TrProcess,
sum(case when status = 5 then 1 else 0 end) as trtprocesstrue
FROM [sales].[SalesOrderHeader] A
INNER JOIN [sales].[SalesTerritory] B on A.TerritoryID = B.TerritoryID
where b.name = 'northwest'
GROUP BY b.name,FORMAT(A.ShipDate, 'yyyy-MM') ;
mes Name TrProcess trtprocesstrue
---------- -------------------------------------------------- ----------- --------------
2005-07 Northwest 31465 20
2005-08 Northwest 31465 28
2005-09 Northwest 31465 21
2005-10 Northwest 31465 22