excel动态表的SQL重复数据

时间:2010-10-25 18:24:42

标签: sql sql-server excel

首先感谢您阅读我并抱歉我的英语不好。

我在sql server 2008 r2上有以下代码

SELECT FA960.*,
       UMCONVPZ.UMFR,
       UMCONVPZ.UMCONF AS Piezas,
       UMCONVPL.UMCONF AS PL, 
       UMCONVCJ.UMCONF AS Cajas
  FROM FA960 FA960
  JOIN UMCONV UMCONVPZ ON FA960.RECURV = UMCONVPZ.UMRESR 
  JOIN UMCONV UMCONVPL ON FA960.RECURV = UMCONVPL.UMRESR 
  JOIN UMCONV UMCONVCJ ON FA960.RECURV = UMCONVCJ.UMRESR
 WHERE FA960.RMUMSR = UMCONVPZ.UMFR
   AND UMCONVPZ.UMTO = 'PZ' 
   AND UMCONVPL.UMTO = 'PL' 
   AND UMCONVCJ.UMTO = 'CJ'

输出:

PERAA  PERMM  NUMLI  RMDESC    RMUMSR   RECURV  RMMIS2  RMWGHT  RMNETW  CONCA  Cont_x_CjPz  PesoNt_x_WhPz  PesoLg_WhLPz PesoNt_x_WhCj   PesoLg_WhLCj    PESOPZNT    PESOPZBR    Pz_Emb  Cj_Emb  UMFR    Piezas  PL  Cajas
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2010   1      40     MAYONESA  LIMONES  SQUEEZE  12 OZ  CJ      405300  12     4.460000     3.840000       12.000000    12.0000000000000000000000   0.3200000000000000000000    0.3716660000000000000000    3.8400000000000000000000    4.4599920000000000000000    0.320000000000  0.371666666666  112476.000000   9373.000000 CJ  12.000000   130.000000  130.000000
2010   1      40     MAYONESA  LIMONES  SQUEEZE  12 OZ  CJ      405300  12     4.460000     3.840000       12.000000    12.0000000000000000000000   0.3200000000000000000000    0.3716660000000000000000    3.8400000000000000000000    4.4599920000000000000000    0.320000000000  0.371666666666  112476.000000   9373.000000 CJ  12.000000   130.000000  12.000000

这是一个视图,UMCONV表是从小块到盒子到碎片的度量转换。

现在我的问题是CJ(Boxes)同时转到PZ(Pieces)和PL(Palets),因此使我成为带有“垃圾数据”的重复条目。


SELECT [UMFR]
      ,[UMTO]
      ,[UMRESR]
      ,[UMWHSE]
      ,[UMLOCN]
      ,[UMVNNO]
      ,[UMSEQN]
      ,[UMCONF]
      ,[UMCALC]
      ,[UMQYF1]
      ,[UMQYT1]
      ,[UMQYF2]
      ,[UMQYT2]
      ,[UMQYF3]
      ,[UMQYT3]
      ,[UMQYF4]
      ,[UMQYT4]
      ,[UMQYF5]
      ,[UMQYT5]
      ,[UMDTMT]
      ,[UMBLNK]
  FROM [LOGISTICA].[dbo].[UMCONV]

UMFR    UMTO    UMRESR  UMWHSE  UMLOCN  UMVNNO  UMSEQN  UMCONF  UMCALC  UMQYF1  UMQYT1  UMQYF2  UMQYT2  UMQYF3  UMQYT3  UMQYF4  UMQYT4  UMQYF5  UMQYT5  UMDTMT  UMBLNK
PL  CJ  405300              1   130.000000  M   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1020627 
CJ  PL  405300              1   130.000000  D   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1020627 
PZ  CJ  405300              1   12.000000   D   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1041016 
CJ  PZ  405300              1   12.000000   M   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1041016

任何想法如何解决?到目前为止,我认为强迫它转换并做一个石头剪刀

还有其他想法吗?

这是

2 个答案:

答案 0 :(得分:2)

这对您有何帮助:

SELECT DISTINCT f.*,
       f.RMUMSR as De,
       COALESCE((SELECT UMCONF FROM UMCONV WHERE UMRESR = f.RECURV AND UMFR = f.RMUMSR AND UMTO = 'PZ'), 1) AS Piezas,
       COALESCE((SELECT UMCONF FROM UMCONV WHERE UMRESR = f.RECURV AND UMFR = f.RMUMSR AND UMTO = 'PL'), 1) AS Palet,
       COALESCE((SELECT UMCONF FROM UMCONV WHERE UMRESR = f.RECURV AND UMFR = f.RMUMSR AND UMTO = 'CJ'), 1) AS Cajas
FROM FA960 f

答案 1 :(得分:1)

您是否尝试过使用DISTINCT:

SELECT DISTINCT
       FA960.*,
       UMCONVPZ.UMFR,
       UMCONVPZ.UMCONF AS Piezas,
       UMCONVPL.UMCONF AS PL, 
       UMCONVCJ.UMCONF AS Cajas
  FROM FA960 FA960
  JOIN UMCONV UMCONVPZ ON UMCONVPZ.UMRESR = FA960.RECURV 
                      AND UMCONVPZ.UMFR = FA960.RMUMSR
                      AND UMCONVPZ.UMTO = 'PZ' 
  JOIN UMCONV UMCONVPL ON UMCONVPL.UMRESR = FA960.RECURV 
                      AND UMCONVPL.UMTO = 'PL'
  JOIN UMCONV UMCONVCJ ON UMCONVCJ.UMRESR = FA960.RECURV
                      AND UMCONVCJ.UMTO = 'CJ'

如果不了解更多数据,很难说。