我有 4 个表需要连接,其中 3 个需要计算,它们的值在第四个表中提供。在连接中,有 2 个字段很重要,需要进行分组。
第一张表:
SELECT customerId, baseCurrencyId, totalOldDebt, totalRecoveredDebt, totalPaidDebt
FROM ViewCustomerDebt
第一次查询的结果:
客户ID | 基础货币ID | 旧债总额 | 已收回债务总额 | 已偿债务总额 |
---|---|---|---|---|
2 | 1 | 2000 | 950 | 0 |
4 | 1 | 0 | 10 | 0 |
1 | 2 | 100 | 100 | 0 |
2 | 2 | 300 | 500 | 1000 |
第二张表:
SELECT customerId ,paymentTypeId ,baseCurrencyId ,subTotalPrice ,totalPrice ,totalCost
FROM ViewCustomerSell
第二次查询的结果:
客户ID | 付款类型Id | 基础货币ID | 小计价格 | 总价 | 总成本 |
---|---|---|---|---|---|
1 | 1 | 1 | 5300 | 5300 | 5008 |
2 | 1 | 1 | 292.5 | 287.5 | 181 |
2 | 2 | 1 | 20638.5 | 20538.5 | 492 |
2 | 2 | 2 | 10 | 10 | 1000 |
3 | 1 | 1 | 174.5 | 174.5 | 160.5 |
3 | 2 | 1 | 1172 | 1172 | 6016 |
4 | 1 | 1 | 661 | 661 | 626 |
4 | 2 | 1 | 144 | 144 | 132.25 |
第三个查询:
SELECT customerId ,paymentTypeId ,baseCurrencyId ,totalPrice ,totalCost
FROM ViewCustomerPurchase
第三次查询的结果:
客户ID | 付款类型Id | 基础货币ID | 总价 | 总成本 |
---|---|---|---|---|
2 | 2 | 1 | 91610 | 0 |
3 | 2 | 1 | 115.5 | 0 |
paymentTypeId
和ViewCustomerSell
中的ViewCustomerDebt
是1
或2
。
然后我想从 Customer
表中进行选择并获取这些列:
customerId
baseCurrencyId,
totalPaidSellPrice (ViewCustomerSell.totalPrice where paymentTypeId = 1),
totalDebtSellPrice (ViewCustomerSell.totalPrice where paymentTypeId = 2),
totalSellDebt ((ViewCustomerSell.totalPrice where paymentTypeId = 2) + ViewCustomerDebt.totalOldDebt - ViewCustomerDebt.totalRecoveredDebt),
totalPaidPurchasePrice (ViewCustomerPurchase.totalPrice where paymentTypeId = 1),
totalDebtPurchasePrice (ViewCustomerPurchase.totalPrice where paymentTypeId = 2),
totalPurchaseDebt ((ViewCustomerPurchase.totalPrice where paymentTypeId = 2) - ViewCustomerDebt.totalPaidDebt),
totalDebtExchange (totalSellDebt - totalPurchaseDebt)
但我希望它们全部按
customerId
和 baseCurrencyId
分组。
这是我的预期结果:
客户ID | 基础货币ID | 总付费销售价格 | 总债务出售价格 | 总销售债务 | 总付费购买价格 | 总债务购买价格 | 购买债务总额 | 总债务交换 |
---|---|---|---|---|---|---|---|---|
2 | 1 | 287.5 | 20538.5 | 21588.5 | 0 | 91610 | 91610 | -70021.5 |
4 | 1 | 661 | 144 | 134 | 0 | 0 | 0 | 134 |
1 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 2 | 0 | 10 | -190 | 0 | 0 | -1000 | -1190 |
3 | 1 | 174.5 | 1172 | 1172 | 0 | 115.5 | 115.5 | 1056.5 |
1 | 1 | 5300 | 0 | 0 | 0 | 0 | 0 | 0 |
等等。
我尝试了这段代码,但它没有按我想要的方式工作:
SELECT C.id as customerId,
COALESCE (VCD.baseCurrencyId, VCSP.baseCurrencyId, VCSD.baseCurrencyId, VCPP.baseCurrencyId, VCPD.baseCurrencyId)
AS baseCurrencyId,
VCSP.totalPrice as totalPaidSellPrice,
VCSD.totalPrice as totalDebtSellPrice,
VCSD.totalPrice + VCD.totalOldDebt - VCD.totalRecoveredDebt as totalSellDebt,
VCPP.totalPrice as totalPaidPurchasePrice,
VCPD.totalPrice as totalDebtPurchasePrice,
VCPD.totalPrice - VCD.totalPaidDebt as totalPurchaseDebt,
(VCSD.totalPrice + VCD.totalOldDebt - VCD.totalRecoveredDebt) -
(VCPD.totalPrice - VCD.totalPaidDebt) as totalExchange
from ViewCustomerDebt as VCD
Full outer join ViewCustomerSell as VCSP on VCD.customerId = VCSP.customerId and
VCD.baseCurrencyId = VCSP.baseCurrencyId and VCSP.paymentTypeId = 1
Full outer join ViewCustomerSell as VCSD on VCD.customerId = VCSD.customerId
and VCD.baseCurrencyId = VCSD.baseCurrencyId and VCSD.paymentTypeId = 2
Full outer join ViewCustomerPurchase as VCPP on VCD.customerId = VCPP.customerId and
VCD.baseCurrencyId = VCPP.baseCurrencyId and VCPP.paymentTypeId = 1
Full outer join ViewCustomerPurchase as VCPD on VCD.customerId = VCPD.customerId
and VCD.baseCurrencyId = VCPD.baseCurrencyId and VCPD.paymentTypeId = 2
INNER JOIN dbo.Customer AS C ON C.id =
COALESCE (VCD.customerId, VCSP.customerId, VCSD.customerId, VCPP.customerId, VCPD.customerId)
这是一个数据库小提琴:https://dbfiddle.uk/uGO8az0s
您想要每个 customerId 和 baseCurrencyId 一行的结果。因此,对于这两列上没有唯一约束的所有表,您必须首先聚合表,以便为每个 customerId 和 baseCurrencyId 生成一行。到达那里后,您可以将这两列的结果连接起来。在下面的查询中,我假设聚合对于您的所有视图都是必要的。
WITH
debt AS
(
SELECT
customerid,
basecurrencyid,
SUM(totalolddebt) AS old,
SUM(totalrecovereddebt) AS recovered,
SUM(totalpaiddebt) AS paid
FROM viewcustomerdebt
GROUP BY customerid, basecurrencyid
),
sell AS
(
SELECT
customerid,
basecurrencyid,
SUM(CASE WHEN paymenttypeid = 1 THEN totalprice END) AS price_type1,
SUM(CASE WHEN paymenttypeid = 2 THEN totalprice END) AS price_type2
FROM viewcustomersell
GROUP BY customerid, basecurrencyid
),
purchase AS
(
SELECT
customerid,
basecurrencyid,
SUM(CASE WHEN paymenttypeid = 1 THEN totalprice END) AS price_type1,
SUM(CASE WHEN paymenttypeid = 2 THEN totalprice END) AS price_type2
FROM viewcustomerpurchase
GROUP BY customerid, basecurrencyid
)
SELECT
COALESCE(debt.customerid, sell.customerid, purchase.customerid) AS customerid,
COALESCE(debt.basecurrencyid, sell.basecurrencyid, purchase.basecurrencyid) AS basecurrencyid,
sell.price_type1 AS total_paid_sell_price,
sell.price_type2 AS total_debt_sell_price,
sell.price_type2 + debt.old - debt.recovered AS total_sell_debt,
purchase.price_type1 AS total_paid_purchase_price,
purchase.price_type2 AS total_debt_purchase_price,
purchase.price_type2 - debt.paid AS total_purchase_debt,
(sell.price_type2 + debt.old - debt.recovered) - (purchase.price_type2 - debt.paid) AS total_debt_exchange
FROM debt
FULL OUTER JOIN sell ON sell.customerid = debt.customerid
AND sell.basecurrencyid = debt.basecurrencyid
FULL OUTER JOIN purchase ON purchase.customerid = COALESCE(debt.customerid, sell.customerid)
AND purchase.basecurrencyid = COALESCE(debt.basecurrencyid, sell.basecurrencyid)
ORDER BY
COALESCE(debt.customerid, sell.customerid, purchase.customerid),
COALESCE(debt.basecurrencyid, sell.basecurrencyid, purchase.basecurrencyid);
如果 SQL Server 符合标准 SQL 并支持
USING
子句,则联接将如下所示:
FROM debt
FULL OUTER JOIN sell USING (customerid, basecurrencyid)
FULL OUTER JOIN sell ON USING (customerid, basecurrencyid)
和笨手笨脚的人
COALESCE(debt.customerid, sell.customerid, purchase.customerid)
COALESCE(debt.basecurrencyid, sell.basecurrencyid, purchase.basecurrencyid)
会变得单纯
customerid
basecurrencyid