noamgot noamgot - 2 months ago 9
SQL Question

get most updated parameter (SQL)

consider the following table:

+-------+-----------+-----------+-----------+
| ID | param1 | param2 | param3 |
+-------+-----------+-----------+-----------+
| 1 | 12 | 21 | 1 |
+-------+-----------+-----------+-----------+
| 2 | 11 | 3 | 22 |
+-------+-----------+-----------+-----------+
| 3 | 55 | 17 | 22 |
+-------+-----------+-----------+-----------+


I need an SQL query which gets the value of the most updated (= highest ID) value of a single parameter (for example, if I want the most updated param2 I want the query to return 17).
I must say that I don't have any knowledge in SQL, I just encountered it at work and I need a solution for this specific problem. when I searched for solutions I only found solutions for getting the whole row, I only need a single parameter from it.
thanks.

Answer

SQL Server:

SELECT TOP 1 Param2
FROM tableName
ORDER BY ID DESC

Oracle:

SELECT Param2
FROM tableName
ORDER BY ID DESC
WHERE ROWNUM <= 1

PostgreSQL:

SELECT Param2
FROM tableName
ORDER BY ID DESC
LIMIT 1