oracle pl / saql ORA-01790:expression必须与对应的表达式具有相同的数据类型

时间:2014-06-05 06:41:31

标签: sql oracle

SELECT SUM(SALES_AMOUNT)SALES,
YEAR,
MONTH,
CATAGORY_ID,
SALES_PERSON_ID,
ITEM_TYPE_ID
FROM APEX_FINAL
    where sales_amount is not null
     and catagory_id is not null
GROUP BY (YEAR,MONTH,CATAGORY_ID,SALES_PERSON_ID,ITEM_TYPE_ID)

union all

SELECT SUM(SALES_AMOUNT)SALES,
YEAR,
MONTH,
CATAGORY_ID,
'all others' SALES_PERSON_ID,
ITEM_TYPE_ID
FROM APEX_FINAL
    where sales_amount is not null
    and sales_person_id is null 
GROUP BY (YEAR,MONTH,CATAGORY_ID,SALES_PERSON_ID,ITEM_TYPE_ID)

这是我的代码plz帮助我解决我的问题,因为我得到了错误的数学数据类型的eroor但所有数据类型相同

3 个答案:

答案 0 :(得分:6)

销售人员ID似乎不匹配。它可能是您表中的NUMERIC数据类型。

您可以在第一个Union Query中使用TO_CHAR(SALES_PERSON_ID),如下所示。它可以帮助您解决问题

SELECT SUM(SALES_AMOUNT)SALES,
YEAR,
MONTH,
CATAGORY_ID,
TO_CHAR(SALES_PERSON_ID) SALES_PERSON_ID,
ITEM_TYPE_ID
FROM APEX_FINAL
    where sales_amount is not null
     and catagory_id is not null
GROUP BY (YEAR,MONTH,CATAGORY_ID,SALES_PERSON_ID,ITEM_TYPE_ID)

union all

SELECT SUM(SALES_AMOUNT)SALES,
YEAR,
MONTH,
CATAGORY_ID,
'all others' SALES_PERSON_ID,
ITEM_TYPE_ID
FROM APEX_FINAL
    where sales_amount is not null
    and sales_person_id is null 
GROUP BY (YEAR,MONTH,CATAGORY_ID,SALES_PERSON_ID,ITEM_TYPE_ID)

答案 1 :(得分:1)

正如@Nisha已经指出的那样,您可能会收到此错误,因为SALES_PERSON_ID是一个数字字段 - 您必须将其转换为字符串或(最好)加入另一个表以获取销售人员的姓名。

此外,我相信您的查询中存在逻辑错误 - 在您的第一个SELECT中,您按catagory_person_id is not null过滤;这应该是sales_person_id is not null

假设是这种情况,那么您可以使用CASE语句简化查询(您也可以使用DECODECOALESCE):

SELECT 
  SUM(SALES_AMOUNT) SALES,
  YEAR,
  MONTH,
  CATAGORY_ID,
  (case 
     when sales_person_id is not null then to_char(SALES_PERSON_ID)
     else 'all others' 
   end) as sales_person_id,
  ITEM_TYPE_ID
FROM APEX_FINAL
where sales_amount is not null
GROUP BY (YEAR,MONTH,CATAGORY_ID,SALES_PERSON_ID,ITEM_TYPE_ID)

答案 2 :(得分:0)

我在一个类似的问题上苦苦挣扎,并意识到我们要合并的两个表的列顺序应该相同!

它仍然会给您同样的错误,因此请确保列的顺序相同:)