generate-series

Best way to count records by arbitrary time intervals in Rails+Postgres

只愿长相守 提交于 2019-11-26 09:23:51
问题 My app has a Events table with time-stamped events. I need to report the count of events during each of the most recent N time intervals. For different reports, the interval could be \"each week\" or \"each day\" or \"each hour\" or \"each 15-minute interval\". For example, a user can display how many orders they received each week, day, or hour, or quarter-hour. 1) My preference is to dynamically do a single SQL query (I\'m using Postgres) that groups by an arbitrary time interval. Is there

Using sql function generate_series() in redshift

送分小仙女□ 提交于 2019-11-26 09:03:35
问题 I\'d like to use the generate series function in redshift, but have not been successful. The redshift documentation says it\'s not supported. The following code does work: select * from generate_series(1,10,1) outputs: 1 2 3 ... 10 I\'d like to do the same with dates. I\'ve tried a number of variations, including: select * from generate_series(date(\'2008-10-01\'),date(\'2008-10-10 00:00:00\'),1) kicks out: ERROR: function generate_series(date, date, integer) does not exist Hint: No function

Generating time series between two dates in PostgreSQL

China☆狼群 提交于 2019-11-26 07:59:01
I have a query like this that nicely generates a series of dates between 2 given dates: select date '2004-03-07' + j - i as AllDate from generate_series(0, extract(doy from date '2004-03-07')::int - 1) as i, generate_series(0, extract(doy from date '2004-08-16')::int - 1) as j It generates 162 dates between 2004-03-07 and 2004-08-16 and this what I want. The problem with this code is that it wouldn't give the right answer when the two dates are from different years, for example when I try 2007-02-01 and 2008-04-01 . Is there a better solution? Can be done without conversion to/from int (but to

generate_series() equivalent in MySQL

╄→尐↘猪︶ㄣ 提交于 2019-11-26 04:55:00
I need to do a query and join with all days of the year but in my db there isn't a calendar table. After google-ing I found generate_series() in PostgreSQL. Does MySQL have anything similar? My actual table has something like: date qty 1-1-11 3 1-1-11 4 4-1-11 2 6-1-11 5 But my query has to return: 1-1-11 7 2-1-11 0 3-1-11 0 4-1-11 2 and so on .. Karolis This is how I do it. It creates a range of dates from 2011-01-01 to 2011-12-31 : select date_format( adddate('2011-1-1', @num:=@num+1), '%Y-%m-%d' ) date from any_table, (select @num:=-1) num limit 365 -- use limit 366 for leap years if you're

Calculate working hours between 2 dates in PostgreSQL

99封情书 提交于 2019-11-26 04:25:41
问题 I am developing an algorithm with Postgres (PL/pgSQL) and I need to calculate the number of working hours between 2 timestamps, taking into account that weekends are not working and the rest of the days are counted only from 8am to 15pm. Examples: From Dec 3rd at 14pm to Dec 4th at 9am should count 2 hours: 3rd = 1, 4th = 1 From Dec 3rd at 15pm to Dec 7th at 8am should count 8 hours: 3rd = 0, 4th = 8, 5th = 0, 6th = 0, 7th = 0 It would be great to consider hour fractions as well. 回答1:

generate_series() equivalent in MySQL

早过忘川 提交于 2019-11-26 01:54:06
问题 I need to do a query and join with all days of the year but in my db there isn\'t a calendar table. After google-ing I found generate_series() in PostgreSQL. Does MySQL have anything similar? My actual table has something like: date qty 1-1-11 3 1-1-11 4 4-1-11 2 6-1-11 5 But my query has to return: 1-1-11 7 2-1-11 0 3-1-11 0 4-1-11 2 and so on .. 回答1: This is how I do it. It creates a range of dates from 2011-01-01 to 2011-12-31 : select date_format( adddate('2011-1-1', @num:=@num+1), '%Y-%m

Generating time series between two dates in PostgreSQL

别说谁变了你拦得住时间么 提交于 2019-11-26 00:54:26
问题 I have a query like this that nicely generates a series of dates between 2 given dates: select date \'2004-03-07\' + j - i as AllDate from generate_series(0, extract(doy from date \'2004-03-07\')::int - 1) as i, generate_series(0, extract(doy from date \'2004-08-16\')::int - 1) as j It generates 162 dates between 2004-03-07 and 2004-08-16 and this what I want. The problem with this code is that it wouldn\'t give the right answer when the two dates are from different years, for example when I