JOIN在SELECT语句中添加额外的行

时间:2018-07-24 21:08:29

标签: sql sql-server

大家好,我有下面的SELECT语句-当我在不带注释字段的情况下运行它时,我得到了正确的结果(38行),但是当我取消注释这些字段(将它们包括在查询中)时,我得到了额外的行(大约58个添加的行)。如果有人知道为什么,我想知道为什么会这样?谢谢!

SELECT DISTINCT
  R.field ,
  O.field ,
--   CASE
     --WHEN sta.field ='011' THEN ppw.field 
     --ELSE sta.field 
  --END AS field ,
  --CASE
     --WHEN sta.field ='011' THEN pv.field 
     --ELSE sta.field 
  --END AS field ,
  --sta.field AS field ,
  --ppw.field AS field ,
  --sta.field ,
  sta.field,
  V.field ,
  CONVERT(VARCHAR(300),R.field ,101) AS field ,
  -- CASE
     --WHEN sta.field ='011' THEN ppv.field 
     --ELSE poh.field 
  --END field ,
  --poh.field ,
  poh.field ,

FROM
    Table1 R
    INNER JOIN Table2 sta
    ON    R.field = sta.field 
    AND   R.field = sta.field 
    AND   R.field LIKE   CASE
                                  WHEN (CHARINDEX('-',field )) > 0
                                  THEN Substring(field , 0, 
(CHARINDEX('-',field ))) + '%'
                                  ELSE field + '%' END
    INNER JOIN Table3 O
    ON      O.field = R.field 
    AND     O.field =
                    CASE
                        WHEN  R.field NOT LIKE '%-%' THEN 
RIGHT(R.field , LEN(R.field ) - 3) + '-0'
                    ELSE RIGHT(R.field , LEN(R.field ) - 3)
                    END                               
    INNER JOIN Table4 V 
    ON V.field = R.field 
    AND V.field = R.field 
    INNER JOIN Table5 poh
    ON         O.field =poh.field 
    AND       O.field = poh.field 
    AND       poh.field = sta.field 
    LEFT JOIN Table6 ppv
    ON ppv.field =poh.field 
    AND ppv.field =poh.field 
    AND ppv.field ='200001'
    LEFT JOIN Table7 ppw
    ON ppw.field ='001'
    AND ppw.field ='02'
    AND ppw.field =ppv.field 
    LEFT JOIN  dbo.Table8 pv
    ON   pv.field ='001'
    AND pv.field =ppw.field 

WHERE
   R.field IN ('001','011')
   AND O.field ='2018-02-28'
   AND R.field ='800155'
   AND poh.field NOT IN ('CPB','CP','C')
   AND poh.field NOT LIKE '9999%'

0 个答案:

没有答案