SQL duplicate data for excel dynamic table
First thanks for reading me and sorry for my bad english.
I have the following code on 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'
Output:
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 开发者_StackOverflow社区 0.320000000000 0.371666666666 112476.000000 9373.000000 CJ 12.000000 130.000000 12.000000
This is a view, the UMCONV table is a conversion of measures that goes from palets to boxes, to pieces.
Now my problem is that CJ (Boxes) go to both PZ (Pieces) and PL (Palets) thus making me a duplicate entry that bring "trash data".
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
Any idea of how to solve it? so far I think force it to convert and do a rock paper scissors
Any other idea?
This is the
How does this work for you:
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
Have you tried using 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'
It's tough to say without knowing more about the data.
精彩评论