Oracle SQL查询未显示正确的值

时间:2015-04-30 08:38:16

标签: sql oracle select count

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

  • id
  • 技术
  • name_event
  • time_event

我使用以下查询计算表中值的数量

    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 个答案:

答案 0 :(得分:1)

在您的查询中,您 <!DOCTYPE html> <meta charset="utf-8"> <head> <style> .bar2 { fill: #00AF9D; } .bar{ fill: #FF5A00; } .axis text { font-family: 'Open Sans', sans-serif; font-size: 2vw; color:grey; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .svg{ position: absolute; top: 5vh; left: 5vw; border: 1px solid #A0A0A0 ; height: 90vh; width: 90vw; border-radius: 25px; box-shadow: 3vh 3vh 3vh #D8D8D8 ; background-color:#FFFFFF;} .lab{ font-family: 'Open Sans', sans-serif; font-size: 1vw; color:grey; } </style> </head> <body> <script src="http://d3js.org/d3.v3.min.js"></script> <script> var w = window.innerWidth; var h = window.innerHeight; data = [{ name: "A", value: 1, value2: 1 }, { name: "B", value: 4, value2: 5 }, { name: "C", value: 17, value2:18 }, { name: "D", value: 30, value2: 30 }, { name: "E", value: 60, value2: 60 }, { name: "F", value: 100, value2: 100 }] var margin = { top: 0.01*h, right: 0.05*w, bottom:0.02*h, left: 0.05*w }, width = 0.9*w - margin.left - margin.right, height =0.9*h - margin.top - margin.bottom; var x = d3.scale.linear() .range([0, width]) var y = d3.scale.ordinal() .rangeRoundBands([0, height], .2); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .attr("class","svg") .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); x.domain([-100,100]) y.domain(data.map(function (d) { return d.name; })); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function (d) {return x(Math.min(0, d.value));}) .attr("y", function (d) {return y(d.name);}) .attr("width", function (d) {return Math.abs(x(d.value) - x(0));}) .attr("height", y.rangeBand()) .on("mouseover", function(){return tooltip.style("visibility", "visible");}) .on("mousemove", function(){return tooltip.style("top", (event.pageY-10)+"px").style( "left",(event.pageX+10)+"px");}) .on("mouseout", function(){return tooltip.style("visibility", "hidden");}); svg.selectAll("text") .data(data) .enter() .append("text") .attr("class", "lab") .text(function (d){return d.value+"%";}) .attr("text-anchor", "middle") .attr("font-family", "sans-serif") .attr("font-size", "10px") .attr("fill", "black") .attr("x", function (d) {return x(Math.min(0, d.value))+(Math.abs(x(d.value) - x(0))/2);}) .attr("y", function (d) {return y(d.name) + (y.rangeBand())/2 ;}) svg.selectAll(".bar2") .data(data) .enter().append("rect") .attr("class", "bar2") .attr("x", function (d) {return x(Math.min(0, -d.value2));}) .attr("y", function (d) {return y(d.name);}) .attr("width", function (d) {return Math.abs(x(-d.value2) - x(0));}) .attr("height", y.rangeBand()) .on("mouseover", function(){return tooltip.style("visibility", "visible");}) .on("mousemove", function(){return tooltip.style("top", (event.pageY-10)+"px").style( "left",(event.pageX+10)+"px");}) .on("mouseout", function(){return tooltip.style("visibility", "hidden");}); svg.selectAll("text1") .data(data) .enter() .append("text") .attr("class", "lab") .text(function (d){return d.value2+"%";}) .attr("text-anchor", "middle") .attr("font-family", "sans-serif") .attr("font-size", "10px") .attr("fill", "black") .attr("x", function (d) {return x(Math.min(0, -d.value2))+(Math.abs(x(-d.value2) - x(0))/2);}) .attr("y", function (d) {return y(d.name)+(y.rangeBand())/2;}); svg.append("g") .attr("class", "x axis") .call(xAxis) .attr("transform", "translate(0," + (height -margin.bottom) + ")"); svg.append("g") .attr("class", "y axis") .append("line") .attr("x1", x(0)) .attr("x2", x(0)) .attr("y2", height -margin.bottom); svg.append("g") .attr("class", "y axis") .append("line") .attr("x1", x(0)) .attr("x2", x(0)) .attr("y2", height -margin.bottom); function type(d) { d.value = +d.value; return d; }; //////////////////////////Tool Tip/////////////////////////////////// var tooltip = d3.select("body") .append("div") .style("position", "absolute") .style("z-index", "10") .style("visibility", "hidden") //.text(function (d){return d.value2+"%";}) .text("tooltip") </script> </body> </html> join,但随后使用dm2相关子查询(并在该子查询中使用相同的别名)。我无法通过我的设置复制问题,但这确实会让人感到困惑!所以试试没有db的第二个副本,只需保留NOT EXISTS

NOT EXISTS

答案 1 :(得分:0)

您在表中使用了2个以上的插入,因此现在您有10 + 2个总共12个插入,以移动数据,使用更新语句。

update db set name_event='status2' where id=4;
update db set name_event='status4' where id=1;