所以我有桌子筛选:
Name Null? Type
--------------------------------------- -------- -------------
SCREENINGNO NOT NULL VARCHAR2(8)
CINEMANO VARCHAR2(8)
MOVIENO VARCHAR2(8)
SUPERVISORNO VARCHAR2(8)
CAMERAOPNO VARCHAR2(8)
HOURSTART NUMBER(38)
SCREENINGDATE DATE
问题是"显示2015年每个月安排的放映次数。"
我试过了:
SELECT to_char(s1.screeningdate, 'mm') AS month
,count(s2.screeningno) AS number_of_times_screened
FROM screening s1
,screening s2
WHERE s1.movieno(+) = s2.movieno
AND to_char(s1.screeningdate, 'yy') = '15'
GROUP BY to_char(s1.screeningdate, 'mm')
ORDER BY to_char(s1.screeningdate, 'mm');
它返回了:
MO NUMBER_OF_TIMES_SCREENED
-- ------------------------
01 22
02 31
03 24
04 32
05 26
06 37
07 15
08 15
10 10
11 5
12 8
选择了11行。
它只返回11行,我需要它来显示9月。有人可以帮忙吗?
答案 0 :(得分:1)
这样做的一种方法是使用带有dual
子句的connect by level
psedotable来生成1到12之间的所有数字,并将其加入到筛选计数中:
SELECT d.month, cnt
FROM (SELECT LPAD(LEVEL, 2, '0') AS month
FROM dual
CONNECT BY LEVEL <= 12) d
LEFT JOIN (SELECT TO_CHAR(screeningdate, 'mm') AS month, COUNT(*) AS cnt
FROM screeninginfo
WHERE TO_CHAR(screeningdate, 'yy') = '15'
GROUP BY TO_CHAR(screeningdate, 'mm')) s ON d.month = s.month
ORDER BY 1 ASC