在表格中
CREATE TABLE "FILES"
(
"ID_FILE" NUMBER(38,0) NOT NULL ENABLE,
"REPORT_DATE" DATE,
"NAME" VARCHAR2(128 BYTE),
"PROCESSED" VARCHAR2(1 BYTE)
)
有些日期不同的记录,名称以' p1',' l1',' m1'以及是否加工 - ' N'和' Y'分别。 我想从最早的日期,未经处理的文件,这三个名称的包中选择,这些名称以' p1',' l1',' m1'结尾。 Oracle查询:
SELECT REPORT_DATE, DISTINCT NAME
FROM
( SELECT *
FROM FILES)
WHERE
( NAME LIKE '%p1' OR
NAME LIKE '%l1' OR
NAME LIKE '%m1') AND
ROWNUM = 3 AND
PROCESSED = 'N'
GROUP BY REPORT_DATE
HAVING MIN(REPORT_DATE);
错误是:
Error at Line: 12 Column: 23 (having clause)
修改:
记录如下:
(1, '11/05/2011', 'some_name_p1', 'N')
(2, '11/05/2011', 'some_name_l1', 'N')
(3, '11/05/2011', 'some_name_m1', 'N')
(4, '11/05/2011', 'some_name_k1', 'Y')
(5, '11/05/2012', 'some_name_p1', 'n')
(6, '11/05/2011', 'some_name_m1', 'N')
查询应该只选择前3行(1,2,3),仅此而已。这三行必须包含全部三个' p1',' l1',' m1'名称中的扩展名。
答案 0 :(得分:0)
HAVING
子句通常用于与聚合函数进行比较。您可能正在寻找的是将REPORT_DATE
与最小值匹配,您可以使用子查询:
SELECT
REPORT_DATE, DISTINCT NAME
FROM
(SELECT * FROM FILES)
WHERE
(NAME LIKE '%p1' OR
NAME LIKE '%l1' OR
NAME LIKE '%m1')
AND ROWNUM = 3
AND PROCESSED = 'N'
AND REPORT_DATE = (SELECT MIN(REPORT_DATE) FROM FILES WHERE NAME LIKE '%p1' OR NAME LIKE '%l1' OR NAME LIKE '%m1')
GROUP BY
REPORT_DATE;