PostgreSQL order by TSRange - postgresql

I think the question is pretty self explanatory, I've been learning about Postgres TSRange column type, but I've not been able to find an example of how you can order by it... How do you order by a TSRange, or specifically the lower bound?

There are range functions described in documentation.
SELECT * FROM my_table ORDER BY lower(range_column);

Related

SQL query to calculate first and second order

I have a table with order_id, Customer_Id and Order_date. I can calculate first time order, as it is an easy 'min' on the date, but for some reason I can't find a way to get the second order date per customer in SQL.
Order_id|Customer_Id|Order_date
Ideal result:
Customer_Id|First_order_date|Second_order_date
I am using Postgres and have beginners level of SQL.
Happy to get any suggestions.
Many thanks for your help,
Misael
What I have tried
Tried using 'row_number() over(partition by customer_id', but I can't seem to make it work. I believe I need to declare what a second purchase means and use a common table expression.

PostgreSQL query I don't understand

I'm sorry for my title, but it's totally right. I don't know PostgreSQL well, but I have to take over other person's application. I know SQL, so it's usually no problem to take over any application based on MSSQL, Oracle, MySQL, PostgreSQL, ... but here is some kind of PostgreSQL facet. Could anyone, please, explain this query to me?
select company_generate_course_template_fc
((select company_id from company order by 1 desc limit 1)::int)
The query calls the function company_generate_course_template_fc() passing the result of the query: select company_id from company order by 1 desc limit 1 as an argument. The result is cast to an integer using ::int (see the manual for details)
Apart from the ::int part (and the different ways of limiting the result to a single row), this wouldn't be much different in other databases
The ANSI SQL equivalent of ::int would be cast(... as integer)
If you are talking about ::int it's type casting from string (company_id) to integer value

Can PostgreSQL recommend optimal field types?

MySQL offers PROCEDURE ANALYSE. Given a query like
SELECT `field` FROM `table` PROCEDURE ANALYSE();
The result offers a suggested field type
Optimal_fieldtype: TINYINT(2) UNSIGNED NOT NULL
Does PostgreSQL offer a similar functionality? I've looked at pg_statistic/pg_stats. Seems like I can use this information to infer what data type might be appropriate, but it would be handy if Postgres could recommend an actual data type itself.
There is no equivalent in Postgres for MySQL's PROCEDURE ANALYSE that I know of. (And I think I would know.)
For the given example, obviously about an integer-type column, I would:
SELECT min(field), max(field) FROM tbl;
And check against the range of possible numeric data types in the manual.
Or for the distribution of distinct values:
SELECT field, count(*) AS ct FROM tbl GROUP BY 1 ORDER BY 2 DESC;
Actually, any DBA with a minimum of experience should know the essential characteristics of basic data types. The best type for each use case depends on a lot more than the current range and distribution of values. MySQL's PROCEDURE ANALYSE() would basically be assistance to newcomers - who would easily misinterpret results.

PosgtreSQL Optimize Query with st_transform, st_makepoint, and st_contains

I have the following query:
UPDATE DestinTable
SET destin = geomVal
FROM GeomTable
WHERE st_contains(st_transform(geom, 4326), st_setsrid(
st_makepoint(d_lon::double precision, d_lat::double precision), 4326));
This query works, but it is very slow. I have to run an update on a very large table, and it is taking a 8+ hours to complete (I run this on 5 different columns). I wanted to know if there was a way to optimize this query to make it run faster. I am unaware of the behind the scenes work associated with an st_contains() method, so there may be some obvious solutions that I am missing.
The easiest way is to create an index on ST_TRANSFORM
CREATE INDEX idx_geom_4326_geomtable
ON GeomTable
USING gist
(ST_Transform(geom, 26986))
WHERE geom IS NOT NULL;
If you have all the fields in one SRID in the table it will be even easier to create a normal GIST index on that table and transform the point you're supplying to the local SRID

Capture Value In Table Upper Case Or Lower case

I want to
SELECT * FROM table1 WHERE name=petter
Now if there is many type of petter in table like PETTER , Petter And petter.
Want all this three (PETTER, Petter, petter) to be considered which command is for this in cognos report studio?
Or in DB2 without using 'IN' function.
I think you want UPPER (or LOWER, the effect should be the same):
SELECT *
FROM table1
WHERE UPPER(name) = 'PETTER'
Remember, though, if you have an index on name, then this won't be able to use that index. You can create (at least if you're on z/OS) an index with that function. On other platforms, you can create a generated column and create index on that.