0

我又来了,所以我再次需要你的建议。我在这里有我的脚本,它使表 A 出现。考虑到每个 dnum 的最近到期日期(日期)和服务项目的能力(onHand),我想在 Order 列中显示 qty 一次。前任。(dnum #593) 如果订购数量为 10 件,则应查找最近的到期日期,即 2014 年 6 月 12 日,然后查看是否可以提供该数量。假设它手头有 50 件,则条件满足,因此它将显示 10 内联到该行项目。我不知道我将如何处理这个问题。任何建议都可以。

谢谢

  SELECT t2.[dnum], t2.[ddate], 
   t2.[ccode], t2.[cname], 
   t1.[icode],t1.[bnum], 
   t1.[wcode],t1.[edate],t1.OnHand,
   t2.Order 
  FROM   (SELECT A.[dnum], A.[status],
           A.[ddate], A.[ccode], 
           A.[cname], B.[icode], 
           B.[desc], B.Order, 
           b.wcode 
  FROM  A INNER JOIN B ON A.dentry = B.dentry 
    WHERE status = 'O') t2 
    LEFT JOIN (SELECT T0.[icode], T0.[bnum], 
                     T0.[wcode], T0.[edate], T0.Onhand
              FROM   oibt T0 
              WHERE  T0.Onhand> 0 
            ) t1 
          ON t1.icode = t2.icode AND t1.wcode = t2.wcode 

表 A(执行代码时的示例输出)

  DNUM    ddate    ccode   cname     icode      bnum       wcode    edate      OnHand      Order 
  593  3/26/2013    C01   CUST01    Item001  MLUF-20121224  W01    12/06/2014    50         10
  593  3/26/2013    C01   CUST01    Item001  MLUF-20130329  W01    03/28/2015    1640       10
  593  3/26/2013    C01   CUST01    Item001  MLUF-20121224  W01    12/23/2014    10         10
  593  3/26/2013    C01   CUST01    Item001  MGTJ-20130201  W01    02/01/20158   830        10
  866  09/04/2013   C45   CUST45    Item835  MFR-20120901   w01    09/01/2014    15          9
  866  09/04/2013   C45   CUST45    Item835  MPPS-20110121  w01    09/01/2014    6           9
  928  04/18/2013   C36   CUST36    Item221  MFC-201209011  w01    01/21/2013    1          10
  928  04/18/2013   C36   CUST36    Item221  MASW-20130201  w01    02/01/2015    37         10

表 B(我将如何得到这个结果??)

  DNUM    ddate    ccode   cname     icode      bnum       wcode    edate      OnHand      Order 
  593  3/26/2013    C01   CUST01    Item001  MLUF-20121224  W01    12/06/2014    50         10
  593  3/26/2013    C01   CUST01    Item001  MLUF-20130329  W01    03/28/2015    1640      
  593  3/26/2013    C01   CUST01    Item001  MLUF-20121224  W01    12/23/2014    10       
  593  3/26/2013    C01   CUST01    Item001  MGTJ-20130201  W01    02/01/2015    830        
  866  09/04/2013   C45   CUST45    Item835  MFR-20120901   w01    09/01/2014    15          9
  866  09/04/2013   C45   CUST45    Item835  MPPS-20110121  w01    09/01/2014    6         
  928  04/18/2013   C36   CUST36    Item221  MFC-201209011  w01    01/21/2013    1          1
  928  04/18/2013   C36   CUST36    Item221  MASW-20130201  w01    02/01/2015    37         9
4

1 回答 1

0

这很复杂。您需要做的是找到一组 dnum 的第一条记录,以及与主选择匹配的位置输出 order 列,如果没有匹配则输出 null。

像这样的东西(尽管请注意我做的 99% 的 SQL 是针对 MySQL 的,所以这没有经过测试)

SELECT A.dnum, A.ddate, 
   A.ccode, A.cname, 
   B.icode,T0.bnum, 
   T0.wcode,T0.edate,T0.OnHand,
   CASE WHEN Sub1.dnum IS NULL THEN NULL ELSE B.Order END AS OrderCol
FROM  A 
INNER JOIN B ON A.dentry = B.dentry AND status = 'O'
LEFT JOIN oibt T0 ON T0.icode = B.icode AND T0.wcode = B.wcode AND T0.Onhand> 0 
LEFT OUTER JOIN 
(
    SELECT A.dnum, MIN(T0.edate) AS MinDate
    FROM  A 
    INNER JOIN B ON A.dentry = B.dentry AND status = 'O'
    LEFT JOIN oibt T0 ON T0.icode = B.icode AND T0.wcode = B.wcode AND T0.Onhand> 0 
    GROUP BY A.dnum
) AS Sub1
ON A.dnum = Sub1.dnum AND T0.edate = Sub1.MinDate
ORDER BY t2.dnum, t1.edate
于 2013-04-25T08:35:23.050 回答