【发布时间】:2014-05-21 11:07:55
【问题描述】:
我有 3 张桌子:
表 Invoices(Invoice, InvoiceAmount(float), Other infos ...), table Payments(Payment, PaymentAmount(float), Other infos ...)和表 PaymentsDet(ID、发票、付款、金额(浮动))。 表 PaymentsDet 将发票和带有金额的付款(由该付款支付的发票部分)链接起来。
我需要一个查询来返回有关每张发票的信息 +
IF(该发票恰好有 1 笔付款)
付款、SUM(PayementsDet.Amount)、其他付款信息...
ELSE(超过 1 笔付款或根本不付款)
Count(Payment)、SUM(PayementsDet.Amount)、用NULL值或'' /strong>。
感谢您的宝贵时间,并希望有足够聪明的人可以帮助我。
编辑:
SELECT Factures.Facture, Factures.Client, Factures.DateFacture, Factures.MoisFacture, Factures.DateRéception, Factures.Echéance, Factures.Montant, Factures.TxTVA,
Factures.Activité,
(SELECT CASE WHEN SUM(Montant) IS NULL THEN '0' ELSE SUM(Montant) END AS Expr1
FROM RèglementsDet
WHERE (Facture = Factures.Facture) AND (Validé = 1)) AS MontantRegl,
(SELECT CASE WHEN COUNT(DISTINCT Règlements.Règlement) > '1' THEN COUNT(DISTINCT Règlements.Règlement)
WHEN COUNT(DISTINCT Règlements.Règlement) = '1' THEN
(SELECT MIN(Règlements.Règlement) AS Expr1
FROM Règlements INNER JOIN
RèglementsDet ON Règlements.Règlement = RèglementsDet.Règlement
WHERE (RèglementsDet.Facture = Factures.Facture)) END AS Règlement
FROM Règlements INNER JOIN
RèglementsDet AS RèglementsDet_2 ON Règlements.Règlement = RèglementsDet_2.Règlement
WHERE (RèglementsDet_2.Facture = Factures.Facture) AND (RèglementsDet_2.Validé = 1)) AS Règlement
FROM Factures LEFT OUTER JOIN
RèglementsDet AS RèglementsDet_1 ON Factures.Facture = RèglementsDet_1.Facture
GROUP BY Factures.Facture, Factures.Client, Factures.DateFacture, Factures.MoisFacture, Factures.DateRéception, Factures.Echéance, Factures.Montant, Factures.TxTVA,
Factures.Activité
如果有人得到更好(更易读)的查询,我想我想通了。
【问题讨论】:
-
您应该尽可能向我们展示您的查询,并告诉我们您遇到的问题,以便我们为您提供适当的提示如何完成它。