generate-series

generate_series() equivalent in snowflake

◇◆丶佛笑我妖孽 提交于 2021-02-08 15:16:32
问题 I'm trying to find the snowflake equivalent of generate_series() (the PostgreSQL syntax). SELECT generate_series(timestamp '2017-11-01', CURRENT_DATE, '1 day') 回答1: This is how I was able to generate a series of dates in Snowflake. I set row count to 1095 to get 3 years worth of dates, you can of course change that to whatever suits your use case select dateadd(day, '-' || seq4(), current_date()) as dte from table (generator(rowcount => 1095)) Originally found here EDIT: This solution is not

Total Number of Records per Week

萝らか妹 提交于 2021-02-08 08:16:01
问题 I have a Postgres 9.1 database. I am trying to generate the number of records per week (for a given date range) and compare it to the previous year. I have the following code used to generate the series: select generate_series('2013-01-01', '2013-01-31', '7 day'::interval) as series However, I am not sure how to join the counted records to the dates generated. So, using the following records as an example: Pt_ID exam_date ====== ========= 1 2012-01-02 2 2012-01-02 3 2012-01-08 4 2012-01-08 1

How to get arrays from a normalised table that stores array elements by index?

一曲冷凌霜 提交于 2021-01-29 08:26:19
问题 I have a table storing array elements by the array they belong to and their index in the array. It seemed smart because the arrays were expected to be sparse, and have their elements updated individually. Let's say this is the table: CREATE TABLE values ( pk TEXT, i INTEGER, value REAL, PRIMARY KEY (pk, i) ); pk | i | value ----+---+------- A | 0 | 17.5 A | 1 | 32.7 A | 3 | 5.3 B | 1 | 13.5 B | 2 | 4.8 B | 4 | 89.1 Now I would like to get these as real arrays, i.e. {17.5, 32.7, NULL, 53} for

Get dates of a day of week in a date range

我与影子孤独终老i 提交于 2021-01-28 22:05:36
问题 I need a function in PostgreSQL that accepts a date range and returns the dates inside the date range that are Mondays. Anybody have an idea how this could be done? 回答1: The most efficient way should be to find the first Monday and generate a series in steps of 7 days: CREATE OR REPLACE FUNCTION f_mondays(dr daterange) RETURNS TABLE (day date) AS $func$ SELECT generate_series(a + (8 - EXTRACT(ISODOW FROM a)::int) % 7 , z , interval '7 days')::date FROM ( SELECT CASE WHEN lower_inc(dr) THEN

Get dates of a day of week in a date range

百般思念 提交于 2021-01-28 22:02:11
问题 I need a function in PostgreSQL that accepts a date range and returns the dates inside the date range that are Mondays. Anybody have an idea how this could be done? 回答1: The most efficient way should be to find the first Monday and generate a series in steps of 7 days: CREATE OR REPLACE FUNCTION f_mondays(dr daterange) RETURNS TABLE (day date) AS $func$ SELECT generate_series(a + (8 - EXTRACT(ISODOW FROM a)::int) % 7 , z , interval '7 days')::date FROM ( SELECT CASE WHEN lower_inc(dr) THEN

Handling of generate_series() in queries with date or timestamp with / without time zone

喜你入骨 提交于 2021-01-04 03:16:43
问题 I have a query to generate a report based on a date series that is grouped by date and employee_id . The date should be based on a particular time zone, in this case 'Asia/Kuala_Lumpur'. But this can change depending on where the user's time zone is. SELECT d::date AT TIME ZONE 'Asia/Kuala_Lumpur' AS created_date, e.id, e.name, e.division_id, ARRAY_AGG( a.id ) as rows, MIN(a.created_at) FILTER (WHERE a.activity_type = 1) as min_time_in, MAX(a.created_at) FILTER (WHERE a.activity_type = 2) as

Handling of generate_series() in queries with date or timestamp with / without time zone

久未见 提交于 2021-01-04 03:11:42
问题 I have a query to generate a report based on a date series that is grouped by date and employee_id . The date should be based on a particular time zone, in this case 'Asia/Kuala_Lumpur'. But this can change depending on where the user's time zone is. SELECT d::date AT TIME ZONE 'Asia/Kuala_Lumpur' AS created_date, e.id, e.name, e.division_id, ARRAY_AGG( a.id ) as rows, MIN(a.created_at) FILTER (WHERE a.activity_type = 1) as min_time_in, MAX(a.created_at) FILTER (WHERE a.activity_type = 2) as

Handling of generate_series() in queries with date or timestamp with / without time zone

放肆的年华 提交于 2021-01-04 03:11:34
问题 I have a query to generate a report based on a date series that is grouped by date and employee_id . The date should be based on a particular time zone, in this case 'Asia/Kuala_Lumpur'. But this can change depending on where the user's time zone is. SELECT d::date AT TIME ZONE 'Asia/Kuala_Lumpur' AS created_date, e.id, e.name, e.division_id, ARRAY_AGG( a.id ) as rows, MIN(a.created_at) FILTER (WHERE a.activity_type = 1) as min_time_in, MAX(a.created_at) FILTER (WHERE a.activity_type = 2) as

Handling of generate_series() in queries with date or timestamp with / without time zone

南楼画角 提交于 2021-01-04 03:11:18
问题 I have a query to generate a report based on a date series that is grouped by date and employee_id . The date should be based on a particular time zone, in this case 'Asia/Kuala_Lumpur'. But this can change depending on where the user's time zone is. SELECT d::date AT TIME ZONE 'Asia/Kuala_Lumpur' AS created_date, e.id, e.name, e.division_id, ARRAY_AGG( a.id ) as rows, MIN(a.created_at) FILTER (WHERE a.activity_type = 1) as min_time_in, MAX(a.created_at) FILTER (WHERE a.activity_type = 2) as

Looping inside a Postgres UPDATE query

匆匆过客 提交于 2020-02-05 05:15:29
问题 (Postgres 10.10) I have the following fields in my_table : loval INTEGER hival INTEGER valcount INTEGER values INTEGER[] I need to set values to an array containing valcount random integers each between loval and hival inclusive. So for: loval: 3 hival: 22 valcount: 6 I'm looking to set values to something like: {3, 6, 6, 13, 17, 22} I know how to do this with an inefficient "loop through the cursor" solution, but I'm wondering if Postgres has a way to do a looping computation inline. Note: I