 |
ใครมีวิธีจัดการข้อมูลแนวนี้หน่อยไหมครับ ช่วยดูทีครับ |
|
 |
|
|
 |
 |
|
ข้างบนจะเอาอะไรออกมานะครับ
เคยใช้ SubQuery เปล่าครับ
ผมก็มั่ว ๆ นะครับ
Code (SQL)
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
|
ประวัติการแก้ไข 2015-07-13 11:49:57
 |
 |
 |
 |
Date :
2015-07-13 11:48:51 |
By :
Digitalhong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
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
ใส่ as ใส่ group by ทุก field ที่ ต้องการให้แสดง
|
ประวัติการแก้ไข 2015-07-13 16:28:14 2015-07-13 16:29:06
 |
 |
 |
 |
Date :
2015-07-13 16:25:17 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สงสัย แล้วข้อมูลที่เอามาแสดงเอามาจากไหนครับ
คุณมีสิทธิ์แก้ไข php ไหมครับ แล้ว มีสิทธิ์ใช้คำสั่ง query database หรือเปล่า
ใช้แค่สิทธิ์ การอ่าน ข้อมูล database เท่านั้นครับ ไม่ได้แก้ไขอะไรเลย
|
 |
 |
 |
 |
Date :
2015-07-13 17:53:21 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ประมาณนี้...มั้ง
Code (SQL)
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
|
 |
 |
 |
 |
Date :
2015-07-13 19:51:24 |
By :
ผ่านมา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าคิวรี่ได้ ก็น่าจะทำตามที่บอกได้ครับ
แต่ก็สงสัยต่อ ในกรณีที่ ตาราง1 มี Branch_ID ซ้ำกัน จะคิด percent อย่างไร
ถ้ามี 2 เรคคอร์ด แต่ละrecord / 2 ตามจำนวน record หรือเปล่า
ถ้าแบ่งเฉลี่ยตามจำนวนเรคคอร์ด ก็ ตามนี้เลยครับ
Code (SQL)
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
แต่ถ้าไม่ ก็ปรับใช้เอานะครับ
|
ประวัติการแก้ไข 2015-07-13 21:41:40
 |
 |
 |
 |
Date :
2015-07-13 21:31:09 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
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
ถ้า chk เป็น 1 แสดงว่า มีค่าจาก ตาราง 2
ถ้า chk เป็น 0 แสดงว่า ตาราง 2 ไม่มี
|
 |
 |
 |
 |
Date :
2015-07-14 10:52:49 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|