有多種方式到達那裏。哪一個最適合您的情況取決於您的數據庫,架構和數據的形狀。
UNION ALL
這裏有一個方法,使用union all
。根據不同的大小和數據的形狀,這樣可以執行得很好,雖然我已經在SQL Server與大工會對occassion問題並行:
select wh_num = t. wh_num ,
QtyReceived = sum( t.act_quantity ) ,
QtyShipped = sum( t.qty ) ,
Adjustments = sum(coalesce(t.item_qty-t.sugg_qty , 0)) ,
endBalance = sum( t.total_qty )
from ( select wh_num = wh_num ,
act_quantity = convert(int, act_quantity) ,
qty = convert(int, null ) ,
item_qty = convert(int, null ) ,
sugg_qty = convert(int, null ) ,
total_qty = convert(int, null )
from irms_2_5_2.dbo.podet
where wh_num = 'hm10'
and delivery between @startdate and @enddate
UNION ALL
select wh_num = wh_num ,
act_quantity = null ,
qty = qty ,
item_qty = null ,
sugg_qty = null ,
total_qty = null
from pick
where wh_num = 'hm10'
and convert(datetime, left(date_time,8),102) between @startdate and @enddate
UNION ALL
select wh_num = wh_num ,
act_quantity = null ,
qty = null ,
item_qty = item_qty ,
sugg_qty = sugg_qty ,
total_qty = null
from auditlog
where wh_num = 'hm10'
and trans_type = 'as'
and convert(datetime, left(date_time,8),102) between @startdate and @enddate
UNION ALL
select wh_num = wh_num ,
act_quantity = null ,
qty = null ,
item_qty = null ,
sugg_qty = null ,
total_qty = total_qty
from inventory
where wh_num = 'hm10'
) t
group by t.wh_num
LEFT JOIN
另一種方法使用left join
和派生表:
select wh_num = desired.wh_num ,
QtyReceived = sum(coalesce(t1.QtyReceived , 0)) ,
QtyShipped = sum(coalesce(t2.QtyShipped , 0)) ,
Adjustments = sum(coalesce(t3.Adjustments , 0)) ,
EndBalance = sum(coalesce(t4.EndBalance , 0))
from (select wh_num = 'hm10') desired
left join (select wh_num ,
QtyReceived = sum(act_quantity)
from irms_2_5_2.dbo.podet
where delivery between @startdate and @enddate
group by wh_num
) t1 on t1.wh_num = desired.wh_num
left join (select wh_num ,
QtyShipped = sum(qty)
from pick
where convert(datetime, left(date_time,8),102) between @startdate and @enddate
group by wh_num
) t2 on t2.wh_num = desired.wh_num
left join (select wh_num ,
Adjustments = sum(item_qty-sugg_qty)
from auditlog
where trans_type = 'as'
and convert(datetime, left(date_time,8),102) between @startdate and @enddate
group by wh_num
) t3 on t3.wh_num = desired.wh_num
left join (Select wh_num ,
EndBalance = sum(total_qty)
FROM inventory
group by wh_num
) t4 on t4.wh_num = desired.wh_num
group by desired.wh_num
相關子查詢
而且使用相關子查詢第三種方法:
select wh_num = desired.wh_num ,
QtyReceived = (select sum(coalesce(act_quantity , 0))
from irms_2_5_2.dbo.podet
where wh_num = desired.wh_num
and delivery between @startdate and @enddate
) ,
QtyShipped = (select QtyShipped = sum(coalesce(qty , 0))
from pick
where wh_num = desired.wh_num
and convert(datetime, left(date_time,8),102) between @startdate and @enddate
) ,
Adjustments = (select Adjustments = sum(coalesce(item_qty-sugg_qty , 0))
from auditlog
where wh_num = desired.wh_num
and trans_type = 'as'
and convert(datetime, left(date_time,8),102) between @startdate and @enddate
) ,
EndBalance = (Select sum(total_qty)
from inventory
where wh_num = desired.wh_num
)
from (select wh_num = 'hm10') desired
最後一個方法,當你有很多的數據庫爭的,你希望儘量減少你的查詢相對足跡可能是有用的對鎖,是創建一個臨時表,在最終結果集中正確的列數:
declare @work table
(
wh_num varchar(32) not null ,
QtyReceived int not null ,
QtyShipped int not null ,
Adjustments int not null ,
EndBalance int not null
)
然後計算每個值分別,插入新行到工作表,即:
insert @work (wh_num,QtyReceived)
select 'hm10' ,
sum(coalesce(act_quantity , 0))
from irms_2_5_2.dbo.podet
where wh_num = 'hm10'
and delivery between @startdate and @enddate
後的表是完全填充,在幾行彙總爲一個返回結果集:
select wh_num ,
QtyReceived = sum(QtyReceived) ,
...
from @work
group by wh_num
這並不是說每個查詢還可以填充T-SQL變量:
declare @QtyReceived int
set @QtyReceived = (select sum(...) from podet where wh_num = 'hm10')
...
了決賽桌少選擇返回最終的結果集:
select wh_num = 'hm10' ,
QtyReceived = @QtyReceived ,
...
很多不同的方式來做到這一點。你只需要找到一個適合你的特定情況的最好方法。
使用'CROSS JOIN'來使用您的每個查詢。如果你只需要一行,那麼你需要做一個'SELECT SUM()'而不用另一列 – Lamak