0

我正在尝试查找在 2012 年 5 月 1 日至 2012 年 5 月 30 日之间开放的开放票。

所以,我应该得到:5 月 1 日:0,5 月 2 日 0,3 日:2

但我目前得到的是 5 月 1 日:2、2 日:2、3 日:2,然后每 3 天开始上涨

数据 :

CREATE TABLE bugs
(
    BUG_ID          NUMBER PRIMARY KEY,
    REPORTED_DATE   DATE NOT NULL,
    DESCRIPTION     VARCHAR2(20),
    PRIORITY        NUMBER(2),
    ASSIGNED_TO     VARCHAR2(10),
    CLOSED_DATE     DATE,
    NOTE            VARCHAR2(20)
);

INSERT INTO BUGS VALUES (1230, '25-APR-12', NULL, 3, 'Team 3', '28-APR-12', NULL); 
INSERT INTO BUGS VALUES (1231, '29-APR-12', NULL, 1, 'Team 1', '29-APR-12', NULL); 
INSERT INTO BUGS VALUES (1232, '03-MAY-12', NULL, 1, 'Team 1', '03-MAY-12', NULL); 
INSERT INTO BUGS VALUES (1233, '03-MAY-12', NULL, 1, 'Team 3', '08-MAY-12', NULL);
INSERT INTO BUGS VALUES (1234, '04-MAY-12', NULL, 2, 'Team 5', '15-MAY-12', NULL);
INSERT INTO BUGS VALUES (1235, '04-MAY-12', NULL, 2, 'Team 1',  NULL,       NULL);
INSERT INTO BUGS VALUES (1236, '05-MAY-12', NULL, 1, 'Team 2', '06-MAY-12', NULL);
INSERT INTO BUGS VALUES (1237, '05-MAY-12', NULL, 3, 'Team 3', '10-MAY-12', NULL);
INSERT INTO BUGS VALUES (1238, '09-MAY-12', NULL, 4, 'Team 5', '16-MAY-12', NULL);
INSERT INTO BUGS VALUES (1239, '09-MAY-12', NULL, 5, 'Team 6',  NULL,       NULL);
INSERT INTO BUGS VALUES (1240, '12-MAY-12', NULL, 5, 'Team 2', '30-MAY-12', NULL);
INSERT INTO BUGS VALUES (1241, '12-MAY-12', NULL, 1, 'Team 1', '12-MAY-12', NULL);
INSERT INTO BUGS VALUES (1242, '13-MAY-12', NULL, 4, 'Team 4', '20-MAY-12', NULL);
INSERT INTO BUGS VALUES (1243, '15-MAY-12', NULL, 4, 'Team 3', '01-AUG-12', NULL);
INSERT INTO BUGS VALUES (1244, '15-MAY-12', NULL, 2, 'Team 4', '20-MAY-12', NULL);
INSERT INTO BUGS VALUES (1245, '20-MAY-12', NULL, 2, 'Team 4',  NULL,       NULL);
INSERT INTO BUGS VALUES (1246, '22-MAY-12', NULL, 2, 'Team 4', '23-MAY-12', NULL);
INSERT INTO BUGS VALUES (1247, '25-MAY-12', NULL, 2, 'Team 1', '29-MAY-12', NULL);
INSERT INTO BUGS VALUES (1248, '30-MAY-12', NULL, 1, 'Team 1', '01-JUN-12', NULL);
INSERT INTO BUGS VALUES (1249, '05-JUN-12', NULL, 1, 'Team 2', '07-JUN-12', NULL);
COMMIT;

到目前为止我的代码:

DECLARE
  v_date bugs.closed_date%type     := '01-may-12';
  v_end_date bugs.closed_date%type := '20-may-12';
  v_openbugs NUMBER(10);
BEGIN
  WHILE v_date <= v_end_date
  LOOP
    SELECT COUNT(bug_id)
    INTO v_openbugs
    FROM bugs
    WHERE bugs.reported_date < v_date
    AND v_date > bugs.closed_date;
    DBMS_OUTPUT.PUT_LINE (v_date ||'   '|| v_openbugs);
    v_date := v_date + 1;
  END LOOP;
END;

我需要在 PL/SQL 中执行此操作。

4

2 回答 2

0

您可以只使用 SQL 来管理它:

WITH DAYS AS
    (SELECT 
        TO_DATE('06/01/2012', 'MM/DD/YYYY') - ROWNUM D
    FROM
        DUAL
    WHERE ROWNUM < 32)
SELECT
    DAYS.D,
    SUM(CASE 
        WHEN B.REPORTED_DATE IS NULL THEN 
            0 
        ELSE 
            1 
        END)
FROM
    DAYS 
    LEFT OUTER JOIN BUGS B
    ON (DAYS.D BETWEEN B.REPORTED_DATE AND NVL(CLOSED_DATE, DAYS.D))
GROUP BY DAYS.D
ORDER BY DAYS.D ASC

如果您要使其成为自动报告,那么我会将日期作为参数传递并将结果存储在游标中,并让调用过程根据需要格式化输出。

于 2013-04-17T02:24:23.033 回答
0

这是我得到的答案

DECLARE
  v_date bugs.closed_date%type     := '01-may-12';
  v_end_date bugs.closed_date%type := '31-may-12';
  v_openbugs NUMBER(10);
  v_maxbug number(10):=0;
BEGIN
DBMS_OUTPUT.PUT_LINE ('Date' ||'   '|| 'Open bugs');
  WHILE v_date <= v_end_date
  LOOP
    SELECT COUNT(bug_id)
    INTO v_openbugs
    FROM bugs
    WHERE bugs.reported_date <= to_date(v_date)
    AND nvl(bugs.closed_date,'31-may-12') >= to_date(v_date);
   if v_maxbug < v_openbugs then v_maxbug:=v_openbugs; end if;
    DBMS_OUTPUT.PUT_LINE (v_date ||'   '|| v_openbugs);
    v_date := v_date + 1;
  END LOOP;
   DBMS_OUTPUT.PUT_LINE ('===========================');
   DBMS_OUTPUT.PUT_LINE ('the max number of open bugs is:'|| v_maxbug);
END;
于 2014-07-21T19:20:34.790 回答