generate-series

How to cast entity to set in PostgreSQL

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 08:07:13
问题 Using Postgres 9.3, I found out that I can perform something like this: SELECT generate_series(1,10); But I can't do this: SELECT (SELECT generate_series(1,10)); Can I somehow cast SELECT result to setof int to use it same as result from generate_series() ? What exactly is happening there why I can use result from function but not from SELECT ? 回答1: Your first form is a non-standard feature of Postgres. It allows SRF (Set Returning Functions) in the SELECT list, which are expanded to multiple

Retrieving row count and returning 0 when no rows

こ雲淡風輕ζ 提交于 2019-12-11 07:56:19
问题 I've written a query to retrieve how many website signups I've had every day: SELECT created, COUNT(id) FROM signups GROUP BY created ORDER BY created desc However, this only retrieves rows for days where people have actually signed up. If nobody has signed up in a day, I would like to return 0 for that day. Is there a way to do this using SQL or will I have to parse through the results using PHP? 回答1: Assuming created to be of type date for lack of information. Postgres provides the

Selecting sum and running balance for last 18 months with generate_series

会有一股神秘感。 提交于 2019-12-11 00:59:59
问题 I have this working query, but I need to add all months to my result, no matter if the items sold during that month: select * from ( select to_char(max(change_date), 'YYYY-MON')::varchar(8) as yyyymmm, max(change_date) as yearmonth, sum(vic.sold_qty / item_size.qty)::numeric(18,2) as sold_qty, -- sold monthly sum(sum(on_hand)) OVER (PARTITION BY vic.item_id order by year,month) as on_hand --running balance from (((view_item_change vic left join item on vic.item_id = item.item_id) left join

PostgreSQL join to denormalize a table with generate_series

只愿长相守 提交于 2019-12-10 17:53:50
问题 I've this table: CREATE TABLE "mytable" ( name text, count integer ); INSERT INTO mytable VALUES ('john', 4),('mark',2),('albert',3); and I would like "denormlize" the rows in this way: SELECT name FROM mytable JOIN generate_series(1,4) tmp(a) ON (a<=count) so I've a number of rows for each name equals to the count column: I've 4 rows with john, 2 with mark and 3 with albert. But i can't use the generate_series() function if I don't know the highest count (in this case 4). There is a way to

REDSHIFT: How can I generate a series of numbers without creating a table called “numbers” in redshift (Postgres 8.0.2)?

流过昼夜 提交于 2019-12-08 00:33:39
问题 I need to create an empty time table series for a report so I can left join activity from several tables to it. Every hour of the day does not necessarily have data, but I want it to show null or zero for inactivity instead of omitting that hour of the day. In later versions of Postgres (post 8.0.2), this is easy in several ways: SELECT unnest(array[0,1,2,3,4...]) as numbers OR CROSS JOIN (select generate_series as hours from generate_series(now()::timestamp, now()::timestamp + interval '1

Cumulative sum of values by month, filling in for missing months

泪湿孤枕 提交于 2019-12-07 05:34:29
问题 I have this data table and I'm wondering if is possible create a query that get a cumulative sum by month considering all months until the current month . date_added | qty ------------------------------------ 2015-08-04 22:28:24.633784-03 | 1 2015-05-20 20:22:29.458541-03 | 1 2015-04-08 14:16:09.844229-03 | 1 2015-04-07 23:10:42.325081-03 | 1 2015-07-06 18:50:30.164932-03 | 1 2015-08-22 15:01:54.03697-03 | 1 2015-08-06 18:25:07.57763-03 | 1 2015-04-07 23:12:20.850783-03 | 1 2015-07-23 17:45

PostgreSQL incremental dates?

人走茶凉 提交于 2019-12-06 14:48:36
问题 I have a database table that contains the following data: ID | Date | Bla 1 | 2013-05-01 | 1 2 | 2013-05-02 | 2 3 | 2013-05-03 | 3 4 | 2013-05-05 | 4 Note that there is a date missing: 2014-05-04 . How should I alter the following query: SELECT * FROM table where DATE >= '2013-05-01' AND DATE <= '2013-05-05' So that I would end up with the following output: ID | Date | Bla 1 | 2013-05-01 | 1 2 | 2013-05-02 | 2 3 | 2013-05-03 | 3 null | 2013-05-04 | null 4 | 2013-05-05 | 4 Is this possible?

REDSHIFT: How can I generate a series of numbers without creating a table called “numbers” in redshift (Postgres 8.0.2)?

試著忘記壹切 提交于 2019-12-06 11:48:41
I need to create an empty time table series for a report so I can left join activity from several tables to it. Every hour of the day does not necessarily have data, but I want it to show null or zero for inactivity instead of omitting that hour of the day. In later versions of Postgres (post 8.0.2), this is easy in several ways: SELECT unnest(array[0,1,2,3,4...]) as numbers OR CROSS JOIN (select generate_series as hours from generate_series(now()::timestamp, now()::timestamp + interval '1 day', '1 hour'::interval )) date_series Redshift can run some of these commands, but throws an error when

Cumulative sum of values by month, filling in for missing months

僤鯓⒐⒋嵵緔 提交于 2019-12-05 09:51:51
I have this data table and I'm wondering if is possible create a query that get a cumulative sum by month considering all months until the current month . date_added | qty ------------------------------------ 2015-08-04 22:28:24.633784-03 | 1 2015-05-20 20:22:29.458541-03 | 1 2015-04-08 14:16:09.844229-03 | 1 2015-04-07 23:10:42.325081-03 | 1 2015-07-06 18:50:30.164932-03 | 1 2015-08-22 15:01:54.03697-03 | 1 2015-08-06 18:25:07.57763-03 | 1 2015-04-07 23:12:20.850783-03 | 1 2015-07-23 17:45:29.456034-03 | 1 2015-04-28 20:12:48.110922-03 | 1 2015-04-28 13:26:04.770365-03 | 1 2015-05-19 13:30:08

PostgreSQL incremental dates?

别等时光非礼了梦想. 提交于 2019-12-04 19:13:32
I have a database table that contains the following data: ID | Date | Bla 1 | 2013-05-01 | 1 2 | 2013-05-02 | 2 3 | 2013-05-03 | 3 4 | 2013-05-05 | 4 Note that there is a date missing: 2014-05-04 . How should I alter the following query: SELECT * FROM table where DATE >= '2013-05-01' AND DATE <= '2013-05-05' So that I would end up with the following output: ID | Date | Bla 1 | 2013-05-01 | 1 2 | 2013-05-02 | 2 3 | 2013-05-03 | 3 null | 2013-05-04 | null 4 | 2013-05-05 | 4 Is this possible? Wolph You can join with a generate_series output: select '2013-05-01'::date + g.o AS "date with offset"