基本上我有两个名为Purchase
和PurchaseRefund
的表。
根据Purchase
,您可以拥有一个PurchaseRefund
但多个PurchaseId
行。
我要做的是编写一个带有参数@purchase_id
和@refund_id
假设我们有一个购买行,其TotalAmount
为10英镑,SubTotal
为10英镑
然后我们有两个PurchaseRefund
行......
216 - Has a refundAmount of £2.50
217 - Has a refundAmount of £2.25
因此,当查询以@refund_id
作为 216 运行时,它应显示
SubTotal: £10
Refund: £2.5
Total: £7.50
当查询以@refund_id
作为 217 运行时,它应显示
SubTotal: £7.50
Refund: £2.25
Total: £5.25
我希望这是有道理的
答案 0 :(得分:1)
您需要从Purchase
加入PurchaseRefund
并获取符合您条件的所有行 - 例如:
DECLARE @Purchase TABLE (PurchaseID INT, TotalAmount DECIMAL(10,2))
INSERT INTO @Purchase VALUES(42, 10.0)
DECLARE @PurchaseRefund TABLE (RefundID INT, PurchaseID INT, RefundAmount DECIMAL(10,2))
INSERT INTO @PurchaseRefund VALUES (216, 42, 2.50)
INSERT INTO @PurchaseRefund VALUES (217, 42, 2.25)
DECLARE @MaxRefundID INT = 217
SELECT
p.PurchaseID,
SUM(r.RefundAmount) AS 'Refund',
MAX(p.TotalAmount) - SUM(r.RefundAmount) AS 'Total'
FROM @Purchase p
INNER JOIN @PurchaseRefund r ON p.PurchaseID = r.PurchaseID
WHERE r.RefundId <= @MaxRefundID
GROUP BY p.PurchaseID
这项工作大部分都可以 - 我得到@MaxRefundID = 216
的值:
PurchaseID Refund Total
42 2.50 7.50
我得到@MaxRefundID = 217
的值:
PurchaseID Refund Total
42 4.75 5.25
答案 1 :(得分:0)
如果这是用于显示/报告,我会在其中执行运行总计,使用sql执行此操作是一个庞大的PIA。
SELECT
p.Amount,
r.Refund,
Sum(rPrevious.Refunds) As RunningTotal,
FROM Purchases p
Inner Join Refunds r On r.PurchaseId = p.PurchaseID And r.RefundID = @RefundID
Inner Join Refunds rPrevious on r.PurchaseID = p.PurchaseID AND rPrevious.RefundID < @RefundID
WHERE p.PurchaseID = @PurchaseID
Group By p.Amount,r.Refund
会给你216 10,2.50,0 和 217这样的10,2.25,2.5
Select (Amount - RunningTotal) as Subtotal,
Refund,
(Amount - Refund - RunningTotal) as Total
From
( SELECT
p.Amount,
r.Refund
Sum(rPrevious.Refunds) As RunningTotal,
FROM Purchases p
Inner Join Refunds r On r.PurchaseId = p.PurchaseID And r.RefundID = @RefundID
Inner Join Refunds rPrevious on rPrevious on r.PurchaseID = p.PurchaseID
AND rPrevious.RefundID < @RefundID
WHERE p.PurchaseID = @PurchaseID
Group By p.Amount,r.Refund
) RunningTotals
应该完成这项工作,但没有经过检查,我可能会对你的专栏名称采取一些许可。
理论虽然你想要的行是合理的 购买金额 - 退款总额&lt; @RefundID 退款金额 你的总数只有一个 - 另一个。或购买金额 - 退款总额&lt; = @RefundID
内部查询技巧只是为了获得退款总额而节省了很多麻烦?两次,一次用于小计,一次用于总计。
缺少括号...
答案 2 :(得分:0)
这建立在marc_s的解决方案之上,以获得您想要的确切数字。
DECLARE @Purchase TABLE (PurchaseID INT, TotalAmount DECIMAL(10,2))
INSERT INTO @Purchase VALUES(42, 10.0)
DECLARE @PurchaseRefund TABLE (RefundID INT, PurchaseID INT, RefundAmount DECIMAL(10,2))
INSERT INTO @PurchaseRefund VALUES (216, 42, 2.50)
INSERT INTO @PurchaseRefund VALUES (217, 42, 2.25)
DECLARE @RefundID int = 217
SELECT
p.TotalAmount - (SELECT SUM(RefundAmount) FROM @PurchaseRefund WHERE RefundID < r.RefundID) as SubTotal,
r.RefundAmount AS 'Refund',
p.TotalAmount - (SELECT SUM(RefundAmount) FROM @PurchaseRefund WHERE RefundID <= r.RefundID) as Total
FROM @Purchase p
INNER JOIN @PurchaseRefund r ON p.PurchaseID = r.PurchaseID
where r.RefundID = @RefundID
GROUP BY r.RefundID, p.TotalAmount, r.RefundAmount
答案 3 :(得分:0)
管理它可能不是最好但它有效....
DECLARE @subtotal DECIMAL(18,2)
SELECT @subtotal = COALESCE (SUM(PurchaseRefund.RefundAmount), 0)
FROM PurchaseRefund
WHERE PurchaseId = @PurchaseId
AND id < @PurchaseRefundId
SELECT pure.Id,
pure.ConsumerId,
pure.ConsumerCode,
pure.RetailerStoreId,
pure.RefundAmount,
pure.TimestampReceived,
pure.TimestampPayPalRequest,
pure.TimestampPayPalResponse,
pure.TimestampResponed AS TimestampResponded,
pure.RefundKey,
pure.ResponseCode,
pure.RetailerId,
reco.Headline AS OfferUsed,
retr.Name AS RetailerName,
rest.Name AS RetailerStoreName,
purc.CurrencyCode,
reco.DiscountType,
reco.DiscountValue,
purc.PayKey,
purc.TransactionId,
2 AS transactionType,
purc.isInStore,
purc.Total - @subtotal AS SubTotal,
purc.Total - @subtotal - pure.RefundAmount AS Total,
purc.TrackingId,
purc.ResponseCode,
pure.TerminalId,
pure.Id AS PurchaseRefundId,
purc.Id AS PurchaseId
FROM PurchaseRefund pure
INNER JOIN Purchase purc
ON pure.PurchaseId = purc.Id
INNER JOIN Retailer retr
ON purc.RetailerId = retr.Id
LEFT OUTER JOIN RetailerCoupon reco
ON purc.RetailerCouponId = reco.Id
LEFT OUTER JOIN RetailerStore rest
ON purc.RetailerStoreId = rest.id
WHERE purc.Id = @PurchaseId
AND pure.Id = @PurchaseRefundId
答案 4 :(得分:-1)
SELECT purchase.id, purchase.amount, sum(purchaseRefund.amount), (purchase.amount - sum(purchaseRefund.amount)) as total
FROM Purchase as purchase
LEFT JOIN PurchaseRefund as purchaseRefund on purchaseRefund.purchaseId = purchase.id
WHERE purchase.id = 216