我有一个返回一些行的表,我们正在查看的是仅当rxID匹配时,以及那些amountPaid是否相反。
在屏幕截图中,我们有两个rxID为3350593的条目,其支付金额为2.35和-2.35。这是一种货币类型。
到目前为止,我的查询只返回那两行,因为它匹配rxId。 y.amountpaid在底部,无法找到,并且t.amountpaid不能仅在被t.rxid分组时被选中。
如果我可以获取查询以删除具有相同rxID和反向数量的paids的那两行,我将非常感激。
SELECT x.*
FROM
(
SELECT IsInstyRxFlag, pri.payerReceiptItemId, CONVERT(varchar,rx.oeDate, 101) rxOeDate, CONVERT(DECIMAL(6,2), drx.payerOblig) payerObligation, CONVERT(DECIMAL(6,2), pri.amountPaid) amountPaid, pri.rxId, CONVERT(DECIMAL(6,2), (pri.amountPaid - drx.payerOblig)) difference, rxeventid
FROM PayerReceiptItem pri
JOIN Rx ON rx.rxId = pri.rxId
JOIN DispenseRx drx ON rx.rxId = drx.rxId
WHERE CONVERT(DECIMAL(6,2), drx.payerOblig) <> CONVERT(DECIMAL(6,2), pri.amountPaid) AND payerReceiptID = 19781
) x
JOIN (SELECT t.rxID
FROM
(
SELECT IsInstyRxFlag, pri.payerReceiptItemId, CONVERT(varchar,rx.oeDate, 101) rxOeDate, CONVERT(DECIMAL(6,2), drx.payerOblig) payerObligation, CONVERT(DECIMAL(6,2), pri.amountPaid) amountPaid, pri.rxId, CONVERT(DECIMAL(6,2), (pri.amountPaid - drx.payerOblig)) difference, rxeventid
FROM PayerReceiptItem pri
JOIN Rx ON rx.rxId = pri.rxId
JOIN DispenseRx drx ON rx.rxId = drx.rxId
WHERE CONVERT(DECIMAL(6,2), drx.payerOblig) <> CONVERT(DECIMAL(6,2), pri.amountPaid) AND payerReceiptID = 19781
) t
GROUP BY t.rxID
HAVING COUNT(t.rxID) > 1) y ON y.rxID = x.rxid
--can't find y.amountpaid, x can be seen
--and y.amountPaid = x.amountPaid
现在使用它,但x.amount支付无效,因为它不是聚合函数。
with
x AS (SELECT IsInstyRxFlag
, pri.payerReceiptItemId
, CONVERT(varchar,rx.oeDate, 101) rxOeDate
, CONVERT(DECIMAL(6,2), drx.payerOblig) payerObligation
, CONVERT(DECIMAL(6,2), pri.amountPaid) amountPaid
, pri.rxId
, CONVERT(DECIMAL(6,2), (pri.amountPaid - drx.payerOblig)) difference
, rxeventid
FROM PayerReceiptItem pri
JOIN Rx ON rx.rxId = pri.rxId
JOIN DispenseRx drx ON rx.rxId = drx.rxId
WHERE CONVERT(DECIMAL(6,2), drx.payerOblig) <> CONVERT(DECIMAL(6,2), pri.amountPaid) AND payerReceiptID = 19781)
SELECT x.*
FROM x
where not exists(
SELECT *
FROM x AS y
WHERE x.amountpaid = -1* y.amountpaid
AND x.difference = x.amountpaid
AND x.rxid = y.rxid
);
答案 0 :(得分:1)
您期望得到什么结果?我简化了你的查询,看看这个:
with
x AS (SELECT IsInstyRxFlag
, pri.payerReceiptItemId
, CONVERT(varchar,rx.oeDate, 101) rxOeDate
, CONVERT(DECIMAL(6,2), drx.payerOblig) payerObligation
, CONVERT(DECIMAL(6,2), pri.amountPaid) amountPaid
, pri.rxId
, CONVERT(DECIMAL(6,2), (pri.amountPaid - drx.payerOblig)) difference
, rxeventid
FROM PayerReceiptItem pri
JOIN Rx ON rx.rxId = pri.rxId
JOIN DispenseRx drx ON rx.rxId = drx.rxId
WHERE CONVERT(DECIMAL(6,2), drx.payerOblig) <> CONVERT(DECIMAL(6,2), pri.amountPaid) AND payerReceiptID = 19781)
SELECT x.*
FROM x
JOIN (
SELECT t.rxID -- how you want to find amountpaid if you don't select this
--you can select max or min from amountpaid, to not group by this field
, min(amountpaid) min_amountpaid, max(amountpaid) max_amountpaid
FROM x t
GROUP BY t.rxID
HAVING COUNT(t.rxID) > 1) y
ON y.rxID = x.rxid
修改强> 但如果您只需要排除这两行,请尝试以下查询:
with
x AS (SELECT IsInstyRxFlag
, pri.payerReceiptItemId
, CONVERT(varchar,rx.oeDate, 101) rxOeDate
, CONVERT(DECIMAL(6,2), drx.payerOblig) payerObligation
, CONVERT(DECIMAL(6,2), pri.amountPaid) amountPaid
, pri.rxId
, CONVERT(DECIMAL(6,2), (pri.amountPaid - drx.payerOblig)) difference
, rxeventid
FROM PayerReceiptItem pri
JOIN Rx ON rx.rxId = pri.rxId
JOIN DispenseRx drx ON rx.rxId = drx.rxId
WHERE CONVERT(DECIMAL(6,2), drx.payerOblig) <> CONVERT(DECIMAL(6,2), pri.amountPaid) AND payerReceiptID = 19781)
SELECT x.*
FROM x
WHERE NOT EXISTS (SELECT 1 FROM x t WHERE t.rxID = x.rxID and t.amountPaid = -1 * x.amountPaid )