Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have two tables names and data in that tables are given below

Loans_Advances_Master:
Dept_id     emp_Id   loan_approved_amt    loan_adv_no
------------------------------------------------------
De_1        emp_1     10000                LA_1
de_1        emp_1     10000                LA_2


loans_deduction_details:
dept_Id   EMp_Id    Loan_deduction_amt  Loan_adv_no
-------------------------------------------------
de_1       emp_1       5000                LA_1
de_1       emp_1       5000                LA_1
de_1       Emp_1       5000                LA_2


how to write select query for above two tables for pending laon i.e
loan_approved_amt for loan_adv_no is LA_1 equl to sum(loan_deduction_amt) from loans_deduction_details form Loan_adv_no Then show 0 other wise show amount required amount should he pay..


How to get the output like This for EMp_1
de_1  emp_1    0
de_1   emp_1   5000

de

What I have tried:

SQL
select Dept_id, Emp_id, Loan_Adv_No,((select loan_adv_no,sum(Loan_Approved_Amont) from [dt_agrovet].[dbo].[Loans_Advances_Master]  group by Loan_Adv_No)- (select sum(loan_deduction_amount) from loans_deduction_details  group by emp_id,Loan_Adv_No) ) as loanamount  from [dt_agrovet].[dbo].[Loans_Advances_Master]   where emp_Id='emp_1'



can please any one give me the solution for this ...
Thank you ...
Posted
Updated 25-Apr-18 0:22am
v2

SQL
SELECT LAM.Dept_id
      ,LAM.emp_Id
      ,(LAM.loan_adv_no-(
	         SELECT SUM(LDD.Loan_deduction_amt)
	           FROM  loans_deduction_details AS LDD
			    WHERE LDD.Loan_adv_no=LAM.loan_adv_no AND LDD.Emp_Id=LAM.Emp_Id)
				    ) AS BalanceAmount
  FROM Loans_Advances_Master AS LAM
 
Share this answer
 
I made some changes in the table structure to make the query cleaner. I removed the columns "emp_id", "dept_id", and "loan_adv_no" from the table "loans_deduction_details" and added a column to hold the id of the good row of "Loans_Advances_Master" to act as a foreign key.

Here is my what my fiddle looks like.
SQL
create table LAM (id int, depId varchar(20), empId varchar(20), loanId varchar(20), loanAmt int);
create table LDD(id int, lamId int, loadDAmt int);

insert into LAM values (1,'De_1','emp_1', 'LA_1', 10000), (2,'De_1','emp_1', 'LA_2', 10000);
insert into LDD values (1,1,5000),(2,1,5000),(3,2,5000);


Now that the tables are initialized I just need to query them.
SQL
select temp.depID, temp.empID, temp.loanId, temp.loadAmt
  from (
    select 
    LAM.depID as depID, 
    LAM.empID as empID, 
    LAM.loanId as loanId, 
    LAM.loanAmt - SUM(LDD.loadDAmt) AS loadAmt 
    FROM LDD 
    INNER JOIN LAM ON LDD.lamId = LAM.Id
    GROUP BY LDD.lamId
  ) temp;


You can also check the fiddle here[^].
 
Share this answer
 
v2
Comments
Maciej Los 25-Apr-18 6:32am    
It would be great if you will post your solution here and a link to sql fiddle (not just a link to fiddle).
GKP1992 25-Apr-18 6:54am    
Okay, I was just too lazy to do that, also I changed the structure a bit to make the query cleaner. For example, I just added a foreign key relationship between the tables and removed the repeated columns.
Santosh kumar Pithani 25-Apr-18 7:32am    
plz replace "LDD.lamId = LAM.Id" instead of "LDD.Loan_adv_no=LAM.loan_adv_no AND LDD.Emp_Id=LAM.Emp_Id"
GKP1992 25-Apr-18 7:45am    
Read the first paragraph of the solution. I removed those columns to avoid doing just that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900