Skip to content

Tag: postgresql

Retrieve all records with the 5 most recent distinct dates

I need to retrieve data for a history module with pagination. The requirement is to show the records for the five most recent dates first (regardless of the number of rows) and load the remaining records in lots (next five dates, any number of rows) as requested by the user. I came up with the query below, which is extremely

count the 3 different values ​that a column can take

I’m trying to count the 3 different values ​​that a column in a table can take, in a single SELECT. What am I doing wrong? I am using a postgresql table where the column named “tipoprova” is an integer Answer You are looking for filtered aggregation: Your query is wrong, because you have the wrong syntax for the CASE expression.

How to count number of groups?

I have a table as follow: I want to count the number of distinct g_n. (Here it’s 3.) I have tried with: But it gives me the number of items per group. How can I count the number of distinct g_n? Answer Use count(distinct): There is no need to aggregate using group by for this result. Note: This ignores NULL

using ORDER BY in SQL for chunks of data

I want to know how I sort data in a SQL query but only in certain chunks. I’ll provide an example to make it easier. In the above example, I want to do an ORDER BY height DESC, BUT only the tallest person of each rank gets ordered and everyone else in the same rank is right under that person