0

尝试创建一个包含到期余额、利息费用、到期总额和付款计划的结果集。他们的方式我正在编码它似乎工作,但它似乎很不雅。有没有办法利用变量使代码更清晰?

SELECT 
    CustFName, CustLName, CustPhone, 
    SUM(InvoiceAmount - PaymentAmount) AS BalanceDue,
    SUM(InvoiceAmount - PaymentAmount)*.195 AS InterestCharge,
    SUM(InvoiceAmount - PaymentAmount) + SUM(InvoiceAmount - PaymentAmount) *.195 AS TotalDue,
    (SUM(InvoiceAmount - PaymentAmount) + SUM(InvoiceAmount - PaymentAmount) *.195)/4 AS PaymentPlan
FROM 
    Customer 
JOIN 
    Vehicle ON Customer.CustomerID = Vehicle.CustomerID
JOIN 
    Invoice ON Vehicle.VehicleID = Invoice.VehicleID
WHERE 
    InvoiceAmount - PaymentAmount > 400
GROUP BY 
    CustFName, CustLName, CustPhone
4

2 回答 2

0
DECLARE @p FLOAT
SET @p = 0.195
SELECT CustFName, CustLName, CustPhone, SUM(InvoiceAmount - PaymentAmount) AS BalanceDue,
    SUM(InvoiceAmount - PaymentAmount)*@p AS InterestCharge,
    SUM(InvoiceAmount - PaymentAmount) + SUM(InvoiceAmount - PaymentAmount) *@p AS TotalDue,
    (SUM(InvoiceAmount - PaymentAmount) + SUM(InvoiceAmount - PaymentAmount) *@p)/4 AS PaymentPlan

FROM Customer JOIN Vehicle
    ON Customer.CustomerID = Vehicle.CustomerID
    JOIN Invoice
    ON Vehicle.VehicleID = Invoice.VehicleID

WHERE InvoiceAmount - PaymentAmount > 400

GROUP BY CustFName, CustLName, CustPhone
于 2013-04-16T04:35:37.723 回答
0

使用虚拟表。除此之外,做得很好。:-)

SELECT 
    CustFName, CustLName, CustPhone, 
    BalanceDue,
    BalanceDue * Factor AS InterestCharge,
    BalanceDue + BalanceDue * Factor AS TotalDue,
    (BalanceDue + BalanceDue * Factor)/4 AS PaymentPlan
from (
        SELECT 
            CustFName, CustLName, CustPhone, 0.195 as Factor
            SUM(InvoiceAmount - PaymentAmount) AS BalanceDue
        FROM 
            Customer 
        JOIN 
            Vehicle ON Customer.CustomerID = Vehicle.CustomerID
        JOIN 
            Invoice ON Vehicle.VehicleID = Invoice.VehicleID
        WHERE 
            InvoiceAmount - PaymentAmount > 400
        GROUP BY 
            CustFName, CustLName, CustPhone
) as A;
于 2013-04-18T03:27:35.690 回答