SELECT
SUM(CASE
WHEN db1.card_type = 'debit' THEN 1
ELSE 0
END) AS DEBIT,
SUM(CASE
WHEN db1.card_type = 'credit' THEN 1
ELSE 0
END) AS CREDIT,
DATE_FORMAT(Latest, '%Y-%m') AS Dato
FROM
(SELECT
pp.debtor_id, pp.card_type, MAX(pp.created) AS Latest
FROM
capital.credit_card cc
JOIN capital.card_to_debtor ctd ON cc.id = ctd.card_id
AND debtor_id IN (SELECT
fs.debtor_id, fs.created, fs.reason
FROM
analysis.full_settlement fs
WHERE
((reason = 'BANK_PAYMENT'
OR fs.reason = 'CARD_PAYMENT')
AND fs.amount < 0)
OR fs.REASON = 'TRIVIAL_BALANCE'
OR fs.id = 13793327
OR fs.id = 7451808)
JOIN analysis.full_settlement fs ON fs.debtor_id = ctd.debtor_id
JOIN (SELECT
p.debtor_id, ccc.card_type, p.created
FROM
capital.payment p
JOIN capital.card_subscription cs ON cs.id = p.subscription_id
JOIN (SELECT
cc.id, ctd.card_id, cc.card_type, ctd.debtor_id
FROM
capital.card_to_debtor ctd
JOIN capital.credit_card cc ON cc.id = ctd.card_id) ccc ON ccc.card_id = cs.card_id
AND ccc.debtor_id = p.debtor_id) pp ON fs.debtor_id = pp.debtor_id
AND pp.created <= fs.created
GROUP BY pp.debtor_id , fs.created) db
JOIN
(SELECT
p.debtor_id, ccc.card_type, p.created
FROM
capital.payment p
JOIN capital.card_subscription cs ON cs.id = p.subscription_id
JOIN (SELECT
cc.id, ctd.card_id, cc.card_type, ctd.debtor_id
FROM
capital.card_to_debtor ctd
JOIN capital.credit_card cc ON cc.id = ctd.card_id) ccc ON ccc.card_id = cs.card_id
AND ccc.debtor_id = p.debtor_id) db1 ON db1.debtor_id = db.debtor_id
AND db1.created = db.Latest
GROUP BY YEAR(Latest) , MONTH(Latest)
答案 0 :(得分:1)
debtor_id IN (SELECT
fs.debtor_id, fs.created, fs.reason
debtor_id
是一列,因此您只能将其与其他列进行比较。将其更改为
debtor_id IN (SELECT
fs.debtor_id FROM...