It should works, but it's not optimized:
DECLARE @MinInstallments VARCHAR(100)
DECLARE @CustCNIC VARCHAR(100)
SET @CustCNIC = '12345-1234567-8'
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;
In my opinion, never use
CASE ... WHEN ... END
to make output query more 'readible' or for 'visualize' data. There are many tools to do it, for example:
ReportViewer[
^]. Final resultset should looks like:
LOAN_ID Customer_CNIC Installment_no Date Amount
1234-55 12345-1234567-8 1 2013-02-04 648700
1234-55 12345-1234567-8 2 2013-03-04 648700
1234-55 12345-1234567-8 3 2013-04-04 648700
1234-55 12345-1234567-8 4 2013-05-04 648700
and it's totally corect! There is nothing wrong with your query.