我无法在这里找到我想要的答案,所以我在 Oracle 论坛上发布了这个问题。我得到的答案正是我想要的。以下查询仅通过表一次且没有任何连接即可获得答案...
SELECT pid,
MAX(CASE
WHEN value_date > TRUNC (SYSDATE) - 6 THEN my_value
ELSE 0
END)
max_7_days,
MAX(value_date)
KEEP (DENSE_RANK FIRST ORDER BY
(CASE
WHEN value_date >
TRUNC (SYSDATE) - 6
THEN
my_value
ELSE
0
END) DESC)
day_7_days,
MAX(CASE
WHEN value_date > TRUNC (SYSDATE) - 29 THEN my_value
ELSE 0
END)
max_30_days,
MAX(value_date)
KEEP (DENSE_RANK FIRST ORDER BY
CASE
WHEN value_date >
TRUNC (SYSDATE) - 29
THEN
my_value
ELSE
0
END DESC)
day_30_days
FROM my_table
GROUP BY pid;
如果有人想测试它,这里有一些示例数据。
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 300, '18-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 200, '17-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 4500, '16-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 800, '15-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 12000, '14-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 21000, '13-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 5600, '12-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 18400, '11-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 7200, '10-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 24000, '09-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 8800, '08-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 6000, '07-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 44200, '06-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 2800, '05-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 58500, '04-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 8000, '03-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 30600, '02-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 7200, '01-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 62700, '31-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 48000, '30-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 16800, '29-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 83600, '28-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 18400, '27-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 16800, '26-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 40000, '25-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 23400, '24-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 99900, '23-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 67200, '22-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 5800, '21-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 57000, '20-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 96100, '19-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 64000, '18-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 52800, '17-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 71400, '16-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 14000, '15-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 7200, '14-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 111000, '13-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 64600, '12-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 113100, '11-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (1, 84000, '10-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 6000, '18-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 7800, '17-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 13500, '16-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 25600, '15-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 276000, '14-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 210000, '13-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 162400, '12-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 36800, '11-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 86400, '10-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 480000, '09-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 8800, '08-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 102000, '07-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,1237600, '06-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 61600, '05-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,1287000, '04-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 144000, '03-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 275400, '02-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 93600, '01-FEB-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,1630200, '31-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,1248000, '30-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 235200, '29-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,3176800, '28-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 73600, '27-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 403200, '26-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 200000, '25-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 280800, '24-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 599400, '23-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,1612800, '22-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 110200, '21-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,1653000, '20-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,2498600, '19-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,2368000, '18-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 211200, '17-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,2570400, '16-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 224000, '15-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 223200, '14-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2, 999000, '13-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,2325600, '12-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,1017900, '11-JAN-10');
INSERT INTO my_table (pid, my_value, value_date) VALUES (2,2352000, '10-JAN-10');
感谢大家为回答这个问题所做的努力!
卡尔