I am trying to make an interest calculation for a loan. I have 2 tables, first table is the loan table that contains the loan amount, interest rate and other data. But interest rate changes daily and is stored on another table. So interest rate must be taken from loan tbl only if a payment is made the same day that loan is created.
Also I need to have amount remained after every payment that is store on tbl payment. So on every payment I need to fill the columns on table payment below. I tried to create a SQL statement to calculate interest rate and another to calculate the amount remained after the payment but cant combine them in one insert statement.
Example for loanid=1 , pay amount=$1000, loanid=29
SELECT
1, 1000 , date(),
(IIF(NOT ISNULL(t1.dailyinterest), t1.dailyinterest, t2.interestrate)
FROM
daily AS t1
RIGHT JOIN
loanable AS t2 ON (t1.loanid = t2.loanid AND t1.data = date())
WHERE
t2.loanid = 29)
SELECT
IIF(NOT ISNULL(pay.amountremained_tobepayed), pay.amountremained_tobepayed, 50000)
FROM
payment AS pay
RIGHT JOIN
(SELECT MAX(paymentid) AS id
FROM payment
WHERE loanid = 29) AS max2 ON pay.paymentid = max2.id
loan table
loanid
clientid
loanamount
interestrate
date
1
1
50000
0.2
05/04/20
daily interest table
clientid
loanid
date
dailyinterest
1
1
06/04/20
0.3
1
1
07/04/20
0.31
1
1
08/04/20
0.32
payment table
paymenid
loanid
amountpayed
paid_date
paid_interest
paid_pricipal
amountremained_tobepayed
Your query could look something like this.
parameters [loan id] int,
[client id] int,
[payment date] datetime,
[payment amount] currency;
insert into [payment table] (
-- payment table column names go here
)
select
-- parameters, names of columns in loan_table and
-- daily_interest_table, and calculated values go here
from loan_table
inner join daily_interest_table
on loan_table.loanid = daily_interest_table.loanid
and loan_table.clientid = daily_interest_table.clientid
where loan_table.loanid = [loan id]
and loan_table.clientid = [client id]
and daily_interest_table.date = [payment date];
I wrote below sql code but giving me error Syntax error missing operator in query
select 29, 1000 , date(), IIF(not IsNull(t1.dailyinterest) ,t1.dailyinterest, t2.interestrate) * 1000/100 AS inte, 1000 - inte as principal, amtremained
FROM daily AS t1
RIGHT JOIN loant AS t2
ON (t1.loanid = t2.loanid and t1.data= date())
inner join
(select IIF(not IsNull(pay.remainedamount), pay.remainedamount, 50000) - principal as amtremained
FROM paymentt AS pay RIGHT JOIN
(SELECT max(paymentid) AS id FROM paymentt where loanid=29) AS max2 ON pay.paymentid=max2.id)
on pay.loanid=t1.loanid
Related
I have two tables in MS Access. One for contracts, with contract id and total value. Another one for the payments made in each contract.
So for example when a client gets a new contract for a total value of USD 100.00 then he can pay it in any amount he want up to the total value, like two payments of 50.00 or one of 30.00 and another of 70.00 or even one hundred payments of 1.00.
And those payments go to the payments table with the contract id and the payment amount.
I need to make a query to find how much do they still have to pay for each contract. What is the net amount for each contract.
I managed to make a query that gives me the result I need, but if the contract is new and there are no payments on the payments table for this new contract, the net amount shown in my query is zero (blank field) and I needed it to show the total value of the contract instead.
For example
Contract of 100.00 with two payments of 10.00 and 15.00 should show the net value as 75.00
Contract of 100.00 with no payments should show net value as 100.00
Here is what I have so far:
'Query 1
SELECT Contracts.[Contract ID],
Sum(Payments.[Payment Value]
FROM Contracts
LEFT JOIN Payments
ON Contracts.[Contract ID] = Payments.[Contract ID]
GROUP BY Contracts.[Contract ID]
Query 2:
SELECT Contracts.[Contract ID],
Contracts.[Contract Value],
Query1.[SumOfValue Payment Value],
[Contract Value] - [SumOfValue Payment Value]
INTO NetValues
FROM Contracts
INNER JOIN Query1
ON Contracts.[Contract ID] = Query1.[Contract ID]
Basically the Query 1 , sums the payments for each contract and Query 2 calculates the net value (total value - paid amount)
If there is any better way to achieve all of this please let me know.
You can do it as a single query as follows:
SELECT c.[Contract ID],
c.[contract value],
p.Paid,
c.[contract value]-nz(p.paid) AS Remaining
FROM Contracts AS c
LEFT JOIN
(SELECT [Contract ID], sum([Payment Value]) as paid
FROM Payments
group by [Contract ID]) AS p
ON c.[Contract ID] = P.[Contract ID];
Given these two tables:
The query will produce this result:
" ... if the contract is new and there are no payments on the payments table for this new contract, the net amount shown in my query is zero (blank field)"
Actually, when you have no payments recorded for a given contract, Query1 gives you Null, not zero, as the sum of the payments. And any number minus Null gives you Null, not that first number.
If you want the Null treated as zero, use the
Nz Function
Here's an Immediate window example of those issues:
Contract_Value = 100.00
SumOfValue_Payment_Value = Null
? Contract_Value - SumOfValue_Payment_Value
Null
' use Nz() to substitute zero for Null
? Contract_Value - Nz(SumOfValue_Payment_Value, 0)
100
You can do the same in Query2 by including this:
[Contract Value] - Nz([SumOfValue Payment Value], 0)
So I have these 3 tables below:
User Table:
Deposit Table:
Withdrawal Table:
Question:
How do I join these 3 tables together, having the values grouped by:
Month
Sex
Age
State
Expected Output:
As seen from my expected output above, I want to separate these users into different buckets
i.e.
Month: January, Sex: F, Age: 18, State: Arizona (Bucket 1)
Month: January, Sex: M, Age: 21, State: Texas (Bucket 2)
and find the sum(Deposit.amount) & sum (Withdrawal.amount) group by the fields above ^
as well as how many users are in this bucket.
Clarification:
"Month" column in my Expected Ouput is based on Transaction Month for both Deposit & Withdrawal table combined, I just added the "Account Created Month" column in User table just in case its possible to do a 3-table join through Month values.
This should work:
SELECT tr.Month, u.Sex, u.Age, u.Stage
, Sum(CASE WHEN [Type] = 'D' THEN tr.Amount ELSE 0 END) As [Total Deposit Amount]
, Sum(CASE WHEN [Type] = 'W' THEN tr.Amount ELSE 0 END) As [Total Withdrawal Amount]
, COUNT(distint u.UserID) As [Number of Users in this Category]
FROM (
SELECT userid, [Deposit Amount] As Amount, [Transaction Month] As Month, 'D' As [Type] FROM [Deposit]
UNION ALL
SELECT userid, [Withdrawal Amount], [Transaction Month], 'W' FROM [Withdrawal]
) tr
INNER JOIN User u ON u.UserID = tr.UserId
GROUP BY tr.Month, u.Sex, u.Age, u.State
Really, deposits and withdrawals should be the same table (Transactions) to begin with, where the only difference is withdrawals are negative.
Also... please stop using images to represent your sample data and results. It makes things so much harder on us to help you, which makes it less likely you'll get a good or prompt answer.
I have this data set with patient information/charges (Table 1).
I am looking to achieve Table 3. Table 3 shown below has:
Distinct Patient Number (Patno)
The sum of Billed Charges for each Patno (sum billed charges then group by patno)
DRG Rate from Table 2. This is just matching the DRG in Table 1 to Table 2 and getting the DRG Rate Associated with it.
Stop loss column. If the sum of Billed Charges for a patient (found in column 2) is >275000 then (SUM of Billed Charges -275000)*.195+ DRG Rate from column 3
Claim total: If there is a stop loss, then the claim total is just = to the stop loss value found in column 4. ELSE, it is equal to the DRG rate.
Note, each patient has only ONE claim, but can have multiple line items as shown in table 1.
Table 1: Raw Data
Table 2: DRG Lookup Table
Table 3: Result Table
Please let me know if you need any additional info. I know this is a lot to ask, honestly any tips/tricks advice will really help ! Thanks in advance !
You can try with this
SELECT PATNO, SUM([Billed Charges]) as BilledCharges,
DR.[DRG Rate],
CASE WHEN SUM([Billed Charges]) >275000 THEN ((SUM([Billed Charges])-275000 )*.195)+DR.[DRG Rate] ELSE 'NO' END AS StopLoss,
CASE WHEN SUM([Billed Charges]) >275000 THEN ((SUM([Billed Charges])-275000 )*.195)+DR.[DRG Rate] ELSE DR.[DRG Rate] END AS ClaimTotal
FROM raw_data AS RD
INNER JOIN DRG_Lookup_Table AS DR ON RD.DRG=DR.[DRG Code]
GROUP BY PATNO
I'm not so sure I understand the cardinality of the records in the two tables well, but this should be fine:
SELECT
t1.PATNO,
t1.[Billed Charges] AS BilledCharges,
t2.[DRG Rate],
t1.DRG,
CASE
WHEN t1.[Billed Charges] >= 275000
THEN (t1.[Billed Charges] - 275000)*.195 + t1.DRG
ELSE 0
END AS [Stop Loss],
CASE
WHEN t1.[Billed Charges] >= 275000
THEN (t1.[Billed Charges] - 275000)*.195 + t1.DRG
ELSE t2.[DRG Rate]
END AS [Claim Total]
FROM
(
SELECT PATNO, DRG, SUM([Billed Charges]) AS [Billed Charges]
FROM TABLE_1
GROUP BY PATNO, DRG
) t1
LEFT JOIN TABLE_2 t2 ON t1.DRG = t2.[DRG Code]
Since I couldn't recreate the tables I have not been able to check the code. Please try and let me know.
select patno,sum(billedcharges)billedcharges,(select drg_rate
from DRG_looking_table where drg_code=r.drg)DRG_Rate, drg,
(case when sum(billedcharges)>275000 then (sum(billedcharges)-275000)*.195+drg_rate else 'No' end) stop_loss,
(case when sum(billedcharges)>275000 then (sum(billedcharges)-275000)*.195+drg_rate else drg_rate end)Claim_Total
from raw_data GROUP BY PATNO
I have searched for an answer for ages with no luck for my particular problem.
I have been asked to create a script to extract data for a mail-merge. Our billing team need to identify users who we have sent final warning notices to but have still not paid their bills.
The biggest challenge that I have been unable to overcome is that some of the customers may have done one of the following:
not made any payments for the outstanding amount
paid the outstanding amount in full in one payment
paid the outstanding amount in full with multiple payments
paid part of the outstanding amount in one or multiple payments
The data that was used to generate the final warning notices is in a table with the outstanding amount appearing as a positive number, and any payments that have been made are in a different table as a negative number.
In my query identifying customers from scenario 1 and 2 would be fairly easy to identify, but 3 and 4 are causing me headaches. I had the idea of adding the outstanding amount to a sum of any payments made. If the sum total is > 0 then the customer has not paid enough to cover the outstanding amount. I have developed a sub-query to get a total of all payments made after a certain date but I can't get the query to add that to the outstanding amount and then check to see if the balance is greater than 0. Error I get is "Cannot perform an aggregate function on an expression containing an aggregate or a subquery."
Can someone recommend a different approach to this query?
SELECT
finalwarning.table_no AS [Property No],
finalwarning.value_1 AS [Arrears Balance],
finalwarning.date_1 AS [Notice Date]
FROM
finalwarning
WHERE
finalwarning.table_no = 172030
AND finalwarning.ref_3 = 'Final'
AND ((SELECT
CASE
WHEN EXISTS (SELECT 1
FROM paymentsmade
WHERE PROPERTY_NO = 1234
AND TRANSACTION_DESC = 'Receipt'
AND TRANSACTION_DATE > '2017-05-01 00:00:00.000'
GROUP BY TRANSACTION_DESC)
THEN (SELECT SUM(amount)
FROM paymentsmade
WHERE paymentsmade.PROPERTY_NO = 1234
AND paymentsmade.TRANSACTION_DESC = 'Receipt'
AND paymentsmade.TRANSACTION_DATE > '2017-05-01 00:00:00.000'
GROUP BY paymentsmade.TRANSACTION_DESC)
ELSE '0'
END) + finalwarning.value_1) < 0
CTE are our friends and really help get the hard work done for us and allow for easier writing of the core SQL. The example below takes your structure and applies the concept of a payments CTE which will aggregate payments by property no and provide for all of the calculations you need to perform in your evaluation. I was uncertain about how the date came into play and the join might be wrong because there were 2 different numbers provided for property_no in the example but I assumed they were one and the same.
WITH paymentCTE (property_no, amount, transaction_desc, cntpayments)
as
(select property_no, sum(amount) as amount, transaction_desc, count(*)
from paymentsmade
where TRANSACTION_DATE > > '2017-05-01 00:00:00.000'
group by property_no, transaction_desc
)
SELECT fw.table_no AS [Property No]
, fw.value_1 AS [Arrears Balance]
, fw.date_1 AS [Notice Date]
, coalesce(p1.amount,0) as [Payments Made]
, fw.value_1+coalesce(p1.amount,0) as [Outstanding Balance]
, coalesce(p1.cntpayments, 0) as [Number of Payments]
FROM finalwarning fw
left outer join paymentCTE p1
on fw.table_no = p1.property_no
and p1.Transaction_desc = 'Receipt'
WHERE p1.cntpayments is null
or (coalesce(p1.cntpayments, 0) = 1 and fw.value_1 + coalesce(p1.amount,0) = 0)
or (coalesce(p1.cntpayments, 0) > 1 and fw.value_1 + coalesce(p1.amount,0) = 0)
or (coalesce(p1.cntpayments, 0) > 0 and fw.value_1 + coalesce(p1.amount,0)<> 0)
Two tables I am trying to join:
tableA: this table contains all of the leads we won by BID and by SOURCE and by DATE.
Bid Amount = tableA.price, Source = tableA.lead_source_id, Date = tableA.time
tableB: this table contains all of the leads we lost by BID and by SOURCE and by DATE.
Bid Amount = tableB.cost, Source = tableB.lead_source_id, Date = tableB.bid_at
I would like to be able to return the number of bids we WON and LOST by BID and by SOURCE and by DATE. Often times there are only records in one table or the other (we won all bids or lost all bids), so it appears the outer join is needed.
Ideal output would be grouped by lead_source_id, bid amount, and time like such:
Lead Source ID, Date, Bid Amount, Won, Lost
1, 1/1/2015, $20, 5, 0
1, 1/1/2015, $25, 0, 9
5, 1/1/2015, $30, 1, 1
10, 1/2/2015, $50, 0, 1
10, 1/2/2015, $55, 1, 0
Try the following query.
;WITH tmp AS (
SELECT lead_source_id, [time] AS [Date], price, COUNT(*) AS won, 0 AS lost
FROM tableA
GROUP BY lead_source_id, [time], price
UNION ALL
SELECT lead_source_id, bid_at AS [Date], cost AS price, 0 AS won, COUNT(*) AS lost
FROM tableB
GROUP BY lead_source_id, bid_at, cost)
SELECT lead_source_id AS [Lead Source ID], [Date], price AS [Bid Amount], SUM(won) AS Won , SUM(lost) AS Lost
FROM tmp
GROUP BY lead_source_id, [Date], price
ORDER BY lead_source_id, [Date]
The common table expression (tmp) calculates number of won/lost bids and the outer select groups your data by id, date and bid amounts.