>>>I need some select/SQL advice please
>>>
>>>I am combinging two tables T1 & T2 they have customer numbers/Custno and quarter sales totals/qs
>>>
>>>T1 contains data for the year 2010 T2 contains data from the year 2011
>>>
>>>Data example is something like this;
>>>
>>>T1.Custno T1.QS
>>>001 $150.00
>>>004 $650.00
>>>008 $50.00
>>>012 $70.00
>>>060 $95.00
>>>
>>>
>>>T1.Custno T1.QS
>>>001 $350.00
>>>002 $550.00
>>>008 $750.00
>>>035 $10.00
>>>060 $595.00
>>>
>>>
>>>I want to create a comparison table that includes all the records of both tables, but I want to match up the customers when they are the same.
>>>I know I can do it with a scan/endscan type sort with conditions, but I was wondering if there was an easier way to do it with a select statement.
>>>
>>>the resulting table would look like this
>>>
>>>Custno 2011_QS 2010_QS
>>>001 $350.00 $150.00
>>>002 $550.00 $0.00
>>>004 $0.00 $650.00
>>>008 $750.00 $50.00
>>>012 $0.00 $70.00
>>>035 $10.00 $0.00
>>>060 $595.00 $95.00
>>>
>>>
>>>Thanks....
>>
>>You just need to join two tables, e.g.
>>
>>select T1.CustNo, T2.QS as 2011_QS, T1.QS as 2010_QS
>>FROM Table1 T1 inner join Table2 T2 on T1.CustNo = T2.CustNo
>
>
>I think you need an outer join
Right, looks like some of the CustNo are only in one of the tables, I didn't notice. In this case:
select NVL(T1.CustNo,T2.CustNo) as CustNo, NVL(T2.QS, 000000.00) as 2011_QS, ;
NVL(T1.QS,000000.00) as 2010_QS ;
FROM Table1 T1 FULL join Table2 T2 on T1.CustNo = T2.CustNo
If it's not broken, fix it until it is.
My Blog