在2个日期之间打开门票

时间:2013-04-17 01:16:54

标签: oracle plsql oracle11g

我正试图找到2012年1月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中执行此操作。

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分: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;