分析功能是为此而设计的 - 只是GROUP BY
适当的列(在这种情况下REPORTER
)并用于MAX( <column_name> ) KEEP ( DENSE_RANK LAST ORDER BY <ordering_column> )
其他每一行。
SQL小提琴
Oracle 11g R2 模式设置:
CREATE TABLE tbl ( REPORTER, SUMMARY, CREATED, UPDATED, status, servicetype ) AS
SELECT 'e159299', 'Route card from August', '7/29/2013', '22:46.0', 5, 'New Route Card' FROM DUAL
UNION ALL SELECT 'e159299', 'I have returned the', '6/11/2013', '32:09.7', 5, 'Cancel Route Card' FROM DUAL
UNION ALL SELECT 'e159324', 'New Route Card: RBI', '1/2/2013', '00:51.0', 5, 'New Route Card' FROM DUAL
UNION ALL SELECT 'e159324', 'Hi, I would require', '10/30/2013', '35:23.0', 5, 'New Route Card' FROM DUAL
UNION ALL SELECT 'e159299', 'Cancel Route Card', '4/30/2013', '53:26.2', 5, 'Cancel Route Card' FROM DUAL;
查询 1:
SELECT REPORTER,
MAX( SUMMARY ) KEEP ( DENSE_RANK LAST ORDER BY TO_DATE( CREATED, 'MM/DD/YYYY' ) ) AS SUMMARY,
MAX( CREATED ) KEEP ( DENSE_RANK LAST ORDER BY TO_DATE( CREATED, 'MM/DD/YYYY' ) ) AS CREATED,
MAX( UPDATED ) KEEP ( DENSE_RANK LAST ORDER BY TO_DATE( CREATED, 'MM/DD/YYYY' ) ) AS UPDATED,
MAX( status ) KEEP ( DENSE_RANK LAST ORDER BY TO_DATE( CREATED, 'MM/DD/YYYY' ) ) AS status,
MAX( servicetype ) KEEP ( DENSE_RANK LAST ORDER BY TO_DATE( CREATED, 'MM/DD/YYYY' ) ) AS servicetype
FROM tbl
GROUP BY REPORTER
结果:
| REPORTER | SUMMARY | CREATED | UPDATED | STATUS | SERVICETYPE |
|----------|------------------------|------------|---------|--------|----------------|
| e159299 | Route card from August | 7/29/2013 | 22:46.0 | 5 | New Route Card |
| e159324 | Hi, I would require | 10/30/2013 | 35:23.0 | 5 | New Route Card |