Select
Branch_ID,
Branch_Name,
Last_Update,
Receipt_Not_Approve,
Receipt_All From,
(Select count(*) FROM TBContract WHERE TBContract.Branch_ID = TBBranch.Branch_ID) as Rows
FROM TBBranch
select branch_id, branch_name, sum(percent)
from (
select branch_id, branch_name, 70 percent from table1 where ........
union all
select branch_id, branch_name, 30 percent from table2 where ........
) group by branch_id
select branch_id, branch_name, sum(percent) as sum_percent
from (
select branch_id, branch_name, 70 as percent from table1 where ........
union all
select branch_id, branch_name, 30 as percent from table2 where ........
) as temp_table group by branch_id, branch_name
select * ,(((Receipt_Not_Approve*1.0)/(Receipt_All*1.0)*30.0)+p70) as p100
from Branch
inner join
(select Branch_ID ,
case when HF_Get_Contract is null or HF_Get_Contract='0' then 0 else 25 end
+case when HF_Called_Date is null then 0 else 25 end
+case when HF_Car_Motor_Number is null or HF_Car_Motor_Number='' then 0 else 10 end
+case when HF_Car_Model_Number is null or HF_Car_Model_Number='' then 0 else 10 end as p70
from(
SELECT
Branch_ID,
max(HF_Get_Contract) as HF_Get_Contract,
max(HF_Called_Date) as HF_Called_Date,
max(HF_Car_Motor_Number) as HF_Car_Motor_Number,
max(HF_Car_Model_Number) as HF_Car_Model_Number
FROM Contract
group by Branch_ID) as a) as b
on Branch.Branch_ID=b.Branch_ID
select Branch_ID, Branch_Name, sum(percent) as total
from (
select Branch_ID, Branch_Name, (Receipt_Not_Approve * 30 / Receipt_All) as percent from table2
union all
select Branch_ID, Branch_Name,
(sum(HF_Get_Contract>0) * 25 / count( Branch_ID) ) +
(sum(HF_Called_Date>'') * 25 / count( Branch_ID)) +
(sum(HF_Car_Motor_Number>'') * 10 / count( Branch_ID)) +
(sum(HF_Car_Modal_Number>'') * 10 / count( Branch_ID)) as percent
from table1 group by Branch_ID, Branch_Name
) as t1
group by Branch_ID, Branch_Name
select Branch_ID, Branch_Name, (sum(percent) * sum(chk) ) as total
from (
select Branch_ID, Branch_Name, (Receipt_Not_Approve * 30 / Receipt_All) as percent, 1 as chk from table2
union all
select Branch_ID, Branch_Name,
(sum(HF_Get_Contract>0) * 25 / count( Branch_ID) ) +
(sum(HF_Called_Date>'') * 25 / count( Branch_ID)) +
(sum(HF_Car_Motor_Number>'') * 10 / count( Branch_ID)) +
(sum(HF_Car_Modal_Number>'') * 10 / count( Branch_ID)) as percent,
0 as chk
from table1 group by Branch_ID, Branch_Name
) as t1
group by Branch_ID, Branch_Name