generate-series

Fill in missing rows when aggregating over multiple fields in Postgres

戏子无情 提交于 2019-12-04 03:57:04
问题 I am aggregating sales for a set of products per day using Postgres and need to know not just when sales do happen, but also when they do not for further processing. SELECT sd.date, COUNT(sd.sale_id) AS sales, sd.product FROM sales_data sd -- sales per product, per day GROUP BY sd.product, sd.date ORDER BY sd.product, sd.date This produces the following: date | sales | product ------------+-------+------------------- 2017-08-17 | 10 | soap 2017-08-19 | 2 | soap 2017-08-20 | 5 | soap 2017-08

Create list with first and last day of month for given period

独自空忆成欢 提交于 2019-12-02 00:37:27
问题 I have to generate a list with two columns of day intervals for every month in a specific period. First column must be the first day of month and the second column the last day of month. Example: Start date: 2014-01-01 End date: 2014-06-30 The result should be in two columns: 1. 2014-01-01 | 2014-01-31 2. 2014-02-01 | 2014-02-28 3. 2014-03-01 | 2014-03-31 4. 2014-04-01 | 2014-04-30 5. 2014-05-01 | 2014-05-31 6. 2014-06-01 | 2014-06-30 I have two functions that get the first and last day from

Join a count query on a generate_series in postgres and also retrieve Null-values as “0”

蹲街弑〆低调 提交于 2019-12-01 15:55:07
What I want to get is a statistic with each month from a generate_series and the sum of the counted id's in every month. This SQL works in PostgreSQL 9.1: SELECT (to_char(serie,'yyyy-mm')) AS year, sum(amount)::int AS eintraege FROM ( SELECT COUNT(mytable.id) as amount, generate_series::date as serie FROM mytable RIGHT JOIN generate_series( (SELECT min(date_from) FROM mytable)::date, (SELECT max(date_from) FROM mytable)::date, interval '1 day') ON generate_series = date(date_from) WHERE version = 1 GROUP BY generate_series ) AS foo GROUP BY Year ORDER BY Year ASC; And this is my output "2006

Join a count query on a generate_series in postgres and also retrieve Null-values as “0”

左心房为你撑大大i 提交于 2019-12-01 14:59:16
问题 What I want to get is a statistic with each month from a generate_series and the sum of the counted id's in every month. This SQL works in PostgreSQL 9.1: SELECT (to_char(serie,'yyyy-mm')) AS year, sum(amount)::int AS eintraege FROM ( SELECT COUNT(mytable.id) as amount, generate_series::date as serie FROM mytable RIGHT JOIN generate_series( (SELECT min(date_from) FROM mytable)::date, (SELECT max(date_from) FROM mytable)::date, interval '1 day') ON generate_series = date(date_from) WHERE

Selecting an average of records grouped by 5 minute periods

故事扮演 提交于 2019-12-01 13:16:39
I'm having a slight issue. I have a PostgreSQL table with such format time (datetime) | players (int) | servers (int) --------------------------------------------------- 2013-12-06 13:40:01 | 80 | 20 2013-12-06 13:41:13 | 78 | 21 etc. I would like to group them by 5 minute periods and get an average of the group as a single value, so there will be 20% of the records, each containing an average of ~5 numbers, with time set to the first time value in the group. I have no idea how to do this in PgSQL. So the result would be: 2013-12-06 13:40:01 | avg of players on :40, :41, :42, :43, :44 | same

Selecting an average of records grouped by 5 minute periods

纵饮孤独 提交于 2019-12-01 11:21:13
问题 I'm having a slight issue. I have a PostgreSQL table with such format time (datetime) | players (int) | servers (int) --------------------------------------------------- 2013-12-06 13:40:01 | 80 | 20 2013-12-06 13:41:13 | 78 | 21 etc. I would like to group them by 5 minute periods and get an average of the group as a single value, so there will be 20% of the records, each containing an average of ~5 numbers, with time set to the first time value in the group. I have no idea how to do this in

Retrieve aggregates for arbitrary time intervals

房东的猫 提交于 2019-11-30 07:37:46
问题 This is the query I have so far, to create daily bars: SELECT DISTINCT date_trunc('hour',t) AS date, min(price) OVER w, max(price) OVER w, first_value(price) OVER w, last_value(price) OVER w FROM ticker WINDOW w AS (PARTITION BY date_trunc('hour',t)); Changing 'hour' to 'min' or 'day' would give me the bars corresponding to these units. However, what if I want 5 min or 15 min bars? date_trunc() doesn't support these and I'm looking for a nice elegant way to do it. 回答1: For 15-minute intervals

Generate series of week intervals for given month

与世无争的帅哥 提交于 2019-11-30 07:03:59
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', dates.d), date_trunc('month',dates.d)) as start FROM generate_series(to_timestamp(1359676800),to

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

本秂侑毒 提交于 2019-11-30 06:01:02
问题 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

Generate_series in Postgres from start and end date in a table

浪子不回头ぞ 提交于 2019-11-29 10:28:04
I have been trying to generate a series of dates (YYYY-MM-DD HH) from the first until the last date in a timestamp field. I've got the generate_series() I need, however running into an issue when trying to grab the start and end dates from a table. I have the following to give a rough idea: with date1 as ( SELECT start_timestamp as first_date FROM header_table ORDER BY start_timestamp DESC LIMIT 1 ), date2 as ( SELECT start_timestamp as first_date FROM header_table ORDER BY start_timestamp ASC LIMIT 1 ) select generate_series(date1.first_date, date2.first_date , '1 hour'::interval)::timestamp