我有SQL Server 2008中的2個表,我想從這兩個表使用連接的細節。如何從SQL Server中的兩個表中獲取值?
T-1:vwHardwareConsolidate
|ID|||Qty|Type|Task_Id|
T-2:
|MasterID|Task_Id|Act_Qty|
我想從T2
從T1
和Masterid, Act_Qty
得到id, task_name, sum(qty), task_id
我已經試過此查詢
select
ID as MasterID, Task_id, Task_Name as Items,
SUM(Qty) as Req_Qty, 0 as Act_Qty
from
vwHardwareConsolidate
where
type = 'Reqrd' and ID = '21'
Group by
Task_Name,id,Task_id
union
(select
m.MasterID, m.Task_Id, vw.Task_Name as Items, 0 as Req_Qty, m.Act_Qty
from
vwHardwareConsolidate vw
Right join
(select
MasterID, m.Task_Id, 0 as Req_Qty, sum(Act_qty) as Act_Qty
from
tbl_MaterialDistribution_Detail m
where
MasterID = '21'
group by
m.Task_Id, MasterID) as m on m.Task_Id = vw.Task_id)
vwHardwareConsolidate
ID Site_name Qty Task_Name Type
1 CITY 1 A16Port_Switch Reqrd
1 CITY 1 Digital_Camera Reqrd
1 CITY 1 Electronic_Pen Reqrd
tbl_MaterialDistribution_Detail
:
MasterID|TaskId|Act_qty
7 31 1
2 32 1
12 39 3
總和(數量)請解釋總和是如何計算的,請提供樣本數據 –
請與產出表提供樣品 –