Outputting the correct balance from payments in each row - mysql

This is probably simple. But I’m at a standstill. I am using Coldfusion 2021 on a Windows PC. I am trying to Output data that has the sum of payments made by a couple of individuals. This sum of payments is subtracted from a goal number of $425. When one of our members make a payment of say $5 dollars one day, then $20 another day, the output would total their payments to date of $25. But I want that payment to date to subtract from the static number of $425 within the output which should be a balance of $400 and so on. But I’m getting some wacky results in my balance column whenever I cfoutput my query. Anyone have any ideas that may point me in the right direction? Below is the code that I used and Below that are two images, one shows the incorrect balance column and the other one shows my desired balance column.
[![Below is the code that I used and Below that are two images, one shows the incorrect balance column and the other one shows my desired balance column.]

Because you have a hard-coded activityCost and activityDeposit (or obtained from elsewhere), there's no need to send them to the database at all. Just get the SUM() of the payments, then in your single-row-per-group output, do the math there, like:
#activityCost - activityDeposit - expense.balance#
And expense query's "balance" column, you might rename it after removing the unnecessary values to just "payments" since that's really what you're grabbing from the database.

Your queries are more complicated than necessary. This will get your data:
select fname, lname, sum(buspayamount) as AmountPaid
from name join bustrip on name.foiid = bustrip.busfoiid
where whatever, maybe a date range
group by fname, lname
This will display it:
<table>
<tr> table headers go here
</tr>
<cfoutput query = "nameofquery">
<tr>
<td> #fname# #lname#</td>
<td>$#AmountPaid#</td>
<td>$425-#AmountPaid#</td>
closing tags

Related

Trying to calculate the total amount of sales for each branch since the start of the year

I'm currently trying to solve a problem where I'm trying to calculate the total amount of sales for each store branch since the beginning of the year, within the database I've created.
I have tried to write a query that returns the result from one store e.g London, however, there are a few more branch_ids e.g. Manchester, Cardiff etc... At the moment it only returns the London store however I'm stuck on how to also print out the other branch_ids so all branch_ids display the total turnover for each individual store.
SELECT branch_id,
date_sale,
sum(cost_order)
FROM Customer_Orders
WHERE branch_id like '%London%'
AND date_sale >='2019-01-01';
I would use this version:
SELECT
branch_id,
SUM(cost_order) AS total
FROM Customer_Orders
WHERE date_sale BETWEEN '2019-01-01' AND '2019-12-31'
GROUP BY
branch_id;
Note that it does not make sense to include the date_sale field in your select clause, because each record in the output actually corresponds to multiple dates of sale. Also, I use BETWEEN to express the year 2019, because writing it this way is SARGable, meaning that an index on date_sale can be used.

Access Creating sum(iif to get the number of periods which apply

I'm trying to make a sum iif function which checks based on an employee's hire date and the ending pay period date whether they are part of the payroll period. So my idea was sum(iif([table1].[hiredate]<=[table2].[ppend],1,0))
While it works well for some, for some employees the number it gives is ridiculous. To give you an idea, there are 200 records for ppend, but it returns for some employees especially those who are well before the earliest ppend date, a number of 400 upward to 450. I'm also attempting to have it compare relative to the current date as well so I've used sum(iif(table1.hiredate<=table2.ppend<=date(),1,0)) but it largely leads to the same result. Could anybody help. Perhaps there is a factor I've neglected.
For table 1 the column data is in this order: employee ID, name, hire date and table 2 is payroll date, pp beg, pp end.

MySQL Date Handling Before a Given Date Table Join

I have two tables.
One contains information on a given reporting date and another has information on an incident date.
I’m trying to generate a list of the latest information before a given incident (i.e. if an incident occurred on 12/15/2015 and I have reports for 12/15/2014, 12/1/2015, and 1/12/2016, I want to pull the information for 12/1/2015 from that Data set. If on the second row there is another incident that occurred on 1/13/2016 and I have the same data as above I only want to return the information on 1/12/2016).
Dates are in a different column than ID.
Tables are in the following format
ID - DateV- ValueX
JAS 2017-12-15 00:00:00 3.45
I’ve tried running a query similar to below, but it only returns the first item ID-DateCombination not a list of all items. I would even be fine with just the latest reporting date by incident ID. That would give me enough to get the values I need.
Reporting Table - TableRep
Incident Table - TableInc
SELECT TableRep.ID, TableRep.DateV, TableRep.ValueX
FROM TableRep
INNER JOIN Table
ON TableInc.DateP > (
SELECT Max(TableRep.DateV) FROM TableRep;);
I’ve narrowed it down to only records before a given date in the table, but I’m struggling to lose the dates other than the latest ones.
Essentially, I would like to pull the id and Date from TableInc and the first date before the date indicated in TableInc from TableRep for each record in TableInc. Some IDs have multiple entries, so I can’t just pull them all and choose the max for each ID, that would only represent about 50% of the population.

spotfire multiple over statements in one custom expression

I have a table of travel expenses for analysis.
I would like to create a calculated column with a value for the maximum count of records with a certain category for each employee on any given day.
For example, if the category being reviewed is "dinner", we would like to know what is the maximum number of dinner transactions charged on any given day.
The following custom expression was able to count how many dinner expenses per employee:
count(If([Expense Type]="Dinner",[Expense Type],null)) over ([Employee])
But when trying to get the max count over days, I cant seem to get it to work. Here is the expression used:
Max(count(If([Expense Type]="Dinner",[Expense Type],null)) over ([Employee])) over (Intersect([Employee],[Transaction Date]))
This seems to provide the same answer as the first expression. Any idea on how to get this code to identify the value on the date with the most expenses for each employee?
If i understand your question and comments correctly, you should be able to use intersect.
count(If([Expense Type]="Dinner",[Expense Type],null)) over (Intersect([Transaction Date],[Employee]))
You may need to cast [Transaction Date] as a date if it is an actual DateTime. Otherwise you'd get one for each unique DT.

Trouble creating a SQL query

I've been thinking about how to compose this SQL query for a while now, but after thinking about it for a few hours I thought I'd ask the SO community to see if they have any ideas.
Here is a mock up of the relevant portion of the tables:
contracts
id
date
ar (yes/no)
term
payments
contract_id
payment_date
The object of the query is to determine, per month, how many payments we expect, vs how many payments we received.
conditions for expecting a payment
Expected payments begin on contracts.term months after contracts.date, if contracts.ar is "yes". Payments continue to be expected until the month after the first missed payment.
There is one other complication to this: payments might be late, but they need to show up as if they were paid on the date expected.
The data is all there, but I've been having trouble wrapping my head around the SQL query. I am not an SQL guru - I merely have a decent amount of experience handling simpler queries. I'd like to avoid filtering the results in code, if possible - but without your help that may be what I have to do.
Expected Output
Month Expected Payments Received Payments
January 500 450
February 498 478
March 234 211
April 987 789
...
SQL Fiddle
I've created an SQL Fiddle: http://sqlfiddle.com/#!2/a2c3f/2
Without writing up the query, I can give you the general idea:
In the contracts table, cast date + term in months, and group the result set by months where ar = 'YES'. This gives you the expected payments.
With the second table, cast payment_date in months and group by months for the number of received payments.
You can then join these two sub-results on month to get both pieces of information in one result set.