Oracle SQL中的字符集不匹配

时间:2018-10-16 09:43:26

标签: sql oracle oracle-sqldeveloper

在Oracle SQL Developer 3.1.07中,下面的两个子查询可以单独正确运行。我要做的就是将它们结合在一起,但是Oracle提供了ORA-12704:字符集不匹配错误。我已经检查过类似的帖子,以检查UNION语法,但我不知道错误是从哪里来的。 select语句从两个查询的同一列中选择字段,因此应该没有数据类型问题。

    SELECT assessmentid,
       claimdate,
       firstdiagnosis,
       affectedsystemdesc,
       thirdparty,
       SUM(settledamount) AS sam,
       param
  FROM (
  SELECT x.xclaimassessment.assessmentid,
         x.xclaimassessment.thirdparty,
         x.xclaimassessment.firstdiagnosis,
         x.xclaimassessment.affectedsystemdesc,
         x.xclaimassessment.claimdate,
         x.xclaimtreatmentinvoice.settledamount,
         'Q1' AS param
    FROM x.xclaimassessment left
    JOIN x.xclaimtreatment
  ON x.xclaimassessment.assessmentid = x.xclaimtreatment.assessmentid
    LEFT JOIN x.xclaimtreatmentinvoice
  ON x.xclaimtreatment.treatmentid = x.xclaimtreatmentinvoice.treatmentid
   WHERE ( ( icd9 = '501'
      OR icd9 = '162' )
     AND thirdparty = 'N' )
     AND months_between(
    SYSDATE,
    TO_DATE(
      x.xclaimassessment.claimdate
    )
  ) < 25
)
 GROUP BY assessmentid,
          claimdate,
          firstdiagnosis,
          affectedsystemdesc,
          thirdparty,
          param
UNION
SELECT assessmentid,
       claimdate,
       firstdiagnosis,
       affectedsystemdesc,
       thirdparty,
       SUM(settledamount) AS sam,
       param
  FROM (
  SELECT x.xclaimassessment.assessmentid,
         x.xclaimassessment.thirdparty,
         x.xclaimassessment.firstdiagnosis,
         x.xclaimassessment.affectedsystemdesc,
         x.xclaimassessment.claimdate,
         x.xclaimtreatmentinvoice.settledamount,
         'Q2' AS param
    FROM x.xclaimassessment left
    JOIN x.xclaimtreatment
  ON x.xclaimassessment.assessmentid = x.xclaimtreatment.assessmentid
    LEFT JOIN x.xclaimtreatmentinvoice
  ON x.xclaimtreatment.treatmentid = x.xclaimtreatmentinvoice.treatmentid
   WHERE months_between(
    SYSDATE,
    TO_DATE(
      x.xclaimassessment.claimdate
    )
  ) < 25
)
 GROUP BY assessmentid,
          claimdate,
          firstdiagnosis,
          affectedsystemdesc,
          thirdparty,
          param
HAVING SUM(settledamount) > 50000;

我们将不胜感激!

1 个答案:

答案 0 :(得分:0)

好的,我的经理K先生确实解决了这个问题。

问题出在THIRDPARTY列,这两个子查询中的数据类型相同。为了解决该问题,我们在两个子查询中都使用了CAST( field AS VARCHAR(3))。

我们通过从查询中删除除第一个字段之外的所有字段,然后逐个添加它们,直到出现问题,来确定问题字段。

我们仍然不明白的是为什么会发生此错误,因此仍然可以理解与此有关的任何信息。这是已解决的代码:

SELECT ASSESSMENTID,CLAIMDATE, FIRSTDIAGNOSIS, AFFECTEDSYSTEMDESC, THIRDP, SUM(SETTLEDAMOUNT) AS SAM, PARAM
FROM (
      SELECT X.XCLAIMASSESSMENT.ASSESSMENTID, CAST(X.XCLAIMASSESSMENT.THIRDPARTY AS VARCHAR(3)) AS THIRDP,
      X.XCLAIMASSESSMENT.FIRSTDIAGNOSIS, X.XCLAIMASSESSMENT.AFFECTEDSYSTEMDESC, 
      X.XCLAIMASSESSMENT.CLAIMDATE, X.XCLAIMTREATMENTINVOICE.SETTLEDAMOUNT, 'Q1' as PARAM
      FROM X.XCLAIMASSESSMENT
      LEFT JOIN X.XCLAIMTREATMENT
      ON X.XCLAIMASSESSMENT.ASSESSMENTID = X.XCLAIMTREATMENT.ASSESSMENTID
      LEFT JOIN X.XCLAIMTREATMENTINVOICE
      ON X.XCLAIMTREATMENT.TREATMENTID = X.XCLAIMTREATMENTINVOICE.TREATMENTID
      WHERE ((ICD9 = '501' or ICD9 = '162') and THIRDPARTY = 'N')and MONTHS_BETWEEN(SYSDATE,to_date(X.XCLAIMASSESSMENT.CLAIMDATE))< 25)
      GROUP BY  ASSESSMENTID, CLAIMDATE, FIRSTDIAGNOSIS, AFFECTEDSYSTEMDESC, THIRDP, PARAM
      UNION
SELECT ASSESSMENTID, CLAIMDATE, FIRSTDIAGNOSIS, AFFECTEDSYSTEMDESC, THIRDP, SUM(SETTLEDAMOUNT) AS SAM, PARAM
FROM(
      SELECT X.XCLAIMASSESSMENT.ASSESSMENTID, CAST(X.XCLAIMASSESSMENT.THIRDPARTY AS VARCHAR(3)) AS THIRDP,
      X.XCLAIMASSESSMENT.FIRSTDIAGNOSIS, X.XCLAIMASSESSMENT.AFFECTEDSYSTEMDESC, 
      X.XCLAIMASSESSMENT.CLAIMDATE, X.XCLAIMTREATMENTINVOICE.SETTLEDAMOUNT, 'Q2' as PARAM
      FROM X.XCLAIMASSESSMENT
      LEFT JOIN X.XCLAIMTREATMENT
      ON X.XCLAIMASSESSMENT.ASSESSMENTID = X.XCLAIMTREATMENT.ASSESSMENTID
      LEFT JOIN X.XCLAIMTREATMENTINVOICE
      ON X.XCLAIMTREATMENT.TREATMENTID = X.XCLAIMTREATMENTINVOICE.TREATMENTID
      WHERE MONTHS_BETWEEN(SYSDATE,to_date(X.XCLAIMASSESSMENT.CLAIMDATE))< 25)
      GROUP BY ASSESSMENTID, CLAIMDATE, FIRSTDIAGNOSIS, AFFECTEDSYSTEMDESC, THIRDP, PARAM
      HAVING SUM(SETTLEDAMOUNT) > 50000;