I inherited a PostgreSQL database in production with one table that is around 250 GB in size. It only has around ten thousand live rows which I estimate to be not more than 20 MB. The table grew to …
Tag: postgresql
Postgres Get column name and value as rows for given ID
I have a table like this I need to get values for a given ID like this is There any simple way to achieve this. Answer You could convert the row to a json & then use json_each to transpose the data as you desire:
In SQL, how do I prevent array elements from being null?
I’m designing a SQL database and I’d like to make sure my schema is as strict as possible. Is there a way to require that an array column doesn’t contain null elements? As I understand it, I can make the whole column NOT NULL, but that’s not the same thing since an array of null elemen…
How to query Json array in jsonb in Postgresql
I have a jsonb column which have following rows ROW1: [ { “cpe23Uri”: “cpe:2.3:a:sgi:irix:3.55:*:*:*:*:*:*:*”, “active”: true }, { “…
Calculate interval between boolean column change
I have a table with measurements of weather here is a simplified version of it: and I need to calculate how much time is been sunny. I know I need to use window functions but I’m stuck. Because I need this interval to be calculated in some range for example for last day. I have been able to make a
Default values for each key in a where-clause
Given a table: I need to set 0 for every ID in where-clause along with IDs: Result What query can I use here? Upd1: let it be Postgres. Upd2: can I do it without joins? Answer You didn’t mention your DBMS, but the following is standard ANSI SQL: It’s possible without a join, but that doesn’t…
Calculating values from two tables where one has key in header and one has it in column values
I have a simple problem that I dont know how to solve in sql. I have two tables, cost : second table: income Now i want to get a result that substract for each party A, B, C the income-cost and finds the net value. I cannot compare the column header to column value. I am quite new to this,
Appending Name based on Min and Max datetime
I have Event_No, Events, Date Range in my table like below. I need to show Min datetime respective Event name with ‘IN'(a concatenation of (Event-‘IN’))and Max datetime respective Event with Out(a concatenation of (Event-‘Out’)). I need My Final Output like below Thanks Answer Th…
PostgreSQL: Inserting tuples in multiple tables using a view and a trigger
I am trying to build an order system that is able to insert a compound order that consists of multiple items and amounts. My database layout is as follows: I have an order table, containing an autoincrement id, item_id, amount and order_group_id columns. I also have an order_group table containing an autoincr…
Indexing for dense_rank query in postgresql
So been trying to run a query on a big table but it’s taking more than a day to finish, when I put explain at the beginning and went through the result, it did not appear as using an index The query …