I have a Service
table and a Payments
table. I want my clients to be able to pay for one service with multiple payments, but when I try to create a form for making a payment, I'm unable to make a
"ServiceA"."Cost" - SUM("Payment"."Amount")
substraction
in my SELECT
query that would show the amount left to pay. I'm being told Strings cannot be added or subtracted in dialect 3
even though all the relevant fields are Numeric, with the same amount of decimal points and everything...
Any idea on what I might be doing wrong?
EDIT:
More details. The relevant tables are actually as follows:
ServiceA
, ServiceB
, ServiceC
, ServiceD
all have a numeric field Cost
. Their primary keys are actually foreign keys - they are all tied to an auto_increment BIGINT
field in a table For
. This is for the purpose of giving them auto_incrementing
, yet non-repeating IDs.
There's a table PS
(stands for Payment-Service
) functions as a intermediate table for a multi-multi relationship. It has two foreign keys - one from Payments
and one from For
. This gives me the option to have multiple payments for one service or to have one payment cover multiple services.
Additionally, every Service
table has a field referencing the ID of the client from "Clients".
In my SELECT
, I try to do the following:
SELECT "Clients"."Name" || ': ' || "ServiceA"."Name" || ' for ' || "ServiceA"."Cost" - SUM("Payments"."Amount") AS "To-Pay", "ID-Name" AS "ID"
FROM "ServiceA"
LEFT OUTER JOIN "Clients" ON "ServiceA"."Client" = "Clients"."ID-Client"
INNER JOIN "For" ON "ServiceA"."Id-ServA" = "For"."ID"
LEFT OUTER JOIN "PS" ON "For"."ID" = "PS"."For"
LEFT OUTER JOIN "Payments" ON "PS"."Payment" = "Payments"."For"
GROUP BY "To-Pay, "ID"
HAVING "PS"."Payment" IS NULL OR **SUM("Payments"."Amount") < "ServiceA"."Cost"
UNION ALL
(like the above but with other "Service" tables)
ORDER BY "To-Pay"
This is for a dropdown list in a Create Payment
form. It seems the problematic parts are the ones in bold.