2017-03-08 25 views
1

我對MS SQL Server有查詢。該查詢會計算每個商品中有多少商品,並在每個商店中排隊購買和銷售。使用union all從四個不同的表中獲取最新日期

SELECT i.no_, 
     s2.location, 
     s2.bincode, 
     s2.inventory, 
     s2.purchase, 
     s2.sale, 
     s2.defbinflag 
FROM dbo.[kasia$item] AS i WITH (nolock) 
     LEFT OUTER JOIN (SELECT s.itemno, 
           s.location, 
           bc.bincode, 
           Sum(s.inventory) AS Inventory, 
           Sum(s.purchase) AS Purchase, 
           Sum(s.sale)  AS Sale, 
           bc.defbinflag 
         FROM (SELECT [item no_]  AS ItemNo, 
             [location code] AS Location, 
             Sum(quantity) AS Inventory, 
             0    AS purchase, 
             0    AS sale, 
             [bin code]  AS BinCode 
           FROM dbo.[kasia$warehouse entry] WITH 
             (nolock 
             ) 
           WHERE (quantity <> 0) 
           GROUP BY [item no_], 
              [location code], 
              [bin code] 
           UNION ALL 
           SELECT no_       AS ItemNo, 
             [location code]    AS Location, 
             0        AS Inventory, 
             Sum([outstanding qty_ (base)]) AS purchase, 
             0        AS sale, 
             [bin code]      AS BinCode 
           FROM dbo.[kasia$purchase line] WITH ( 
             nolock) 
           WHERE ([document type] = 1) 
             AND (type = 2) 
           GROUP BY no_, 
              [location code], 
              [bin code] 
           UNION ALL 
           SELECT no_       AS ItemNo, 
             [location code]    AS Location, 
             0        AS Inventory, 
             0        AS purchase, 
             Sum([outstanding qty_ (base)]) AS sale, 
             [bin code]      AS BinCode 
           FROM dbo.[kasia$sales line] WITH ( 
             nolock) 
           WHERE ([document type] = 1) 
             AND (type = 2) 
           GROUP BY no_, 
              [location code], 
              [bin code] 
           UNION ALL 
           SELECT [item no_]      AS ItemNo, 
             [transfer-from code]   AS Location, 
             0        AS Inventory, 
             0        AS purchase, 
             Sum([outstanding qty_ (base)]) AS sale, 
             [transfer-from bin code]  AS BinCode 
           FROM dbo.[kasia$transfer line] WITH ( 
             nolock) 
           GROUP BY [item no_], 
              [transfer-from code], 
              [transfer-from bin code]) AS s 
           LEFT OUTER JOIN 
           (SELECT DISTINCT 
           [item no_], 
           [location code], 
           [bin code] AS BinCode, 
           [default] AS DefBinFlag 
           FROM dbo.[kasia$bin content] 
             WITH (nolock) 
           GROUP BY [item no_], 
              [location code], 
              [bin code], 
              [default]) AS bc 
              ON s.itemno = bc.[item no_] 
               AND bc.[location code] = 
                s.location 
               AND bc.bincode = s.bincode 
         WHERE (bc.bincode IS NOT NULL) 
         GROUP BY s.itemno, 
            s.location, 
            bc.bincode, 
            bc.defbinflag) AS s2 
        ON s2.itemno = i.no_ 

但我需要需要添加最新的進境之日起在四個表的一個

[kasia$warehouse entry] 
[kasia$purchase line] 
[kasia$sales line] 
[kasia$transfer line] 

我怎麼能拿在決勝盤一列包含最後一個條目的日期時間變量從四張桌子?我還沒有試過殺豬此查詢與我試圖讓這將使其成爲不可讀的日期優秀作品,但列如下:

[kasia$warehouse entry] [Expected Receipt Date] 
[kasia$purchase line] [Shipment Date] 
[kasia$sales line] [Shipment Date] 
[kasia$transfer line] [Registering Date] 

現在我有這樣的例子,結果:

+---------+----------+---------+-----------+----------+------+------------+ 
| No_  | location | bincode | inventory | purchase | sale | defbinflag | 
+---------+----------+---------+-----------+----------+------+------------+ 
| 0035513 | dp  | V14-3 | 3   | 2  | 1 | 1   | 
+---------+----------+---------+-----------+----------+------+------------+ 

我需要添加一個列的日期。

+1

從我可以告訴,你沒有返回*任何*日期從這些表。我不明白這個問題。 –

+0

是的!我將編輯帖子。 – HellOfACode

+0

不是一個解決方案,而是一個建議。我會將查詢分解成幾個CTE,以便更容易地進行審閱。看着它讓我頭暈目眩。這樣做也可以讓你使用像ROW_NUMBER或DENSE_RANK這樣的窗口函數來確定排序。 – pimbrouwers

回答

1

max([whichever date])添加到每個聯合的所有查詢以及它上面的查詢和頂部查詢。像這樣:

select 
    i.no_ 
    , s2.location 
    , s2.bincode 
    , s2.inventory 
    , s2.purchase 
    , s2.sale 
    , s2.defbinflag 
    , s2.maxDate 
from dbo.[kasia$item] as i with (nolock) 
    left join (
    select 
     s.itemno 
     , s.location 
     , bc.bincode 
     , Sum(s.inventory) as Inventory 
     , Sum(s.purchase) as Purchase 
     , Sum(s.sale) as Sale 
     , bc.defbinflag 
     , max(s.MaxDate) as maxDate 
    from (
     select 
      [item no_] as ItemNo 
     , [location code] as Location 
     , Sum(quantity) as Inventory 
     , 0 as purchase 
     , 0 as sale 
     , [bin code] as BinCode 
     , max([Expected Receipt Date]) as MaxDate 
     from dbo.[kasia$warehouse entry] with (nolock) 
     where (quantity <> 0) 
     group by 
      [item no_] 
     , [location code] 
     , [bin code] 

     union all 
     select 
      no_ as ItemNo 
     , [location code] as Location 
     , 0 as Inventory 
     , Sum([outstanding qty_ (base)]) as purchase 
     , 0 as sale 
     , [bin code] as BinCode 
     , max([Shipment Date]) as MaxDate 
     from dbo.[kasia$purchase line] with (nolock) 
     where ([document type] = 1) 
     and (type = 2) 
     group by 
     no_ 
     , [location code] 
     , [bin code] 

     union all 
     select 
      no_ as ItemNo 
     , [location code] as Location 
     , 0 as Inventory 
     , 0 as purchase 
     , Sum([outstanding qty_ (base)]) as sale 
     , [bin code] as BinCode 
     , max([Shipment Date]) as MaxDate 
     from dbo.[kasia$sales line] with (nolock) 
     where ([document type] = 1) 
     and (type = 2) 
     group by 
     no_ 
     , [location code] 
     , [bin code] 

     union all 
     select 
     [item no_] as ItemNo 
     , [transfer-from code] as Location 
     , 0 as Inventory 
     , 0 as purchase 
     , Sum([outstanding qty_ (base)]) as sale 
     , [transfer-from bin code] as BinCode 
     , max([Registering Date]) as MaxDate 
     from dbo.[kasia$transfer line] with (nolock) 
     group by [item no_] 
     , [transfer-from code] 
     , [transfer-from bin code] 
    ) as s 
    left join (
    select distinct 
     [item no_] 
     , [location code] 
     , [bin code] as BinCode 
     , [default] as DefBinFlag 
    from dbo.[kasia$bin content] with (nolock) 
    group by [item no_] 
     , [location code] 
     , [bin code] 
     , [default] 
    ) as bc 
     on s.itemno = bc.[item no_] 
    and bc.[location code] = s.location 
    and bc.bincode = s.bincode 
    where (bc.bincode is not null) 
    group by s.itemno 
    , s.location 
    , bc.bincode 
    , bc.defbinflag 
    ) as s2 
    on s2.itemno = i.no_ 
0

我已經做了類似的問題,通過連接相同的表的最大日期和關鍵得到最新的快照,一個例子是在這裏添加,同樣可以用於所有表。

FROM dbo.[kasia$warehouse entry] warehouse 
    JOIN (select itemno, location, bincode, max(ReceiptDate) lastdate from 
      dbo.[kasia$warehouse entry] 
      Group by itemno, location, bincode 
     ) lastreceiptwarehouse 
     on lastreceiptwarehouse.itemno = warehouse.itemno 
     and lastreceiptwarehouse.location = warehouse.location 
     and lastreceiptwarehouse.bincode = warehouse.bincode 
      and lastreceiptwarehouse.lastdate= warehouse.ReceiptDate