国家间贸易差额 - Northwind 数据库
Trading balance between countries - Northwind database
我有很多任务要做,使用旧的 Northwind 数据库,我需要为每个国家/地区创建一个贸易余额。这意味着,我需要计算每个国家向每个国家销售了多少,他们购买了多少以及余额是多少,正负。
我有一个包含“CountryID, CountryName, ContinentID
”列的 table Countries
。
我更希望国家以其原始名称命名,而不是 CountryID
.
我有 table Suppliers
列 SupplierID
匹配 CountryId
。 Customers
table、CustomerID
和 CountryId.
也是如此
到目前为止我有这个:
SELECT c.CountryID, SUM(CONVERT(money, (od.UnitPrice * od.Quantity) * (1 - od.Discount) / 100) * 100) AS ProductSales
From Countries c
LEFT OUTER JOIN Suppliers s ON c.CountryID = s.CountryID
JOIN Customers cu ON c.CountryID = cu.CountryID
JOIN Products p ON s.SupplierID = p.SupplierID
JOIN Orders o ON cu.CustomerID = o.CustomerID
JOIN [Order Details] od ON o.OrderID = od.OrderID
GROUP BY c.CountryID
结果:
9 2072561.58
6 65322.04
7 56430.15
4 106925.77
19 412799.24
13 17205.45
5 200785.20
16 35966.40
17 272475.70
11 78850.80
20 2947015.56
8 406791.55
我认为它没有给我任何接近我需要的东西,可能这只是所有已售产品的价值。我坚持执行此任务,我会提供您需要的任何信息。
它将如下所示(抱歉,我的本地机器上没有安装此数据库)。可能会有帮助-
select
fc.Countryname as FromCountryName,
tc.Countryname as ToCountryName,
SUM(CONVERT(money, (od.UnitPrice * od.Quantity) * (1 - od.Discount) / 100) * 100)
from Suppliers as s
inner join Countries as fc on fc.id = s.countryid
inner join Products as p on p.supplierid = s.id
inner join [Order Details] as od on od.productid = p.id
inner join Orders as o on o.id = od.OrderId
inner join Customers as cu on cu.id = o.customerId
inner join Countries as tc on tc.id = cu.countryid
group by fc.Countryname, tc.Countryname
我们有 Group By
FromCountry
和 ToCountry
的数据。
对于下面评论的第二个要求(也许下面的查询在性能方面效率不高)-
select
max(fc.name) as CountryA,
min(tc.name) as CountryB,
BalanceAB = sum(case when fc.name > tc.name then (CONVERT(money, (od.UnitPrice * od.Quantity) * (1 - od.Discount) / 100) * 100) end),
BalanceBA = sum(case when fc.name < tc.name then (CONVERT(money, (od.UnitPrice * od.Quantity) * (1 - od.Discount) / 100) * 100) end),
SUM((case when fc.name < tc.name then -1 else 1 end) * CONVERT(money, (od.UnitPrice * od.Quantity) * (1 - od.Discount) / 100) * 100) as [Total(A - B)]
from @Supplier as s
inner join @Country as fc on fc.id = s.countryid
inner join @Product as p on p.supplierid = s.id
inner join @OrderDetail as od on od.productid = p.id
inner join @Order as o on o.id = od.OrderId
inner join @Customer as cu on cu.id = o.customerId
inner join @Country as tc on tc.id = cu.countryid
group by (case when fc.name < tc.name then fc.name+' to '+tc.name else tc.name+' to '+fc.name end)
输出-
CountryA CountryB BalanceAB BalanceBA Total(A - B)
US UK 20.00 200.00 -180.00
我有很多任务要做,使用旧的 Northwind 数据库,我需要为每个国家/地区创建一个贸易余额。这意味着,我需要计算每个国家向每个国家销售了多少,他们购买了多少以及余额是多少,正负。
我有一个包含“CountryID, CountryName, ContinentID
”列的 table Countries
。
我更希望国家以其原始名称命名,而不是 CountryID
.
我有 table Suppliers
列 SupplierID
匹配 CountryId
。 Customers
table、CustomerID
和 CountryId.
到目前为止我有这个:
SELECT c.CountryID, SUM(CONVERT(money, (od.UnitPrice * od.Quantity) * (1 - od.Discount) / 100) * 100) AS ProductSales
From Countries c
LEFT OUTER JOIN Suppliers s ON c.CountryID = s.CountryID
JOIN Customers cu ON c.CountryID = cu.CountryID
JOIN Products p ON s.SupplierID = p.SupplierID
JOIN Orders o ON cu.CustomerID = o.CustomerID
JOIN [Order Details] od ON o.OrderID = od.OrderID
GROUP BY c.CountryID
结果:
9 2072561.58
6 65322.04
7 56430.15
4 106925.77
19 412799.24
13 17205.45
5 200785.20
16 35966.40
17 272475.70
11 78850.80
20 2947015.56
8 406791.55
我认为它没有给我任何接近我需要的东西,可能这只是所有已售产品的价值。我坚持执行此任务,我会提供您需要的任何信息。
它将如下所示(抱歉,我的本地机器上没有安装此数据库)。可能会有帮助-
select
fc.Countryname as FromCountryName,
tc.Countryname as ToCountryName,
SUM(CONVERT(money, (od.UnitPrice * od.Quantity) * (1 - od.Discount) / 100) * 100)
from Suppliers as s
inner join Countries as fc on fc.id = s.countryid
inner join Products as p on p.supplierid = s.id
inner join [Order Details] as od on od.productid = p.id
inner join Orders as o on o.id = od.OrderId
inner join Customers as cu on cu.id = o.customerId
inner join Countries as tc on tc.id = cu.countryid
group by fc.Countryname, tc.Countryname
我们有 Group By
FromCountry
和 ToCountry
的数据。
对于下面评论的第二个要求(也许下面的查询在性能方面效率不高)-
select
max(fc.name) as CountryA,
min(tc.name) as CountryB,
BalanceAB = sum(case when fc.name > tc.name then (CONVERT(money, (od.UnitPrice * od.Quantity) * (1 - od.Discount) / 100) * 100) end),
BalanceBA = sum(case when fc.name < tc.name then (CONVERT(money, (od.UnitPrice * od.Quantity) * (1 - od.Discount) / 100) * 100) end),
SUM((case when fc.name < tc.name then -1 else 1 end) * CONVERT(money, (od.UnitPrice * od.Quantity) * (1 - od.Discount) / 100) * 100) as [Total(A - B)]
from @Supplier as s
inner join @Country as fc on fc.id = s.countryid
inner join @Product as p on p.supplierid = s.id
inner join @OrderDetail as od on od.productid = p.id
inner join @Order as o on o.id = od.OrderId
inner join @Customer as cu on cu.id = o.customerId
inner join @Country as tc on tc.id = cu.countryid
group by (case when fc.name < tc.name then fc.name+' to '+tc.name else tc.name+' to '+fc.name end)
输出-
CountryA CountryB BalanceAB BalanceBA Total(A - B)
US UK 20.00 200.00 -180.00