Page 1 of 1

Compare 2 Table + Sort

Posted: Tue Mar 27, 2018 8:04 pm
by parakorn
ต้องการรวม Code ทั้ง 2 ชุด ให้เหลือชุดเดียวครับ ปกติต้องดึงข้อมูล 2 ครั้ง แล้วนำมา Map กันซึ่งใช้เวลานานมากครับ
และต้องการให้ ข้อมูล Sort ยอดจากน้อยไปหามาก (ยอด tt_cost) รวมถึง Group สินค้าทั้ง 2 Table ให้เหลือบรรทัดเดียวด้วยครับ :shock: :roll:

Code1

Code: Select all

select location,group_no,group_name,a.item,item_desc,tran_code,gl_ref_no,month(tran_date)MM,sum(units) units,sum(total_cost) tt_cost
from DB_data..tran_data_R1_17 a,item_master b
where tran_date between '09-22-2017' and '03-21-2018'
and a.item=b.item 
and gl_ref_no in ('13') 
and location in ('691') 
and tran_code in ('22','23')
group by location,group_no,group_name,a.item,item_desc,tran_code,gl_ref_no,month(tran_date)
union
select location,group_no,group_name,a.item,item_desc,tran_code,gl_ref_no,month(tran_date)MM,sum(units) units,sum(total_cost) tt_cost
from DB_data..tran_data_R1_18 a,item_master b
where tran_date between '09-22-2017' and '03-21-2018'
and a.item=b.item 
and gl_ref_no in ('13') 
and location in ('691') 
and tran_code in ('22','23')
group by location,group_no,group_name,a.item,item_desc,tran_code,gl_ref_no,month(tran_date)
Code2

Code: Select all

select location,group_no,group_name,a.item,item_desc,tran_code,gl_ref_no,month(tran_date)MM,sum(units) units,sum(total_cost) tt_cost
from DB_data..tran_data_R1_17 a,item_master b
where tran_date between '09-22-2017' and '03-21-2018'
and a.item=b.item  
and location in ('691') 
and tran_code in ('2')
and a.item in('0000095506500','4893049000423','8850161161247')
group by location,group_no,group_name,a.item,item_desc,tran_code,gl_ref_no,month(tran_date)

Re: Compare 2 Table + Sort

Posted: Tue Mar 27, 2018 8:15 pm
by snasui
:lol: ไม่มี Table และ Data ตัวอย่างให้ทดสอบจะตอบค่อนข้างยากครับ

ลองสร้างนำ Query ทั้งสองไปสร้างเป็น View แยกเป็น 2 View จากนั้นสร้าง View ที่ 3 หรือสร้าง Query เข้ามา Join View เข้าด้วยกันแล้ว Group อีกทีครับ