2017-03-08 104 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