2014-02-28 32 views
2

我有四個查詢需要返回到單個行。我試圖自己想出一個內聯視圖,但仍然遇到錯誤或執行時間很長。我需要將這四個值返回到一行以產生庫存輪次報告。庫存回報的公式是收到的單元總數/期末餘額* 12。使用T-SQL;將四個查詢合併爲一行

查詢...

Declare @startdate datetime = cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)as Date) 
Declare @enddate datetime = cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())+1, 0)as Date) 

Select sum(act_quantity) as QtyReceived, podet.wh_num 
FROM [irms_2_5_2].[dbo].[podet] 
WHERE delivery between @startdate and @enddate 
    and wh_num = 'HM10' 
Group by podet.wh_num 

Select sum(qty) as QtyShipped, pick.wh_num 
FROM pick 
WHERE convert(datetime, left(date_time,8),102) between @startdate and @enddate 
    and wh_num = 'HM10' 
Group by pick.wh_num 

Select sum(item_qty-sugg_qty) as Adjustments, auditlog.wh_num 
FROM auditlog 
WHERE wh_num = 'hm10' 
    and trans_type = 'as' 
    and convert(datetime, left(date_time,8),102) between @startdate and @enddate 
Group by auditlog.wh_num 


Select sum(total_qty) as endBalance, inventory.wh_num 
FROM inventory 
WHERE wh_num = 'HM10' 
Group by inventory.wh_num 

enter image description here

+0

使用'CROSS JOIN'來使用您的每個查詢。如果你只需要一行,那麼你需要做一個'SELECT SUM()'而不用另一列 – Lamak

回答

0

通過UNION'ing他們產生一個結果。然後做子查詢,像這樣:

產生一致的形式原始數據:

SELECT * 
INTO #temp 
FROM 
(
Select sum(act_quantity) as [QTY], podet.wh_num AS [NUM], 'QtyReceived' AS [TYPE] 
FROM [irms_2_5_2].[dbo].[podet] 
WHERE delivery between @startdate and @enddate 
     and wh_num = 'HM10' 
Group by podet.wh_num 
UNION ALL 
Select sum(qty) as [QTY], pick.wh_num AS [NUM], 'QtyShipped' AS [TYPE] 
FROM pick 
WHERE convert(datetime, left(date_time,8),102) between @startdate and @enddate 
     and wh_num = 'HM10' 
Group by pick.wh_num 
UNION ALL 
....<REST OF YOUR STUFF, SIMILAR MANNER 
) AS RAW 

SELECT 
(SELECT Qty FROM #TEMP WHERE Type='QtyShipped') AS QTYShipped, 
(SELECT Qty FROM #TEMP WHERE Type='QtyReceived') AS QTYReceived, 
... 
... 
1

簡單地定義四個變量,分配值給他們,並在年底選擇。對於例如:

DECLARE @QtyReceived INT, 
     @QtyShipped INT, 
     @Adjustments INT, 
     @EndBalance INT 

SELECT @QtyReceived = Sum(act_quantity) 
FROM ... 
... 
SELECT @EndBalance = Sum(total_qty) 
FROM ... 

SELECT @QtyReceived QtyReceived, @QtyShipped QtyShipped, @Adjustments Adjustments, 
     @EndBalance EndBalance 
2

有多種方式到達那裏。哪一個最適合您的情況取決於您的數據庫,架構和數據的形狀。

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 , 
     ... 

很多不同的方式來做到這一點。你只需要找到一個適合你的特定情況的最好方法。