mysql 像校验码一样求和的子查询
mysql subquery to sum like checknumbers
我有两个表,一个有 ID 和我正在检查重复的字段的串联,另一个有 ID 和所有其他分类数据,一个项目是 AmountPaid。以下查询:
Select
i.id,
i.CheckNumber,
AmountPaid
from
HS i where i.id in (
SELECT id
FROM HS_dups
WHERE concatckBatch IN (SELECT *
FROM (SELECT concatckBatch
FROM HS_dups
GROUP BY concatckBatch
HAVING COUNT(concatckBatch) > 1) AS a) AND concatckBatch != '')
给我这个答案:
id checkNumber amount
15 1114392 1708.5
16 1114392 1432.2
17 1114392 1188.06
18 1114392 1405.25
19 1114392 603.96
我需要它给我这个答案,它是所有相同的支票号码和对应的id的总和:
id checkNumber amount
15 1114392 6337.97
16 1114392 6337.97
17 1114392 6337.97
18 1114392 6337.97
19 1114392 6337.97
我尝试了各种子查询,但由于某种原因我受阻了。
已编辑:感谢 jpw:这是正确答案:
Select
i.id,
i.CheckNumber,
i2.AmountPaid
from
HS i join (
select
checknumber,
sum(amountpaid) AmountPaid
from HS
where id in (
SELECT id
FROM HS_dups
WHERE concatckBatch IN (
SELECT *
FROM (
SELECT concatckBatch
FROM HS_dups
GROUP BY concatckBatch
HAVING COUNT(concatckBatch) > 1
) AS a
) AND concatckBatch != ''
)
group by checknumber) i2 on i2.checkNumber = i.checkNumber
where
i.id in (
SELECT id
FROM HS_dups
WHERE concatckBatch IN (
SELECT *
FROM (
SELECT concatckBatch
FROM HS_dups
GROUP BY concatckBatch
HAVING COUNT(concatckBatch) > 1
) AS a
) AND concatckBatch != ''
)
您可以在派生的 table 中进行计算。查询应该是不言自明的。
Select
i.id,
i.CheckNumber,
i2.AmountPaid
from
HS i
join (
select
checknumber,
sum(amountpaid) AmountPaid
from HS
group by checknumber
) i2 on i2.checkNumber = i.checkNumber
where
i.id in (
SELECT id
FROM HS_dups
WHERE concatckBatch IN (
SELECT *
FROM (
SELECT concatckBatch
FROM HS_dups
GROUP BY concatckBatch
HAVING COUNT(concatckBatch) > 1
) AS a
) AND concatckBatch != ''
)
在其他数据库中,这可以使用窗口分析函数轻松实现,例如 sum(i.amountpaid) over (partition by i.checknumber)
但遗憾的是 MySQL 不支持该构造。
我有两个表,一个有 ID 和我正在检查重复的字段的串联,另一个有 ID 和所有其他分类数据,一个项目是 AmountPaid。以下查询:
Select
i.id,
i.CheckNumber,
AmountPaid
from
HS i where i.id in (
SELECT id
FROM HS_dups
WHERE concatckBatch IN (SELECT *
FROM (SELECT concatckBatch
FROM HS_dups
GROUP BY concatckBatch
HAVING COUNT(concatckBatch) > 1) AS a) AND concatckBatch != '')
给我这个答案:
id checkNumber amount
15 1114392 1708.5
16 1114392 1432.2
17 1114392 1188.06
18 1114392 1405.25
19 1114392 603.96
我需要它给我这个答案,它是所有相同的支票号码和对应的id的总和:
id checkNumber amount
15 1114392 6337.97
16 1114392 6337.97
17 1114392 6337.97
18 1114392 6337.97
19 1114392 6337.97
我尝试了各种子查询,但由于某种原因我受阻了。
已编辑:感谢 jpw:这是正确答案:
Select
i.id,
i.CheckNumber,
i2.AmountPaid
from
HS i join (
select
checknumber,
sum(amountpaid) AmountPaid
from HS
where id in (
SELECT id
FROM HS_dups
WHERE concatckBatch IN (
SELECT *
FROM (
SELECT concatckBatch
FROM HS_dups
GROUP BY concatckBatch
HAVING COUNT(concatckBatch) > 1
) AS a
) AND concatckBatch != ''
)
group by checknumber) i2 on i2.checkNumber = i.checkNumber
where
i.id in (
SELECT id
FROM HS_dups
WHERE concatckBatch IN (
SELECT *
FROM (
SELECT concatckBatch
FROM HS_dups
GROUP BY concatckBatch
HAVING COUNT(concatckBatch) > 1
) AS a
) AND concatckBatch != ''
)
您可以在派生的 table 中进行计算。查询应该是不言自明的。
Select
i.id,
i.CheckNumber,
i2.AmountPaid
from
HS i
join (
select
checknumber,
sum(amountpaid) AmountPaid
from HS
group by checknumber
) i2 on i2.checkNumber = i.checkNumber
where
i.id in (
SELECT id
FROM HS_dups
WHERE concatckBatch IN (
SELECT *
FROM (
SELECT concatckBatch
FROM HS_dups
GROUP BY concatckBatch
HAVING COUNT(concatckBatch) > 1
) AS a
) AND concatckBatch != ''
)
在其他数据库中,这可以使用窗口分析函数轻松实现,例如 sum(i.amountpaid) over (partition by i.checknumber)
但遗憾的是 MySQL 不支持该构造。