SELECT Year(S.Saledate) as Year,Month(S.Saledate) as Month,case when A.soldocatname is null then S.soldocatname else A.soldocatname end as soldocatname,
case when A.soldosubcatname is null then S.soldosubcatname else A.soldosubcatname end as soldosubcatname,
case when A.soldosubdeptname is null then S.soldosubdeptname else A.soldosubdeptname end as soldosubdeptname,
case when A.Soldogroup is null then S.Soldogroup else A.Soldogroup end as Soldogroup,
case when A.brandname is null then S.brandname else A.brandname end as brandname,
case when A.PODOM_OVS is null then S.PODOM_OVS else A.PODOM_OVS end as PODOM_OVS,
case when A.BarCode is null then S.BarCode else A.BarCode end as BarCode,
case when A.ProdName is null then S.ProdName else A.ProdName end as ProdName
FROM soldout2015 S
LEFT OUTER JOIN aging A
ON S.BarCode = A.BarCode
UNION
SELECT Year(S.Saledate) as Year,Month(S.Saledate) as Month,case when A.soldocatname is null then S.soldocatname else A.soldocatname end as soldocatname,
case when A.soldosubcatname is null then S.soldosubcatname else A.soldosubcatname end as soldosubcatname,
case when A.soldosubdeptname is null then S.soldosubdeptname else A.soldosubdeptname end as soldosubdeptname,
case when A.Soldogroup is null then S.Soldogroup else A.Soldogroup end as Soldogroup,
case when A.brandname is null then S.brandname else A.brandname end as brandname,
case when A.PODOM_OVS is null then S.PODOM_OVS else A.PODOM_OVS end as PODOM_OVS,
case when A.BarCode is null then S.BarCode else A.BarCode end as BarCode,
case when A.ProdName is null then S.ProdName else A.ProdName end as ProdName
FROM soldout2015 S
RIGHT OUTER JOIN aging A
ON S.BarCode = A.BarCode
WHERE A.Branchcode = 'HHHH'