postgresql-8.2

Optimising Select number of rows in PostgreSql for multiple group by fields

懵懂的女人 提交于 2020-02-05 03:47:32
问题 I have this query to get number of rows for the given filters, it takes too long time to get the result since db is too big, is there any way to optimise it i'm using postgresql 8.2 SELECT COUNT(1) as numrows from ( select lower(column1) as column1, column2, column3, column4, sum(column5) as column5, sum(column6) as column6 from table_name tablename where column_date >= 'SOME DATE' and column_date < 'SOME DATE' group by lower(column1) as column1, column2, column3, column4 ORDER BY column5

Dynamic table names by date

南楼画角 提交于 2019-12-24 09:28:50
问题 So I have tables that I need to generate nightly. As an example I have tables such as foo_01jan16, foo_02jan2016, foo_03jan2016, etc. Additionally I reference these table(s) in other queries that I run daily. However, find and replace seems inefficient. What I want to do is automate this process. I want to do something like: CREATE OR REPLACE FUNCTION table_date() RETURNS text AS $$ SELECT 'foo_'||to_char(current_timestamp, 'DDMONYY') AS result $ LANGUAGE SQL; Then in query I can reference

Group_concat equivalent in postgresql 8.2.11

北慕城南 提交于 2019-12-13 05:42:17
问题 I am using a older version of Postgres 8.2.11. Can anyone tell me the equivalent of MySql's group_concat for this Postgres 8.2.11. I have tried array_accum , array_to_string , string_agg but it doesn't work in this version 回答1: The "not quite duplicate" in the comments should point you in the right direction: create your own aggregate function. First you'll need a non-aggregate string concatenation function, something like this: create function concat(t1 text, t2 text) returns text as $$

Sort timestamps (including future) by absolute distance from “now”

放肆的年华 提交于 2019-12-12 10:48:59
问题 With a date field I can do this: ORDER BY ABS(expiry - CURRENT_DATE) With a timestamp field I get the following error: function abs(interval) does not exist 回答1: Use now() or CURRENT_TIMESTAMP for the purpose. The reason for the different outcome of your queries is this: When you subtract two values of type date , the result is an integer and abs() is applicable. When you subtract two values of type timestamp (or just one is a timestamp ), the result is an interval , and abs() is not