Di SQLite, Anda mendapatkan kolom peningkatan otomatis dengan menggunakan INTEGER PRIMARY KEY
.(Dan jika Anda mengandalkan nilai sebenarnya dari payers.id
dalam kueri, Anda harus memberikannya secara eksplisit.)
Anda tidak boleh mencoba menggabungkan gabungan saat tidak diperlukan:
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;