If I do
SELECT COUNT(*) as NUMBERRESULTS
from amsuser.car
I return 82 results, but if
SELECT COUNT(*) as NUMBERRESULTS
from amsuser.car
left join amsuser.custhist RW on loan.CUST_NUMBER = RW.cust_number AND loan.ACCOUNT_NUMBER = RW.ACCOUNT_NUMBER AND RW.trans_code='AJ' AND loan.LOAN_CODE='RW'
left join amsuser.custhist RO on loan.CUST_NUMBER = RO.cust_number AND loan.ACCOUNT_NUMBER = RO.ACCOUNT_NUMBER AND RO.trans_code='RG' AND loan.LOAN_CODE='RW'
I get 812 results? How can the joins be adding rows?

Anupam SinghRank #643 · 3.7K reputationPosted May 21, 2014, 5:00 AM EDT
yes.. here is the solution then. actually the problem is duplicate records right?
so first we need to take distinct records then we can apply join .
try this (please refactor this b'coz i wrote this in notepad not in ssms)
WITH right_table as
(
SELECT selery,PF,ROW_NUMBER() OVER(PARTITION by ID ORDER BY id)
AS duplicaterec
FROM table1
)
select ltbl.col1,rtbl.col2, rtbl.col1,rtbl.col2 from // here we can apply join
right_table rtbl join left_table ltbl on ltbl.id=rtbl.id
WHERE rtbl.duplicaterec = 1 //should be 1
Jignesh TrivediRank #16 · 50.3K reputationPosted May 20, 2014, 8:29 PM EDT
The SQL LEFT Join returns all rows from the left table even if there are no matching in right table.
This join always match record on the column which are passed after on key word.
In you case, It returns multiple result because it may be custhist table contain multiple record in combination of cust_number, account_number ,trans-code and loan_code.
hope this will help you.
patrickRank #4,458 · 342 reputationPosted May 21, 2014, 4:22 AM EDT
I think I understand, although I was under the assumption that left join returned only one record for each record in table A, regardless of the records in table B;
So I need to make sure that my parameters in the join only return 1 record for my custhist table?
Jignesh TrivediRank #16 · 50.3K reputationPosted May 21, 2014, 4:24 AM EDT
you have to take care if you want to single records....
patrickRank #4,458 · 342 reputationPosted May 21, 2014, 4:27 AM EDT
Anupam SinghRank #643 · 3.7K reputationPosted May 21, 2014, 4:34 AM EDT
You can use subquery if join doesnt fulfill your requirement .
like :
select t1.id,t1.name,t1.age,(select top(1) s.sslery from salery s where s.id=t1.id ) as selery from table1 t1
you can use more subquery as per your need ..
hope this will help.
patrickRank #4,458 · 342 reputationPosted May 21, 2014, 4:43 AM EDT
patrickRank #4,458 · 342 reputationPosted May 21, 2014, 5:17 AM EDT
Anupam SinghRank #643 · 3.7K reputationPosted May 21, 2014, 5:28 AM EDT
With :With is basically use to set data in temporary variable.(this is my definition.)
Partition : PARTITION BY Divides the result set into partitions .
PARTITION BY Divides the result set into partitions. partition by just works on a window function, like row_number.(this is what google says :) b'coz is used it many time but unknowingly)
patrickRank #4,458 · 342 reputationPosted May 21, 2014, 5:30 AM EDT