Need to create third table based on ID range in first table and line items in second table - sql

The first table contains a range of account IDs for a company.
Table 1 Company
CID Minacct Maxacct
1 100 200
2 300 350
3 500 700
The second table contains line items for balances in accounts. The key to this problem, is that not all accounts are used in an account range from table 1. For ex., Company 1 could have account IDs from 100 to 200, but in actuality only has 100, 105, 118, and 170.
Table 2 Accts
AcctID Balance
100 $5
105 $10
118 $15
170 $20
300 $25
325 $30
350 $35
501 $40
502 $45
503 $50
602 $55
700 $60
Need to create Table 3 that combines Tables 1 and 2, hopefully using a nifty Select and not a slow loop.
CID AcctID Bal
1 100 $5
1 105 $10
1 118 $15
1 170 $20
2 300 $25
2 325 $30
2 350 $35
3 501 $40
3 502 $45
3 503 $50
3 602 $55
3 700 $60
Thanks!!
David

This is a join query but the join condition is an inequality:
select c.cid, a.*
from accts a join
company c
on a.acctid between c.minacct and c.maxacct;

Related

How to query for amount waived for every contract status?

I have 2 tables:
Table 1 "contract" is as the following (sample only):
contract_id
contract_status
111
Active
122
Active
133
Finished
144
Active
155
Finished
contract_status values are: Active and Finished
Table 2 "ledger" is as the following (sample only):
ledger_id
contract_id
ledger_status
amount
1gh
111
WAIVED
450
2uk
111
PAID
0
2jz
111
WAIVED
200
4bc
122
PAID
0
5jw
122
UNPAID
150
3xs
133
PAID
0
9kd
133
WAIVED
250
7bf
144
WAIVED
100
8aq
155
UNPAID
700
One contract can have a lot of ledger_id
How can I calculate the total amount that has been waived for each contract status (active and finished)?
Expected output
contract_status
total_amount_waived
Active
750
Finished
250
We can try a join aggregation approach:
SELECT c.contract_status, COALESCE(SUM(l.amount), 0) AS total_amount_waived
FROM contract c
LEFT JOIN ledger l
ON l.contract_id = c.contract_id AND
l.ledger_status = 'WAIVED'
GROUP BY c.contract_status;

Cumulative over table rows with condition Oracle PL/SQL

I have two tables:
Employees:
employee_id field max_amount
3 a 3000
4 a 3000
1 a 1600
2 a 500
4 b 4000
2 b 4000
3 b 1700
ordered by employee, field, amount desc.
Amounts (pol, premia,field):
pol premia field **assign_to_employee**
11 900 a 3
44 1000 a 3
55 1400 a 4
77 500 a 3
88 1300 a 1
22 800 b 4
33 3900 b 2
66 1300 b 4
Assign Stats Table:
employee_id field max_amount true_amount remain
3 a 3000 2400 600
4 a 3000 1400 1600
1 a 1600 1300 300
2 a 500 0 500
4 b 4000 2100 1900
2 b 4000 3900 100
3 b 1700 0 1700
The output : assign_to_employee field (merged to amounts table).
Algoritem wise : The method is to assign pol's to employees until the premia needs to be added to the cumulative_sum is bigger then the max amount per employee listed in the employees table. You always start with the employess with most max amount until you cannot add any other pols to the employee.
I start with the employees with the grater max_amount per field.
I keep doing this until no pols remains to be assign.
Can you help me solve this?
Thank you.

Join table and applying filter?

I have a question on this query forever and never figured it out, need some help and thanks in advance for all answers!
I have table 1: Payment (storing historical payment info, I only look at one month data and only type#1 payment, so i need to apply two filters here)
Pymt
ServiceID InsuranceID Amount Month Type
1 A $10 Jun15 1
2 A $15 Jun15 1
3 B $15 Jun15 1
4 C $30 Jun15 1
5 D $50 Jun15 1
Ins
Insurance ID Name
A AAA
B BBB
C CCC
D DDD
Expected Result
Service ID Insurance ID Name Amount Month Type
1 A AAA $10 Jun15 1
2 A AAA $15 Jun15 1
3 B BBB $15 Jun15 1
4 C CCC $30 Jun15 1
5 D DDD $50 Jun15 1
So there are two tasks here:
1. filter table 1 by Jun 15 and Type 1
add a new column"Name" in table 1 to indicate the name of the insurance WITHOUT adding new rows ( it is like a vlookup ) so monthly amount total is the same
I tried differnt joins and WHERE(filter) always added new rows to result which I don't want, please advise.
Thanks!
select p.ServiceID, i.InsuraneID, i.Name, p.Amount, p.Month, p.Type
from Pymt p
inner join Ins i on p.InsuranceID = i.InsuranceID
where p.Month='Jun15' and p.Type=1

Complicated Sql Query Of Joining Two Tables

I having Following Two Tables and wants the result date and bank wise. The Example is as below.
Table1: Name:- AccountMast
companyID A/CID Name BalDR BalCR
102 14 SHOBHA NULL NULL
102 15 SONKI NULL NULL
102 16 BANK OF INDIA null 12000
Table2 Name :- DeBank
companyID transID date name particulars deposit withdrawal
102 2 12/04/2012 BANK OF INDIA MAHENDRA 1000 NULL
102 4 15/04/2012 CENTRAL BANK MAHENDRA 1000 NULL
102 3 20/05/2012 BANK OF INDIA MAHENDRA 2000 NULL
Result
BANK WISE RESULT BANK OF INDIA
date transID particulars deposit withdrawal BALANCE
01/04/2012 BAL B/F 12000
12/04/2012 2 MAHENDRA 1000 NULL 13000
20/05/2012 3 MAHENDRA 2000 NULL 15000
BANK WISE RESULT CENTRAL BANK OF INDIA
date transID particulars deposit withdrawal BALANCE**
01/04/2012 BAL B/F
15/04/2012 4 MAHENDRA 2000 NULL 2000
So what is the sql statement for it?.

SQL Query to join two columns of a table with other tables

I have 3 tables as shown in the diagram:
The test data in each of these tables is as given below:
ACCOUNT_GROUP
GroupID Name
101 Bank Accounts
105 Cash-in-hand
113 Indirect Expenses
120 Purchase Accounts
122 Sales Accounts
125 Sundry Creditors
ACCOUNT_MASTER
AccID Name GroupID
1001 RBS A/C 23456 101
1002 HSBC A/C 123456 101
1003 CASH A/C 105
1004 DISCOUNT 113
1005 CASH SALES 122
1006 CASH PURCHASE 120
1007 JOHNSON 125
ACCOUNT_TXNS
TxnID TxnDate FromAccID ToAccID Amt CrDr
1 20-Jul-2011 1002 1003 250000 C
2 20-Jul-2011 1001 1002 985241 C
3 20-Jul-2011 1005 1003 65451 C
4 20-Jul-2011 1006 1003 412874 D
5 20-Jul-2011 1007 1003 521400 C
6 20-Jul-2011 1003 1007 200 D
I want to get the account names of both FromAccID and ToAccID and their group names in a single query like this:
TxnID TxnDate FromAcc FromAccGroup ToAcc ToAccountGroup Amt CrDr
1 20-Jul-2011 HSBC A/C 123456 Bank Accounts CASH A/C Cash-in-hand 250000 C
2 20-Jul-2011 RBS A/C 23456 Bank Accounts HSBC A/C 123456 Bank Accounts 985241 C
3 20-Jul-2011 CASH SALES Sales Accounts CASH A/C Cash-in-hand 65451 C
4 20-Jul-2011 CASH PURCHASE Purchase Accounts CASH A/C Cash-in-hand 412874 D
5 20-Jul-2011 JOHNSON Sundry Creditors CASH A/C Cash-in-hand 521400 C
6 20-Jul-2011 CASH A/C Cash-in-hand JOHNSON Sundry Creditors 200 D
Kindly help me to achieve this.
You can use the same table more than once in a single query, just use aliases.
Something like
SELECT *
FROM ACCOUNT_TXNS txns INNER JOIN
ACCOUNT_MASTER fromAcc ON txns.FromAccID = fromAcc.AccID INNER JOIN
ACCOUNT_GROUP fromAccGrp ON fromAcc.GroupID = fromAccGrp.GroupID INNER JOIN
ACCOUNT_MASTER toAcc ON txns.ToAccID = toAcc.AccID INNER JOIN
ACCOUNT_GROUP toAccGrp ON toAcc.GroupID = toAccGrp.GroupID