简体   繁体   English

为什么Count不在我的oracle sql中工作

[英]Why doesn't Count work in my oracle sql

I am trying to do a count of items related to a person for my query, but I keep getting a 我正在尝试为我的查询计算一个与人相关的项目,但我不断得到一个

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    The specified number was invalid.
*Action:   Specify a valid number.

Here is my query: 这是我的查询:

    select distinct
    p.person_id as id,
    p.fullname as name, 
    p.email, 
    (select count(*) from ASSETS_MASTER m, PERSON p where m.asset_user = p.person_id) as Assets,
    p.last_updated
 from person p 
where p.deleted = 0
order by p.fullname asc;

the error is because of : 错误是因为:

select count(*) from ASSETS_MASTER m, PERSON p where m.asset_user = p.person_id

in which case : 在这种情况下 :

asset_user is datatype varchar2(255) asset_user是数据类型varchar2(255)

person_id is datatype number person_id是数据类型编号

but if I run 但如果我跑

select asset_id from ASSETS_MASTER m, PERSON p where m.asset_user = p.person_id

this will show results of 这将显示结果

CFRI1823m
5384w
CFRI5039
CFRI2319
CFRI5024
....

Can anyone tell me what I am getting this error? 任何人都可以告诉我我得到的错误是什么?

To further illustrate the point I made in a Comment, which was: 为了进一步说明我在评论中提出的观点,即:

We can't tell without seeing your data. 没有看到您的数据,我们无法分辨。 Are you perhaps trying to compare a string ( m.asset_user ) with a number ( p.person_id ), or something of that nature? 您是尝试将字符串( m.asset_user )与数字( p.person_id )或某种性质的东西进行比较吗?

Here is a similar example using the EMPLOYEES and DEPARTMENTS tables in the standard HR schema (which exists by default, after installation, on most Oracle systems): 下面是使用标准HR模式中的EMPLOYEES和DEPARTMENTS表的类似示例(在大多数Oracle系统上,默认情况下,安装后存在):

select count(*) from hr.employees e, hr.departments d 
                where e.department_id = d.department_name;

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    The specified number was invalid.
*Action:   Specify a valid number.

Mathguy has an interesting explanation in the comments above. Mathguy在上面的评论中有一个有趣的解释。 I don't know why you have your asset_user as VARCHAR if it is a number, and you might want to consider modifying the data type if you can. 我不知道为什么你的asset_user为VARCHAR,如果它是一个数字,你可能想考虑修改数据类型。 However, if it is possible to type convert to a number (ie your asset_user doesn't have values like AD34 ), you should be able to fix your query by doing this: 但是,如果可以键入转换为数字(即您的asset_user没有像AD34这样的值),您应该能够通过执行以下操作来修复查询:

select count(*) from ASSETS_MASTER m, PERSON p 
where CAST(m.asset_user AS INT) = p.person_id

You can try this: 你可以试试这个:

select count(*) from ASSETS_MASTER m, PERSON p where m.asset_user = '' || p.person_id

but it will prevent the use of the index of p.person_id in this case you'll have to create a functional index of to_char(person_id) 但它会阻止使用p.person_id的索引在这种情况下你必须创建一个功能索引to_char(person_id)

Answering your question directly - in your query Oracle implicitly calls to_number for varchar m.asset_user. 直接回答您的问题 - 在您的查询中,Oracle隐式调用to_number以获取varchar m.asset_user。 It works fine until m.asset_user is assigned to something like '12345' or null or even ''... but once it meets something like '1 23' - it returns invalid number. 它工作正常,直到m.asset_user被分配给类似'12345'或null或甚至''的东西......但是一旦它遇到类似'1 23'的东西 - 它返回无效的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM