generate-series

Postgresql generate_series of months

你。 提交于 2019-11-27 13:04:41
问题 I'm trying to generate a series in PostgreSQL with the generate_series function. I need a series of months starting from Jan 2008 until current month + 12 (a year out). I'm using and restricted to PostgreSQL 8.3.14 (so I don't have the timestamp series options in 8.4). I know how to get a series of days like: select generate_series(0,365) + date '2008-01-01' But I am not sure how to do months. 回答1: select DATE '2008-01-01' + (interval '1' month * generate_series(0,11)) Edit If you need to

Group by data intervals

让人想犯罪 __ 提交于 2019-11-27 04:35:10
问题 I have a single table which stores bandwidth usage on the network over a period of time. One column will contain the date time (primary key) and another column will record the bandwidth. Data is recorded every minute. We will have other columns recording other data at that moment in time. If the user requests the data on 15 minute intervals (within a 24 hour period given start and end date), is it possible with a single query to get the data I require or would I have to write a stored

generate_series() method fails in Redshift

笑着哭i 提交于 2019-11-27 02:06:40
When I run the SQL Query: select generate_series(0,g) from ( select date(date1) - date(date2) as g from mytable ; It returns an error: INFO: Function "generate_series(integer,integer)" not supported. ERROR: Specified types or functions (one per INFO message) not supported on Redshift tables. But when I run this query: select generate_series(0, g) from (select 5 as g) It returns the below response: generate_series ----------------- 0 1 2 3 4 5 (6 rows) Why does the second query work, while the first fails? The generate_series() function is not fully supported by Redshift. See the Unsupported

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

守給你的承諾、 提交于 2019-11-27 00:32:06
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 a way to do that? 2) An easy but ugly brute force way is to do a single query for all records within the start

Remove blank-padding from to_char() output

时间秒杀一切 提交于 2019-11-26 23:37:09
问题 I generate a view from this: create or replace view datetoday as select to_char(dt, 'yyyy-mm-dd') as date, to_char(dt, 'Day') as weekday from (select ('2013-03-01'::date + i) dt from generate_series(0,'2013-03-03'::date - 2013-03-01'::date) as t(i)) as t; It gives me the weekday info as text type. Then I use: select date::date, weekday::varchar from datetoday; Now the table is like 2013-3-1 Friday 2013-3-2 Saturday If I want to choose the entry: select * from datetoday where weekday='Friday'

How to perform a select query in a DO block?

冷暖自知 提交于 2019-11-26 21:13:21
问题 I want to port the below SQL code from MS SQL-Server to PostgreSQL. DECLARE @iStartYear integer DECLARE @iStartMonth integer DECLARE @iEndYear integer DECLARE @iEndMonth integer SET @iStartYear = 2012 SET @iStartMonth = 4 SET @iEndYear = 2016 SET @iEndMonth = 1 ;WITH CTE AS ( SELECT --@iStartYear AS TheStartYear @iStartMonth AS TheRunningMonth ,@iStartYear AS TheYear ,@iStartMonth AS TheMonth UNION ALL SELECT --CTE.TheStartYear AS TheStartYear --@iStartYear AS TheStartYear CTE.TheRunningMonth

Using sql function generate_series() in redshift

爷,独闯天下 提交于 2019-11-26 20:55:33
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 matches the given name and argument types. You may need to add explicit type casts. [SQL State=42883] Also

Generate series of dates - using date type as input

蓝咒 提交于 2019-11-26 19:12:23
Documentation for generate_series says that argument can be int or bigint for generate_series(start, stop) and generate_series(start, stop, step) cases and timestamp or timestamp with time zone for generate_series(start, stop, step interval) . What is the reason that generate_series works also with date type as input and returns timestamp with timezone ? pg=# select generate_series('2014-01-01'::date,'2014-01-02'::date,'1 day'); generate_series ------------------------ 2014-01-01 00:00:00+01 2014-01-02 00:00:00+01 (2 rows) Thanks to function type resolution we can also pass date values to

Calculate working hours between 2 dates in PostgreSQL

瘦欲@ 提交于 2019-11-26 16:33:21
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. Erwin Brandstetter According to your question working hours are: Mo–Fr, 08:00–15:00 . Rounded results For just two

Generate series of dates - using date type as input

断了今生、忘了曾经 提交于 2019-11-26 12:16:33
问题 Documentation for generate_series says that argument can be int or bigint for generate_series(start, stop) and generate_series(start, stop, step) cases and timestamp or timestamp with time zone for generate_series(start, stop, step interval) . What is the reason that generate_series works also with date type as input and returns timestamp with timezone ? pg=# select generate_series(\'2014-01-01\'::date,\'2014-01-02\'::date,\'1 day\'); generate_series ------------------------ 2014-01-01 00:00