generate-series

Generate series of week intervals for given month

南笙酒味 提交于 2019-11-29 08:38:18
问题 In a Postgres 9.1 database, I am trying to generate a series of weeks for a given month but with some constraints. I need all weeks to start on Monday and get cut when they start or end in another month. Example: For February, 2013 I want to generate a series like this: start ------------------------ 2013-02-01 00:00:00+00 2013-02-04 00:00:00+00 2013-02-11 00:00:00+00 2013-02-18 00:00:00+00 2013-02-25 00:00:00+00 The query that I have now looks like this: SELECT GREATEST(date_trunc('week',

generate_series function in Amazon Redshift

风格不统一 提交于 2019-11-29 00:07:18
I tried the below: SELECT * FROM generate_series(2,4); generate_series ----------------- 2 3 4 (3 rows) SELECT * FROM generate_series(5,1,-2); generate_series ----------------- 5 3 1 (3 rows) But when I try, select * from generate_series('2011-12-31'::timestamp, '2012-12-31'::timestamp, '1 day'); It generated error. ERROR: function generate_series(timestamp without time zone, timestamp without time zone, "unknown") does not exist HINT: No function matches the given name and argument types. You may need to add explicit type casts. I use PostgreSQL 8.0.2 on Redshift 1.0.757. Any idea why it

Postgresql generate_series of months

只谈情不闲聊 提交于 2019-11-28 20:41:19
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. select DATE '2008-01-01' + (interval '1' month * generate_series(0,11)) Edit If you need to calculate the number dynamically, the following could help: select DATE '2008-01-01' + (interval '1' month *

Insert multiple rows in one table based on number in another table

青春壹個敷衍的年華 提交于 2019-11-28 12:24:19
I am creating a database for the first time using Postgres 9.3 on MacOSX. Let's say I have table A and B . Table A starts off as empty and Table B as filled. I would like the number of entries in column all_names in table B to equal the number for each names in table A like table B below. Thus names should contain each unique entry from all_names and number its count. I am not used to the syntax yet so I do not really know how to go about it. The birthday column is redundant. A names | number -------+------------ Carl | 3 Bill | 4 Jen | 2 B all_names | birthday -------+------------ Carl | 17

SQLite generate_series missing

这一生的挚爱 提交于 2019-11-28 05:58:08
问题 I'm trying to work with the SQLite CLI, and I can't get the generate_series function to work. I can simulate it with the recursive CTE, as suggested in the documentation, but I can't seem to get any of the examples in that link to work. Here's some output from my session: sqlite> with recursive generate_series(value) as ( select 1 union all select value+1 from generate_series where value+1<=3) select value from generate_series; 1 2 3 sqlite> select value from generate_series; Error: no such

Remove blank-padding from to_char() output

僤鯓⒐⒋嵵緔 提交于 2019-11-28 02:02:22
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' to change it from text to character varying . It seems that the length is fixed is not according to

How to perform a select query in a DO block?

坚强是说给别人听的谎言 提交于 2019-11-27 23:33:47
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 + 1 AS TheRunningMonth --,CTE.TheStartYear + (CTE.TheRunningMonth / 12) AS TheYear ,@iStartYear + (CTE

Group by data intervals

折月煮酒 提交于 2019-11-27 22:34:32
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 procedure/cursor to do this? Users may then request 5 minute intervals data etc. I will most likely be using

Postgres - how to return rows with 0 count for missing data?

随声附和 提交于 2019-11-27 19:30:44
I have unevenly distributed data(wrt date) for a few years (2003-2008). I want to query data for a given set of start and end date, grouping the data by any of the supported intervals (day, week, month, quarter, year) in PostgreSQL 8.3 ( http://www.postgresql.org/docs/8.3/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC ). The problem is that some of the queries give results continuous over the required period, as this one: select to_char(date_trunc('month',date), 'YYYY-MM-DD'),count(distinct post_id) from some_table where category_id=1 and entity_id = 77 and entity2_id = 115 and date <

generate_series function in Amazon Redshift

别来无恙 提交于 2019-11-27 15:13:13
问题 I tried the below: SELECT * FROM generate_series(2,4); generate_series ----------------- 2 3 4 (3 rows) SELECT * FROM generate_series(5,1,-2); generate_series ----------------- 5 3 1 (3 rows) But when I try, select * from generate_series('2011-12-31'::timestamp, '2012-12-31'::timestamp, '1 day'); It generated error. ERROR: function generate_series(timestamp without time zone, timestamp without time zone, "unknown") does not exist HINT: No function matches the given name and argument types.