I SQLite får du en autoinkrementerende kolonne ved at bruge INTEGER PRIMÆR NØGLE
.(Og hvis du stoler på de faktiske værdier af payers.id
i forespørgslen, skal du give dem eksplicit.)
Du bør ikke forsøge at indlejre joins, når det ikke er nødvendigt:
SELECT payer_payment.payer_id,
Sum(payer_payment.amount) AS total_paid,
Sum(payer_payment.pays * payments_share.single_share) AS fair_share
FROM payers
INNER JOIN payer_payment
ON payers.id = payer_payment.payer_id
INNER JOIN (SELECT payment_id,
Sum(amount) / Sum(pays) AS single_share
FROM payer_payment
GROUP BY payment_id) AS payments_share
ON payer_payment.payment_id = payments_share.payment_id
WHERE payers.user_id = 1
GROUP BY payer_payment.payer_id;