Recently I wanna make a sql query on PostgreSQL that can do a counting of a table records that of everyday (or maybe every hour) of a certain period (a month or a day etc).
A loop through every day of a period (month), in other words.
How can I do this?
I wish the record could be something similar to this:
--------------------
count | day | month |
1 | 1 | 4 |
1 | 2 | 4 |
1 | 3 | 4 |
1 | 4 | 4 |
1 | 5 | 4 |
Update:
I have a table called visitors:
first_seen:date_time; last_seen: date time; id: integer
I wanna calculate total visitor of every day of a month. But since a visitor can be shown up more than multiple day a time, i.e stayed for 3 days. I found a simple count(id) wont do the trick.
Update 2:
This is what i did so far, but apparently wrong.
SELECT count("visitors"."id"),
EXTRACT(hour from visitors.last_seen) AS hour,
EXTRACT(day from "visitors"."last_seen") AS date,
EXTRACT(month from "visitors"."last_seen") AS month
FROM "visitors"
WHERE (("visitors"."first_seen" between '2014-04-09 15:30:07.423401' and '2014-04-10 15:30:07.423476')
or ("visitors"."last_seen" between '2014-04-09 15:30:07.423401' and '2014-04-10 15:30:07.423476')
or ("visitors"."first_seen" < '2014-04-09 15:30:07.423401' and "visotors".last_seen > '2014-04-10 15:30:07.423476'))
GROUP BY month,date,hour ORDER BY month asc, date asc , hour asc
Sorry for the lack of informations.