查询在转轴后无法工作

时间:2017-12-26 03:10:58

标签: sql sql-server pivot sql-order-by

我正在尝试开发一个查询,我得到了类似的东西

SELECT a.No,
       a.Finish,
       a.Shift,
       a.McCode,
       d.NAME,
       b.ItemCode,
       b.ItemName,
       b.Qty,
       b.LotNo,
       b.Description,
       CASE
         WHEN b.Description LIKE '%not good%' THEN 'NG'
         ELSE 'OK'
       END                                     AS OKNG,
       c.ItemCode                              AS matcode,
       c.LotNo                                 AS matlot,
       CASE
         WHEN e.GroupName = 'CONTACT' THEN 'CONTACT'
         ELSE 'Coil/Silver/Wire'
       END                                     AS GroupName,
       ( c.Qty / ( a.Qty + a.QtyNg ) ) * b.Qty AS materialused
FROM   PPRDDLV a
       LEFT JOIN ICMUTTRAN b
              ON a.PrdNo = b.TranNo
                 AND a.No = b.TranId
       LEFT JOIN ICMUTTRAN c
              ON a.PrdNo = c.TranNo
                 AND a.Finish = c.DatePost
                 AND c.TranTypeID = 6
                 AND c.LotNo <> '0'
       LEFT JOIN popr d
              ON a.OprCode = d.Code
       LEFT JOIN ICITEM e
              ON c.ItemId = e.id
WHERE  c.qty IS NOT NULL
       AND b.ItemCode IS NOT NULL 

我在大约2秒内获得了大约49000条记录,然后我不想转动具有2种值的列,所以我把它变成了这样的东西

SELECT no,
       finish,
       shift,
       mccode,
       NAME,
       itemcode,
       itemname,
       qty,
       lotno,
       description,
       okng,
       matcode,
       matlot
FROM   (SELECT a.No,
               a.Finish,
               a.Shift,
               a.McCode,
               d.NAME,
               b.ItemCode,
               b.ItemName,
               b.Qty,
               b.LotNo,
               b.Description,
               CASE
                 WHEN b.Description LIKE '%not good%' THEN 'NG'
                 ELSE 'OK'
               END                                     AS OKNG,
               c.ItemCode                              AS matcode,
               c.LotNo                                 AS matlot,
               CASE
                 WHEN e.GroupName = 'CONTACT' THEN 'CONTACT'
                 ELSE 'Coil/Silver/Wire'
               END                                     AS GroupName,
               ( c.Qty / ( a.Qty + a.QtyNg ) ) * b.Qty AS materialused
        FROM   PPRDDLV a
               LEFT JOIN ICMUTTRAN b
                      ON a.PrdNo = b.TranNo
                         AND a.No = b.TranId
               LEFT JOIN ICMUTTRAN c
                      ON a.PrdNo = c.TranNo
                         AND a.Finish = c.DatePost
                         AND c.TranTypeID = 6
                         AND c.LotNo <> '0'
               LEFT JOIN popr d
                      ON a.OprCode = d.Code
               LEFT JOIN ICITEM e
                      ON c.ItemId = e.id
        WHERE  c.qty IS NOT NULL
               AND b.ItemCode IS NOT NULL) AS a
       PIVOT (Max(materialused)
             FOR groupname IN ([CONTACT],
                               [Coil/Silver/Wire])) AS b 

但即使在10分钟后查询也不会完成,它甚至不会显示单个记录。除了那个,在我使用数据透视表之前的第一个查询,我通过a.no发出了一个订单,但是当我执行它时,结果只显示为记录105,但查询仍然有效,并且没有加载记录。

任何人都可以帮我找出发生了什么事吗? 谢谢!

这里是从内部查询收集的样本数据和查询后的预期值。 enter image description here

我意识到我选择了&#34; matcode&#34;在外部选择,但它不应该在那里(但仍然没有工作,并注意列&#34;使用的材料&#34;,我将在稍后使用它已完成)

2 个答案:

答案 0 :(得分:0)

尝试使用条件聚合而不是使用pivot。

SELECT no,
       finish,
       shift,
       mccode,
       NAME,
       itemcode,
       itemname,
       qty,
       lotno,
       description,
       okng,
       matcode,
       matlot,
       [CONTACT] = Max(CASE WHEN GroupName = 'CONTACT' THEN materialused END),
       [Coil/Silver/Wire] = Max(CASE WHEN GroupName <> 'CONTACT' THEN materialused END)
FROM   (<inner query>) AS a
GROUP  BY no,
          finish,
          shift,
          mccode,
          NAME,
          itemcode,
          itemname,
          qty,
          lotno,
          description,
          okng,
          matcode,
          matlot 

答案 1 :(得分:0)

---IF you don't have index on filter columns then try to apply for performance  

 SELECT a.No,
       a.Finish,
       a.Shift,
       a.McCode,
       d.NAME,
       b.ItemCode,
       b.ItemName,
       b.Qty,
       b.LotNo,
       b.Description,
        CASE WHEN b.Description LIKE '%not good%' THEN 'NG'
               ELSE 'OK'
              END  AS OKNG,
     c.ItemCode,   
     c.LotNo,   
     MAX(CASE WHEN e.groupname ='CONTACT' THEN    ( c.Qty / ( a.Qty + a.QtyNg ) ) * b.Qty END)OVER(ORDER BY a.no) AS [CONTACT],
       MAX(CASE WHEN e.groupname ='Coil/Silver/Wire' THEN  ( c.Qty / ( a.Qty + a.QtyNg ) ) * b.Qty END)OVER(ORDER BY a.no) AS [Coil/Silver/Wire]
FROM   PPRDDLV a WITH(NOLOCK)
       LEFT JOIN ICMUTTRAN b WITH(NOLOCK)
              ON a.PrdNo = b.TranNo
                 AND a.No = b.TranId
       LEFT JOIN ICMUTTRAN c WITH(NOLOCK)
              ON a.PrdNo = c.TranNo
                 AND a.Finish = c.DatePost
                 AND c.TranTypeID = 6
                 AND c.LotNo <> '0'
       LEFT JOIN popr d WITH(NOLOCK)
              ON a.OprCode = d.Code
       LEFT JOIN ICITEM e
              ON c.ItemId = e.id
WHERE  c.qty IS NOT NULL
       AND b.ItemCode IS NOT NULL