postgreSQL:按日期查询
发布网友
发布时间:2022-04-07 20:48
我来回答
共1个回答
热心网友
时间:2022-04-07 22:17
CREATE TABLE table1 (
id INT,
name VARCHAR(8),
createdate DATE
);
INSERT INTO table1
SELECT 1, 'name1', TO_DATE('2013-12-12', 'YYYY-MM-DD') UNION ALL
SELECT 2, 'name2', TO_DATE('2013-12-13', 'YYYY-MM-DD') UNION ALL
SELECT 3, 'name2', TO_DATE('2013-12-14', 'YYYY-MM-DD') UNION ALL
SELECT 4, 'name2', TO_DATE('2013-12-15', 'YYYY-MM-DD') UNION ALL
SELECT 5, 'name2', TO_DATE('2013-12-15', 'YYYY-MM-DD') UNION ALL
SELECT 6, 'name2', TO_DATE('2013-12-12', 'YYYY-MM-DD');
SELECT DISTINCT
(SELECT COUNT(1) FROM table1 t2 WHERE t1.createdate >= t2.createdate) AS count,
createdate
FROM
table1 t1
ORDER BY
createdate;
count | createdate
-------+------------
2 | 2013-12-12
3 | 2013-12-13
4 | 2013-12-14
6 | 2013-12-15
(4 行记录)