After being helped very well by Stack community and learning a bit I created a query to show the sum of payments per client. A client can pay multiple times. I would like to know the sum of payments in a period per client. So far I managed to see different shelters, the sum of payments but only one client shows up per shelter instead of multiple clients.
This is the query. It's probably an easy thing for someone else, but I don't know where to start or how to search.
The db fiddle link is: https://www.db-fiddle.com/f/84VP3HEQLpiQgYEFEVn919/0
SELECT
Shelter,
Date,
ClientID,
Sum(Amount) as Amount
FROM (
SELECT
MAX(IF(FieldName = 'SHELTER', FieldValue, NULL)) AS Shelter,
SUM(IF(FieldName = 'Amount', FieldValue, NULL)) AS Amount,
Max(IF(FieldName = 'ClientID', FieldValue, NULL)) AS ClientID,
MAX(IF(FieldName = 'Date', FieldValue, NULL)) AS Month
FROM o2pe0_rsform_submission_values
WHERE FormId = 23
GROUP BY SubmissionId
) DerivedTable
GROUP BY Shelter
Required output from left to right in table:
Shelter | ClientID | Month | Amount
Bellville | A82QS3OK | 07-2020 | 200.1
Bellville | G993HJOS | 06-2020 | 500
**Bellville | G993HJOS | 08-2020 | 212.6**
Napier Street | BL9B2GDF | 07-2020 | 300
Progress update 2020-07-06: The answer from MickMackUSA works as an example script to answer the question about having the result: Sum per client per month and uses the field "Date" to extract information. https://www.db-fiddle.com/f/84VP3HEQLpiQgYEFEVn919/18
SELECT
Shelter,
YEAR(Date) AS Year,
Month(Date) AS Month,
ClientID,
FORMAT(SUM(Amount), 2) AS Amount
FROM (
SELECT
MAX(IF(FieldName = 'SHELTER', FieldValue, NULL)) AS Shelter,
SUM(IF(FieldName = 'Amount', FieldValue, NULL)) AS Amount,
Max(IF(FieldName = 'ClientID', FieldValue, NULL)) AS ClientID,
MAX(IF(FieldName = 'Date', FieldValue, NULL)) AS Date
FROM o2pe0_rsform_submission_values
WHERE FormId = 23
GROUP BY SubmissionId
) DerivedTable
GROUP BY Shelter, ClientId, YEAR(Date), Month(Date)
ORDER BY Shelter, ClientId, YEAR(Date), Month(Date)
However, I need to know the result per "Monthpayment". That has a date value (YYYY-MM-DD). The field "date" is to define the moment of payment and the field "Monthpayment" is to define for which month the payment is for. That's why I need an addition to the script. I do get the query error message: Error: ER_BAD_FIELD_ERROR: Unknown column "Monthpayment" in 'field list'.
https://www.db-fiddle.com/f/84VP3HEQLpiQgYEFEVn919/19
SELECT
Shelter,
Month(Monthpayment) AS Monthpayment,
ClientID,
FORMAT(SUM(Amount), 2) AS Amount
FROM (
SELECT
MAX(IF(FieldName = 'SHELTER', FieldValue, NULL)) AS Shelter,
SUM(IF(FieldName = 'Amount', FieldValue, NULL)) AS Amount,
Max(IF(FieldName = 'ClientID', FieldValue, NULL)) AS ClientID,
MAX(IF(FieldName = 'Monthpayment', FieldValue, NULL)) AS Month
FROM o2pe0_rsform_submission_values
WHERE FormId = 23
GROUP BY SubmissionId
) DerivedTable
GROUP BY Shelter, ClientId, Monthpayment
ORDER BY Shelter, ClientId, Monthpayment
What do I do wrong?
Progress update 2020-07-07 db-fiddle.com/f/84VP3HEQLpiQgYEFEVn919/20
The issue is almost solved:
MAX(IF(FieldName = 'Monthpayment', FieldValue, NULL)) AS Month
Should be
MAX(IF(FieldName = 'Monthpayment', FieldValue, NULL)) AS Monthpayment
However, the database sees two dates with July as different months. Dates 2020-07-06
and 2020-07-09
are seen as two different months.
47511
rows. The Date is08-07-2020
, but the Month is5-2020
. It looks like you have too much redundant and needless data in this table. These excess rows are going to impede the efficiency of your queries as your data volume swells. You should store Date as YYYY-MM-DD. You should ditchFormId
andSubmit
. I don't know that you needMonth
andMonth-selection
(maybe you have a good reason). Please repair or clarify this mcve: db-fiddle.com/f/84VP3HEQLpiQgYEFEVn919/4 @GartDate
instead ofMonth
? db-fiddle.com/f/84VP3HEQLpiQgYEFEVn919/5