generate-series

How to get number of invoices for last 12 weeks in Postgres

一曲冷凌霜 提交于 2020-01-06 01:35:24
问题 Invoice database contains invoice dates: create table dok ( dokumnr serial primary key, invoicedate date not null ); Dashboard requires comma separated list containing number of invoices for last 12 weeks, e.q 4,8,0,6,7,6,0,6,0,4,5,6 List contains always 12 elements. If there are no invoices for some 7 day interval, 0 should appear. Every element should contain number of invoices for 7 days. Query should find maximum date before current date: select max(invoicedate) as last_date from dok; And

PostgreSQL: How to figure out missing numbers in a column using generate_series()?

给你一囗甜甜゛ 提交于 2020-01-03 15:35:07
问题 SELECT commandid FROM results WHERE NOT EXISTS ( SELECT * FROM generate_series(0,119999) WHERE generate_series = results.commandid ); I have a column in results of type int but various tests failed and were not added to the table. I would like to create a query that returns a list of commandid that are not found in results . I thought the above query would do what I wanted. However, it does not even work if I use a range that is outside the expected possible range of commandid (like negative

array_agg group by and null

只谈情不闲聊 提交于 2019-12-31 03:11:31
问题 Given this table: SELECT * FROM CommodityPricing order by dateField "SILVER";60.45;"2002-01-01" "GOLD";130.45;"2002-01-01" "COPPER";96.45;"2002-01-01" "SILVER";70.45;"2003-01-01" "GOLD";140.45;"2003-01-01" "COPPER";99.45;"2003-01-01" "GOLD";150.45;"2004-01-01" "MERCURY";60;"2004-01-01" "SILVER";80.45;"2004-01-01" As of 2004, COPPER was dropped and mercury introduced. How can I get the value of (array_agg(value order by date desc) ) [1] as NULL for COPPER ? select commodity,(array_agg(value

PostgreSQL generate_series() with SQL function as arguments

老子叫甜甜 提交于 2019-12-30 11:39:10
问题 I have a SQL function called get_forecast_history(integer,integer) that takes two arguments, a month and a year. The function returns a CUSTOM TYPE created with: CREATE TYPE fcholder AS (y integer, m integer, product varchar, actual real); The first line of the function definition is: CREATE OR REPLACE FUNCTION get_forecast_history(integer, integer) RETURNS SETOF fcholder AS $$ Calling: SELECT * FROM get_forecast_history(10, 2011); For example produces the following table (the result type of

Why does PostgreSQL combine series in a wrong way?

别等时光非礼了梦想. 提交于 2019-12-24 15:33:25
问题 I got some strange behavior of combined generate_series. In 2 different polygons that I tried to fill with a grid, one grid was much rarer: The query was like this: SELECT osm_id , generate_series(floor(st_xmin(way))::int, ceiling(st_xmax(way))::int, 150) x, generate_series(floor(st_ymin(way))::int, ceiling(st_ymax(way))::int, 150) y from osm_polygon order by osm_id, x, y; I tried tracing the problem, and just entered min/max coordinates. Generate series from min/max values create correct

updating existing records with a unique integer

核能气质少年 提交于 2019-12-24 01:47:47
问题 I have an existing table with records in it and I've just added a new column ver which I would like to be unique. create table foo ( bar text, ver integer ); select * from foo; bar ver --- --- one null two null three null I'm struggling with how to do this for some reason. I want to do something like: update foo set ver = ( select generate_series(1, 1000) ); or maybe update foo set ver = v from (select generate_series(1, 1000) as v ); ...but of course neither of those work. Can anyone point

How to get average values for time intervals in Postgres

若如初见. 提交于 2019-12-23 21:45:56
问题 I'm using PostgreSQL 9.6. I have a table like this: mac sn loc time date vin1 vin2 vin3 1a34 4as11111111 aaaa 7:06:18 1/1/2018 447.42 472.32 682.59 1a34 4as11111111 aaaa 7:06:43 1/1/2018 455.97 476.25 682.59 1a34 4as11111111 aaaa 7:07:35 1/1/2018 470.88 484.2 682.5 I need to calculate the average of the vin1 , vin2 , vin3 within time intervals of 300 sec (5 min). For example, starting from the first time (7:06:18 - 7:11:18), for the dates in range. I can select the data I need with this query

PostgreSQL: Frequency Table Expansion

喜夏-厌秋 提交于 2019-12-22 12:27:13
问题 Does anyone know how to expand a frequency table in PostgreSQL? For example, transform table x: data | frequency -------+----------- string | 4 into data | index -------+------- string | 1 string | 2 string | 3 string | 4 Set up code: CREATE TABLE x ( data TEXT, frequency INTEGER ); INSERT INTO x VALUES ('string',4); 回答1: This is amazingly simple with generate_series(): SELECT data, generate_series(1, frequency) AS index FROM x; 来源: https://stackoverflow.com/questions/12327046/postgresql

Postgres generate_series

时光总嘲笑我的痴心妄想 提交于 2019-12-21 05:40:50
问题 What I want to is to make statistics on a table and for this I'm using generate_series(); Here is what I'm doing: SELECT x.month, amount FROM (SELECT generate_series( min(date_trunc('month', date)), max(date_trunc('month', date)), '1 month' ) AS month FROM table WHERE user_id = 55 AND ... ) x LEFT JOIN ( SELECT SUM(amount) AS amount, date_trunc('month', date) AS month FROM table WHERE user_id = 55 AND ... GROUP BY month ) q ON q.month = x.month ORDER BY month This works well but when I want

Generate_series in Postgres from start and end date in a table

一笑奈何 提交于 2019-12-18 06:02:06
问题 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