Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle SQL查询未显示正确的值_Sql_Oracle_Select_Count - Fatal编程技术网

Oracle SQL查询未显示正确的值

Oracle SQL查询未显示正确的值,sql,oracle,select,count,Sql,Oracle,Select,Count,我有一个包含以下列的表: 身份证件 技术 活动名称 时间事件 我使用以下查询计算表中的值数 select 1, 'Folder' as "Technology", 'Status' as "Name_Event", count(dm1.id) as "number of items", max(TO_CHAR(TO_DATE('20000101','yyyymmdd')+(SYSDATE - dm1.time_event),'hh24:mi:ss')) as "Time in syst

我有一个包含以下列的表:

身份证件 技术 活动名称 时间事件 我使用以下查询计算表中的值数

    select  1, 'Folder' as "Technology", 'Status' as "Name_Event", count(dm1.id) as "number of items", max(TO_CHAR(TO_DATE('20000101','yyyymmdd')+(SYSDATE - dm1.time_event),'hh24:mi:ss')) as "Time in system" 
    from db dm1 join db dm2 on dm1.id = dm2.id
    where dm1.technology = 'Folder' and dm1.name_event = 'status1' and NOT EXISTS(SELECT 1 FROM db dm2 WHERE dm2.name_event = 'status2' and dm2.id = dm1.id)
我做了以下插入

    INSERT INTO DB(id, technology, name_event, time_event) VALUES(1,'Folder', 'status1', 01:00:00);
    INSERT INTO DB(id, technology, name_event, time_event) VALUES(2,'Folder', 'status1', 01:00:00);
    INSERT INTO DB(id, technology, name_event, time_event) VALUES(3,'Folder', 'status1', 01:00:00);
    INSERT INTO DB(id, technology, name_event, time_event) VALUES(4,'Folder', 'status1', 01:00:00);
    INSERT INTO DB(id, technology, name_event, time_event) VALUES(5,'Folder', 'status1', 01:00:00);
    INSERT INTO DB(id, technology, name_event, time_event) VALUES(6,'Folder', 'status1', 01:00:00);
    INSERT INTO DB(id, technology, name_event, time_event) VALUES(7,'Folder', 'status1', 01:00:00);
    INSERT INTO DB(id, technology, name_event, time_event) VALUES(8,'Folder', 'status1', 01:00:00);
    INSERT INTO DB(id, technology, name_event, time_event) VALUES(9,'Folder', 'status1', 01:00:00);
    INSERT INTO DB(id, technology, name_event, time_event) VALUES(10,'Folder', 'status1', 01:00:00);
INSERT INTO DB(id, technology, name_event, time_event) VALUES(4,'Folder', 'status2', 02:00:00);
INSERT INTO DB(id, technology, name_event, time_event) VALUES(1,'Folder', 'status2', 03:00:00);
我在状态1中插入了10行,并将状态2移动到状态2。因此,当我执行查询时,状态1中应该有8项,但结果是10


我做错了什么?

您在表中又使用了2个插入,所以现在您总共有10+2个插入,要移动数据,请使用update语句

update db set name_event='status2' where id=4;
update db set name_event='status4' where id=1;
在查询中,您加入dm2,但随后使用不存在的相关子查询,并在该子查询中使用相同的别名。我无法复制我的设置中的问题,但这肯定会混淆一些东西!因此,请尝试不使用db的第二个副本,只保留不存在的副本:


将这10条insert语句与create table脚本一起提供如何?除非我们有您的示例数据,否则我们很难看出您做错了什么-您能添加表定义和insert语句吗?您能在表中提供一些示例数据吗,然后我们可以更快速地提供答案,您是否在同一会话中运行insert、move命令和sql?如果不执行提交并在其他会话中运行sql,它仍将显示旧值。*只是在这里检查一下。你确定问题出在查询中吗?如何移动值?我无法使用更新,因为我需要保留原始行-@吉尔,你能详细说明一下你的要求吗?如果保留原始行,则最终将得到两个数据,重复id,否则可以保留一个触发器,并将数据移动到另一个表中,该表将保存旧数据,该表将反映新的更改。我的要求是,在发生状态更改时进行插入。在这之后,我做了很多分析,因为这是一个监控表。使用重复的表是愚蠢的,因为实际上有10个以上的状态,但我简化了我的问题。如果从db dm1中选择COUNT*,其中dm1.technology='Folder'和dm1.name_event='status1',会得到什么?或者做一个SELECT*FROM…-似乎其他人在重新创建时也有同样的困难,这可能表明您的数据中发生了一些有趣的事情至少它现在通过您的代码工作了,我不知道为什么它没有首先发生,可能需要进行一次提交。谢谢
select  1, 'Folder' as "Technology", 'Status' as "Name_Event", count(dm1.id) as "number of items", max(TO_CHAR(TO_DATE('20000101','yyyymmdd')+(SYSDATE - dm1.time_event),'hh24:mi:ss')) as "Time in system" 
from db dm1 
where dm1.technology = 'Folder' and dm1.name_event = 'status1' and NOT EXISTS(SELECT 1 FROM db dm2 WHERE dm2.name_event = 'status2' and dm2.id = dm1.id)