Skip to content

Tag: postgresql

Are nested SQL queries atomic?

I have some micro service (almost) system where each program has its own port. Moreover, not all ports in the system are available. For simplicity, I decided to keep a list of free ports in a table in PostgreSQL. Accordingly, I have this table: Respectively, if the value in the service column is null, then th…

SQL returning 1 single column value on a multiple row

This is my query so far. But what I need is to return one single value of a column like this. But in my query so far, this is the returning output. This my query. How can I get rid of the double values? Answer You can use analytical function as follows: Let’s say the column you highlighted is sol.actual…

Select Last Rows with Distinct Field

I have a table with the following schema: In this table only the id field is unique. I’m concerned with getting the rows containing the last X distinct itemid, ordered by date. For example, in the sample above, if I’d like to get the last 3 distinct itemid, I’d be getting the first 4 rows, s…

Insert new records with several ids from another table

I have a table, which has 9 records with id = 1 – 9 (for example, there can be more than 20 ids). I have one varchar value = ‘premium’. I need to insert these values to another table, after this action I should have 9 records with id from the first table and ‘premium’ varchar in …

How to re-run Diesel migrations?

I’m using Diesel with PostgreSQL. I added my migrations, and they worked fine, outputting everything in the schema.rs file. Until I noticed that I was missing the created_at fields on some of my migrations. I edited the SQL and ran diesel migration run. Nothing happened, no errors, no success. Is there …

PostgreSQL Transform Geometries

I have a table full of Easting/Northing points that I want to transform into a column in SRID:27700. I’m using Postgres with postgis installed. I’m trying this: Of course this returns the locations in easting/northing, but when I try to first transform the points using ST_Transform, it returns SQL…