generate-series

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

一世执手 提交于 2019-12-17 15:43:17
问题 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

generate_series() method fails in Redshift

久未见 提交于 2019-12-17 05:11:36
问题 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

Get count of created entries for each day

杀马特。学长 韩版系。学妹 提交于 2019-12-13 14:48:21
问题 Let's say I have a this search query like this: SELECT COUNT(id), date(created_at) FROM entries WHERE date(created_at) >= date(current_date - interval '1 week') GROUP BY date(created_at) As you know then for example I get a result back like this: count | date 2 | 15.01.2014 1 | 13.01.2014 9 | 09.01.2014 But I do not get the days of the week where no entries where created. How can I get a search result that looks like this, including the days where no entries where created? count | date 2 | 15

sum and generate series does't work in postgresql

亡梦爱人 提交于 2019-12-13 06:19:53
问题 In classification_indicator_id column I have some digits. I would like to sum this digits in 1 day series. I wrote below query select a.data_start::date, a.segment1::integer as "Segment1" from ( select data as data_start, (select sum(classification_indicator_id) from classifications where classification_indicator_id = 3)::integer as segment1 from generate_series('2013-03-25'::timestamp without time zone, '2013-04-01'::timestamp without time zone, '1 day'::interval) data ) a group by a.data

Time series querying in Postgres

爱⌒轻易说出口 提交于 2019-12-12 14:15:47
问题 This is a follow on question from @Erwin's answer to Efficient time series querying in Postgres. In order to keep things simple I'll use the same table structure as that question id | widget_id | for_date | score | The original question was to get score for each of the widgets for every date in a range. If there was no entry for a widget on a date then show the score from the previous entry for that widget. The solution using a cross join and a window function worked well if all the data was

How can I generate a series of repeating numbers in PostgreSQL?

假如想象 提交于 2019-12-12 10:28:58
问题 In PostgreSQL, is it possible to generate a series of repeating numbers? For example, I want to generate the numbers 1 to 10, with each number repeated 3 times: 1 1 1 2 2 2 3 3 3 .. and so on. 回答1: You could cross join it to a series of 3: SELECT a.n from generate_series(1, 100) as a(n), generate_series(1, 3) 回答2: You could try integer division like this: SELECT generate_series(3, 100) / 3 回答3: For such small numbers CROSS JOIN two VALUES expressions: SELECT n FROM (VALUES (1),(2),(3)) x(r) -

Return list with dates within a specified range from a single query

有些话、适合烂在心里 提交于 2019-12-11 21:25:06
问题 I'm trying to obtain a list of dates within range, similar to the command NOW() in PostgreSQL, with the only difference that now() , returns only the current date. If I execute it as follows I obtain: select now(); 2013-09-27 15:27:50.303-05 Or by example, I could do this: select now() - interval '1' day; and the result is yesterday 2013-09-27 15:27:50.303-05 What I need is a query that could return a list with every date within a given range, so if I provide 2013-09-20 and 2013-09-27 (I'm

Powershell How to generate multiple combinations

纵饮孤独 提交于 2019-12-11 14:53:43
问题 I'm trying to create a function that compares if the beginning of an input string matches one of the possibilities generated from the combinations in multiple System.Arrays . The result of this function will be a returned object with the input GroupName we received and if it's a valid name or not $true/$false . Array1: Country ------- BEL NLD Array2: Color ---- Green Red Array3: Type Object ---- ------ Fruit Banana Veggetables Aubergine Fruit Appel Veggetables Carrot Fruit Peer Generated list

Select all integers that are not already in table in postgres

↘锁芯ラ 提交于 2019-12-11 09:33:32
问题 I have some ids in a table, but there are gaps in between. I want to select these gaps. For example, the integer numbers in my table are: 1 2 5 9 15 And I want to select: 3 4 6 7 8 10 11 12 13 14 My version of PostgreSQL is 9.1.1, so I cannot use int4range. 回答1: Use generate_series() and LEFT JOIN to the table: SELECT g.nr FROM generate_series(1,15) g(nr) LEFT JOIN tbl USING (nr) WHERE tbl.nr IS NULL; Replace all occurrences of nr with your actual column name. Or use one of the other basic

How to aggregate data from multiple years on MM-DD, ignoring year

别来无恙 提交于 2019-12-11 08:26:49
问题 Postgres version 9.4.18, PostGIS Version 2.2. Here are the tables I'm working with (and can unlikely make significant changes to the table structure): Table ltg_data (spans 1988 to 2018): Column | Type | Modifiers ----------+--------------------------+----------- intensity | integer | not null time | timestamp with time zone | not null lon | numeric(9,6) | not null lat | numeric(8,6) | not null ltg_geom | geometry(Point,4269) | Indexes: "ltg_data2_ltg_geom_idx" gist (ltg_geom) "ltg_data2_time