Skip to content

Tag: postgresql

Find minimum and show the area/values around it

I would like to find a minimum in a very long time series, but instead of show the whole series with 2000 points, or only 1 line, I want to show the 20 rows with the profit value before and after the minimum, result example : So, per company, find the minimum, then print the “area” around the mini…

Select second most recent date from inner join

I have this query : I get this result : How can I select only the second most recent created_at date from each unique display_name ? Answer You could use row_number to assign a sequence to your dates and apply this before joining, then include as part of your join criteria, such as: You could also apply this …

GROUP BY Subquery returns more than one row

I’m looking for a way to solve the following situation. I have a table that I need to return only one number for each “p.pays”, This query is supposed to list “nom from table Pays” where at least half of the “athlete” have are in the table “Resultat” but m…

How can I join two results

I have 3 tables purchase, air_transfers and sea_transfers. I have made the example code in db fiddle. My problem is on 6th paragraph where I don’t have any sea transfers, I get null as id and thats because I’m using id from sea transfers. What should I do to fix this? purchase table: air transfer …

SQL Query: finding cheapest car by company

Following YouTube tutorial “Learn PostgreSQL Tutorial – Full Course for Beginners”, I replicate teacher’s code but yields different result and cannot figure out why. Table is this simple: and so on, 1000 entries. Querying cheapest model from manufacturer, tutorial says: And it works pr…

Decluttering a SQL query

For a practice project i wrote the following query and i was wondering if there is way to make it more efficient than writing everything 12 times like a for loop for sql. EDIT I was given the 3 tables represented on the following database schema , and im trying to create a table with the total sales of dairy