Click here to Skip to main content
15,886,035 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
DECLARE @MinInstallments VARCHAR(100) 
DECLARE @CustCNIC VARCHAR(100) 

Select @CustCNIC =Customer_CNIC from Customer 

SELECT @MinInstallments = MIN(Installment_No) 
FROM Installments AS i 
WHERE Loan_id IN 
(SELECT Loan_id FROM Customer WHERE Customer_CNIC = @CustCNIC);

SELECT CASE i.Installment_no WHEN @MinInstallments THEN i.Loan_id ELSE '' END AS LOAN_ID, 
'Customer_CNIC' = CASE WHEN i.Installment_no = @MinInstallMents THEN @CustCNIC ELSE ''END, i.Installment_no, i.Date, i.Amount 
FROM Installments i, Loan l, Customer c 
WHERE l.Loan_id = c.Loan_id and c.Loan_id = i.Loan_id and c.Customer_CNIC=
@CustCNIC ORDER BY l.Loan_id,i.Installment_no
Posted
Updated 9-Jan-13 13:11pm
v3
Comments
PIEBALDconsult 9-Jan-13 18:58pm    
"Last one" what? How many should there be? What database system?
Jibesh 9-Jan-13 19:49pm    
Check this http://www.codeproject.com/Questions/524904/whereplusisplusproblemplusinplusqueryplus-3fplushe
for more :)
PIEBALDconsult 9-Jan-13 21:07pm    
Nah.

1 solution

Try this

SQL
DECLARE @CustMinLoanInstallments AS TABLE(Customer_CNIC VARCHAR(100),Loan_Id VARCHAR(100), 
MinInstallments BIGINT) 

INSERT INTO @CustMinLoanInstallments 
SELECT i.Loan_id , C.Customer_CNIC MIN(Installment_No)
FROM Installments AS i , Customer AS C 
WHERE i.Loan_id =C.Loan_id

SELECT CASE i.Installment_no WHEN cmli.MinInstallments THEN i.Loan_id ELSE '' END AS LOAN_ID,
'Customer_CNIC' = CASE WHEN i.Installment_no = cmli.MinInstallments THEN @CustCNIC ELSE ''END, i.Installment_no, i.Date, i.Amount
FROM Installments i, Loan l, Customer c ,@CustMinLoanInstallments cmli 
WHERE l.Loan_id = c.Loan_id and c.Loan_id = i.Loan_id 
  AND cmli.Loan_id = i.Loan_id and cmli.Customer_CNIC = c.Customer_CNIC
 ORDER BY l.Loan_id,i.Installment_no


Hope this helps
--RDBurmon
 
Share this answer
 

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