I’m currently trying to find the percentage of certain amount of preregistered users in my postgres db, the operation would be (1185 * 100) / 3104 = 38.17. To do that I’m using two select statements to retrieve each count, but I’ve been unable to operate between them: This is what I have: Thanks in advance for any hint or
Tag: postgresql
ppgsql declaring variable from parameter
I am running a sql query from the command line where I pass in value called tablename: I then set the variable like this: And then I have a function where I want to use the variable tablename like this: I have also tried declaring the variable with a few variations of this: Please let me know if you have
Postgresql unique constraint for colums of multiple related tables
I have the following tables: server (server_id as PK) server_groups Each server can have multiple server groups, every group name within a server must be unique. group_tags Each group can have multiple tags, a tag can be connected to multiple groups. Each tag name must be unique. group_tag_relations Problem: I need group tag names and server group names to be
List values with MaxDate
Im trying to create ie query to show itens with MAX DATE, but I don´t know how ! Follow the script and result: I need to show only tasks with the last date of each one. Can you help me ? Answer You seem to be using Postgres (as suggested by the use of casting operator ::). If so –
How to deal with Case Sensitivity in PostgreSQL
I have a master table for color codes which stores various unique color values. It has a column Color_value on which UNIQUE constraint has been applied. However it is accepting both ‘Black’ and ‘black’ values. How can such situations be handled? Answer You can create a unique index like this: That said, it would be much simpler to make your
How do I use one SQL statement to invalidate groups of records?
I’m working on an assignment for class. I need to write an SQL statement that returns every single country in the database where no single ethnic group makes up more than 25% of the population. This is the closest I can get: But, that only returns records where ethnic groups are over 25% population. It doesn’t remove other rows in
Minimum of Days Difference between Dates and Update PostgreSQL
I have a situation like, I need to find out which record is having minimum difference of days between atleast 2 dates. Like – Table Data is like Input Parameter : [aDate = 12-Nov-2020, Infold = 2] Result should be: Need to find the date difference between the input dates and existing dates, find the record which is having minimum
aggregating Posts with all their Hashtags using Postgres
I have something like Posts in FB/instagram. When a user searches for something, the input query is used to either find Posts that either have this query in their content, OR – one or more of post’s hashtags match this query. The problem : I can’t figure out how to have Posts returned with a nested array of all its
Rewrite SQL query to remove duplicate SELECTs in [WHERE xxx IN] condition
I need to execute following query: What is bothering me – is that it has to select same values 3 times. I am sure that there is a better, proper way of doing it. Could you please suggest how this query might be rewritten? Answer You can use an EXISTS condition with an IN: Which is more or less the
Postgresql replace comma from right in position 3 and replace dot and comma left to that position
I am having a currency value with comma and dot. I need to change some European style currency to UK style value. I have tried using right() and replace functions but no luck. Could anyone please help me in writing replace function to handle this. Answer This is an issue in European countries, especially when new and legacy software have