使用union all从四个不同的表中获取最新日期

时间:2017-03-08 13:01:55

标签: sql sql-server

我有一个MS SQL Server查询。该查询计算库存中每种产品的数量,每个商店排队等待购买和销售。

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          |
+---------+----------+---------+-----------+----------+------+------------+

我需要添加一个包含日期的列。

2 个答案:

答案 0 :(得分: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_

答案 1 :(得分: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