拉出的行数不一致

时间:2014-02-10 18:38:51

标签: sql sql-server sql-server-2008

尝试真正描绘一些数据。通过以下查询,我有时会得到一个包含所有NULLS的额外行。如果我在where语句中注释掉“isnumeric”,并使用当前注释掉的下面的那个,我得到一个一致的数字,但是当我切换回“isnumeric”时,我有时会得到那个NULL行。

我认为这句话在我的语法中有问题。任何人都有任何想法吗?

谢谢!

马特

select distinct 
   AROBP_A.IDINVC     as TAP_AROBP_A_IDINVC,
   AROBP_A.IDCUST     as TAP_AROBP_A_IDCUST,
   ARCUS.NAMECUST     as TAP_ARCUS_NAMECUST,
   ARIBH.DATEINVC     as TAP_ARIBH_DATEINVC,
   ARIBH.TEXTTRX      as TAP_ARIBH_TEXTTRX,
   ARIBH.AMTINVCTOT   as TAP_ARIBH_AMTINVCTOT_ABS,
   TAP_ARIBH_AMTINVCTOT =
                      case ARIBH.TEXTTRX
                      when 3
                      then - ARIBH.AMTINVCTOT
                      else ARIBH.AMTINVCTOT
                      end,
   AROBP_A.AMTPAYMTC  as TAP_AROBP_A_AMTPAYMTC,
   TAP_INV_DIFF =     case ARIBH.TEXTTRX
                      when 3
                      then AROBP_A.AMTPAYMTC - ARIBH.AMTINVCTOT
                      else AROBP_A.AMTPAYMTC + ARIBH.AMTINVCTOT
                      end,
   AROBP_A.DATEBTCH   as TAP_AROBP_A_DATEBTCH,
   AROBP_A.TRXTYPE    as TAP_AROBP_A_TRXTYPE,
   AROBP_A.TRANSTYPE  as TAP_AROBP_A_TRANSTYPE,
   AROBP_A.FISCPER    as TAP_AROBP_A_FISCPER,
   AROBP_A.FISCYR     as TAP_AROBP_A_FISCYR,
   ARSAP.NAMEEMPL     as TAP_ARSAP_NAMEEMPL,
   ARSAP2.NAMEEMPL    as TAP_ARSAP2_NAMEEMPL
into #TMPACCPAC
from GGIAPP01.GGI.dbo.ARIBH ARIBH
right join GGIAPP01.GGI.dbo.AROBP AROBP_N on ARIBH.IDINVC = AROBP_N.IDINVC
left  join GGIAPP01.GGI.dbo.AROBP AROBP_A on ARIBH.IDINVC = AROBP_A.IDINVC
left  join GGIAPP01.GGI.dbo.ARCUS ARCUS   on ARIBH.IDCUST = ARCUS.IDCUST
left  join GGIAPP01.GGI.dbo.ARSAP ARSAP   on ARCUS.CODESLSP1 = ARSAP.CODESLSP
left  join GGIAPP01.GGI.dbo.ARSAP ARSAP2  on ARIBH.CODESLSP1 = ARSAP2.CODESLSP
where AROBP_N.FISCYR = '2014' and AROBP_N.FISCPER = '03'
  and
  isnumeric(AROBP_N.IDINVC) = 1
  --AROBP_N.IDINVC not like '[A-Z]%' and AROBP_N.IDINVC not like '%[A-Z]'
order by TAP_ARSAP_NAMEEMPL, TAP_AROBP_A_IDCUST, TAP_AROBP_A_IDINVC;

select * from #TMPACCPAC
order by TAP_ARSAP_NAMEEMPL, TAP_AROBP_A_IDCUST, TAP_AROBP_A_IDINVC;

drop table #TMPACCPAC;

2 个答案:

答案 0 :(得分:0)

如下更改您的查询并再次测试,您的NULL行不应该回来。我认为您的问题来自RIGHTLEFT OUTER JOIN到同一个表GGIAPP01.GGI.dbo.AROBP。如果你不需要返回该表中的结果,如果不匹配的是GGIAPP01.GGI.dbo.ARIBH,那么我只需更改为LEFT OUTER JOIN,然后只需删除RIGHT加入其他明智的使用FULL OUTER JOIN因为它在查询中。

SELECT DISTINCT AROBP_A.IDINVC AS TAP_AROBP_A_IDINVC
       ,AROBP_A.IDCUST AS TAP_AROBP_A_IDCUST
       ,ARCUS.NAMECUST AS TAP_ARCUS_NAMECUST
       ,ARIBH.DATEINVC AS TAP_ARIBH_DATEINVC
       ,ARIBH.TEXTTRX AS TAP_ARIBH_TEXTTRX
       ,ARIBH.AMTINVCTOT AS TAP_ARIBH_AMTINVCTOT_ABS
       ,TAP_ARIBH_AMTINVCTOT = CASE ARIBH.TEXTTRX
                                 WHEN 3 THEN -ARIBH.AMTINVCTOT
                                 ELSE ARIBH.AMTINVCTOT
                               END
       ,AROBP_A.AMTPAYMTC AS TAP_AROBP_A_AMTPAYMTC
       ,TAP_INV_DIFF = CASE ARIBH.TEXTTRX
                         WHEN 3 THEN AROBP_A.AMTPAYMTC - ARIBH.AMTINVCTOT
                         ELSE AROBP_A.AMTPAYMTC + ARIBH.AMTINVCTOT
                       END
       ,AROBP_A.DATEBTCH AS TAP_AROBP_A_DATEBTCH
       ,AROBP_A.TRXTYPE AS TAP_AROBP_A_TRXTYPE
       ,AROBP_A.TRANSTYPE AS TAP_AROBP_A_TRANSTYPE
       ,AROBP_A.FISCPER AS TAP_AROBP_A_FISCPER
       ,AROBP_A.FISCYR AS TAP_AROBP_A_FISCYR
       ,ARSAP.NAMEEMPL AS TAP_ARSAP_NAMEEMPL
       ,ARSAP2.NAMEEMPL AS TAP_ARSAP2_NAMEEMPL
    INTO #TMPACCPAC
    FROM GGIAPP01.GGI.dbo.ARIBH ARIBH
    FULL OUTER JOIN GGIAPP01.GGI.dbo.AROBP AROBP_A
        ON ARIBH.IDINVC = AROBP_A.IDINVC
    LEFT  JOIN GGIAPP01.GGI.dbo.ARCUS ARCUS
        ON ARIBH.IDCUST = ARCUS.IDCUST
    LEFT  JOIN GGIAPP01.GGI.dbo.ARSAP ARSAP
        ON ARCUS.CODESLSP1 = ARSAP.CODESLSP
    LEFT  JOIN GGIAPP01.GGI.dbo.ARSAP ARSAP2
        ON ARIBH.CODESLSP1 = ARSAP2.CODESLSP
    WHERE AROBP_A.FISCYR = '2014'
        AND AROBP_A.FISCPER = '03'
        AND ISNUMERIC(AROBP_A.IDINVC) = 1
  --AROBP_A.IDINVC not like '[A-Z]%' and AROBP_A.IDINVC not like '%[A-Z]'
ORDER BY TAP_ARSAP_NAMEEMPL
       ,TAP_AROBP_A_IDCUST
       ,TAP_AROBP_A_IDINVC;

SELECT *
    FROM #TMPACCPAC
    ORDER BY TAP_ARSAP_NAMEEMPL
       ,TAP_AROBP_A_IDCUST
       ,TAP_AROBP_A_IDINVC;

DROP TABLE #TMPACCPAC;

而不是使用AND ISNUMERIC(AROBP_A.IDINVC) = 1

替换AND ISNUMERIC(ARIBH.IDINVC) = 1的测试

答案 1 :(得分:0)

这可能不是您问题的答案,但值得一看。ISNUMERIC()函数的问题有时会将字符值解析为数值。例如见下面......

测试数据

DECLARE @Table TABLE(Value NVARCHAR(100))
INSERT INTO @Table VALUES
('0123'),('123456'),('12345e2'),('1232e5'),
('aaa55'),('5655e512'),('asas5'),('aabbcc')

<强>查询

SELECT Value
      ,ISNUMERIC(Value) AS Is_Numeric
      ,CASE WHEN Value NOT LIKE '%[^0-9]%'
            THEN 1 ELSE 0 END 
                        AS Is_Like_Number
FROM @Table

结果集

╔══════════╦════════════╦════════════════╗
║  Value   ║ Is_Numeric ║ Is_Like_Number ║
╠══════════╬════════════╬════════════════╣
║ 0123     ║          1 ║              1 ║ 
║ 123456   ║          1 ║              1 ║
║ 12345e2  ║          1 ║              0 ║
║ 1232e5   ║          1 ║              0 ║
║ aaa55    ║          0 ║              0 ║
║ 5655e512 ║          0 ║              0 ║
║ asas5    ║          0 ║              0 ║
║ aabbcc   ║          0 ║              0 ║
╚══════════╩════════════╩════════════════╝

现在,如果您查看ISNUMERIC()函数和Like Operator返回的结果,则数字会将值12345e21232e5解析为数值。原因是ISNUMERIC函数将这些值视为12345x10^21232x10^5

因此,当您使用ISNUMERIC函数而不是LIKE运算符时,您将获得意外的重新连接。我建议坚持LIKE运算符它更可靠,更不容易出错。