如何在sql中只显示一次值

时间:2013-04-24 09:30:33

标签: sql-server-2008

我再次堆叠,所以我再次需要你的建议。 我在这里看到了我的脚本,这使得表A出现了。我想在Order列中显示qty一次,考虑每个dnum最近的到期日期(edate)和服务项目的容量(onHand)。恩。 (dnum#593)如果订购的数量是10pcs,它应该查找最近的到期日期,即12/06/2014,然后查看它是否可以提供数量。鉴于它有50pcs,然后条件满足,所以它将显示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

1 个答案:

答案 0 :(得分:0)

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

像这样的东西(虽然请注意我做的SQL的99%是针对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