SQL Query to join two columns of a table with other tables - sql-server-2005

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

Related

SQL query to select specific fields, process sum and count from two tables

i have these two tables
ORDERS
id order_id e_id e_name
1 1000 1001 Tom
2 1009 1001 Tom
3 1010 1001 Tom
4 1011 1002 Parker
5 1012 1002 Parker
6 1013 1003 Rohan
Transactions
id order_id amount status
1 1000 100 success
2 1009 80 success
3 1010 100 failed
4 1011 50 success
6 1012 50 success
7 1013 100 failed
i would like to join two tables, select fields, process sum count and filter like this
e_id e_name amount_sum total_counts total_success_amount success_count
1001 Tom 280 3 180 2
1002 Parker 100 2 100 2
1003 Rohan 100 1 0 0
this is what i tried
use card;
SELECT COUNT(orders.order_id) as `total_counts`,
COUNT(CASE WHEN transactions.status = 'success' THEN 1 END) as `success_count`,
SUM(0 + transactions.amount) as `amount_sum`, orders.e_id,
orders.e_name
FROM orders
LEFT JOIN transactions
ON transactions.order_id=orders.order_id
GROUP BY (orders.e_id), (orders.order_id),
(orders.e_name), (transactions.amount), (transactions.status);
i tried many queries also not able to achieve it. Suggest me query to get my operation.

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

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;

Please help me to solve this [duplicate]

This question already has an answer here:
How can I perform this aggregate?
(1 answer)
Closed 9 years ago.
I have crated two table one is cutomer and other one is ord
select * from customers;
Customer table
1 101 jun 23 yyyy 15000
2 102 jas 24 zzzz 10000
3 103 fat 20 kkkk 20000
4 104 jini 40 llll 30000
5 105 michael 30 dddd 25000
6 106 das 25 hhhh 10000
7 107 vijay 26 mmmm 12000
8 108 thanku 31 jjjj 26000
9 109 vishnu 34 gggg 24000
10 110 vas 28 ffff 18000
select * from ord;
This is order table
1 12/11/2013 1:00:00 AM 102 2500
2 202 12/11/2013 4:14:17 AM 102 3000
3 203 12/9/2013 9:18:16 PM 103 2000
4 204 12/8/2013 12:00:00 PM 102 1000
5 205 12/24/2013 107 2000
This is tha union command that I have used
select c.name,c.salary,o.amount
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id;
then the resulting table is
1 jas 10000 1000
2 jas 10000 3000
3 jas 10000 2500
4 fat 20000 2000
5 vijay 12000 2000
I want resulting table like this
1 jas 10000 6500
2 fat 20000 2000
3 vijay 12000 2000
plz help me for solving this.
group by c.name, c.salary with sum(salary) is what you want:
select c.name, c.salary, sum(o.amount )
from CUSTOMERS c
inner join ord o on c.id=o.customer_id
group by c.name, c.salary;
try this if it will work.
select c.name,c.salary,sum(o.amount)
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id
group by 1,2;
Thanks.
select c.name,c.salary,SUM(o.amount )
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id
GROUP BY c.name,c.salary
I think this will work
Use Left Join or RIGHT JOIN
select c.name,c.salary,o.amount
from CUSTOMERS c
left join ord o
on c.id=o.customer_id;

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?.

How to sum Accounts by account code length?

I have 2 tables: DimAccounts and FactBudget.
DimAccounts example:
AccountKey AccountCode AccountName AccountGroup AccountType
1.6 1 6 1 NN 6 S
1.6 10 6 10 MMM 6 S
1.6 101 6 101 TTT 6 S
1.6 1010 6 1010 IIII 6 B
1.6 1011 6 1011 OOOO 6 B
1.6 1012 6 1012 KKK 6 B
FactBudget example:
TimeKey AccountKey Debit Credit
20110719 1.6 1010 20.00 5.00
20110719 1.6 1011 15.00 0.00
20110719 1.6 1000 5.00 0.00
20110719 1.6 1012 10.00 5.00
20110719 1.6 1112 10.00 0.00
In FactBudget are many Accounts just with type B. I need to get Debit and Credit Sums for Account type S (Sum).
Solution example for example data:
TimeKey AccountKey Debit Credit
20110719 1.6 1 60.00 10.00
20110719 1.6 10 50.00 10.00
20110719 1.6 101 45.00 10.00
To calculate debit and credit for sum account 1.6 101 (7 symbols with whitespace) we need to substring all acounts in factbudget up to 7 symbols (1.6 1012 -> 1.6 101, 1.6 1112 -> 1.6 111, 1.6 1011->1.6 101) and then where are they equal
(1.6 101 = 1.6 101) to group by timekey and sum debit and credit.
To calculate debit and credit for sum account 1.6 1 (5 symbols with whitespace) we need to substring all acounts in factbudget up to 5 symbols (1.6 1012 -> 1.6 1, 1.6 1112 -> 1.6 1, 1.6 1011->1.6 1) and then where are they equal
(1.6 1 = 1.6 1) to group by timekey and sum debit and credit:) and so on.
So, How to get S Accounts Debit and Cred Sum by TimeKey and AccountKey?
Basically, you could take this answer and just change one of the join conditions:
SELECT
f.TimeKey,
s.AccountKey,
SUM(f.Debit) AS Debit,
SUM(f.Credit) AS Credit
FROM DimAccounts s
INNER JOIN DimAccounts b ON b.AccountCode LIKE s.AccountCode + '%'
/* alternatively: ON s.AccountCode = LEFT(b.AccountCode, LEN(s.AccountCode)) */
INNER JOIN FactBudget f ON f.AccountKey = b.AccountKey
WHERE s.AccountType = 'S'
AND b.AccountType = 'B'
GROUP BY
f.TimeKey,
s.AccountKey
SELECT F.TimeKey,
D.AccountKey,
SUM(F.Debit) Debit,
SUM(F.Credit) Credit
FROM DimAccounts D
INNER JOIN FactBudget F ON F.AccountKey LIKE D.AccountKey + '%'
WHERE D.AccountType = 'S'
GROUP BY F.TimeKey,
D.AccountKey
Maybe something like this:
SELECT
FactBudget.TimeKey,
DimAccounts.AccountKey,
SUM(FactBudget.Debit) AS Debit,
SUM(FactBudget.Credit) AS Credit
FROM
DimAccounts
JOIN FactBudget
ON DimAccounts.AccountKey=
SUBSTRING(FactBudget.AccountKey,0,DATALENGTH(DimAccounts.AccountKey)+1)
WHERE
DimAccounts.AccountType='S'
GROUP BY
FactBudget.TimeKey,
DimAccounts.AccountKey