sql - last value from a column and clock value

sql last value group by
last value sql
sql last value ignore nulls
first value and last value in sql
last value redshift
sql server last_value group by
how to compare current value with previous value in sql
last value bigquery

i try to have for each host last value of name in showing last clock,

i tried :

SELECT distinct HOST , e.name, e.severity, FROM_UNIXTIME(e.clock), e.clock
     FROM hosts
     LEFT JOIN host_inventory ON (host_inventory.hostid = hosts.hostid)
     LEFT JOIN items i ON (i.hostid = hosts.hostid)
     LEFT JOIN functions f ON (f.itemid = i.itemid)
     INNER JOIN triggers t ON (t.triggerid = f.triggerid AND t.value=1)
     INNER JOIN events e ON (e.objectid = t.triggerid)
     ORDER BY e.clock desc

i have :

i want :

thanks for any response


You could use row_number() to deduplicate:

WITH hosts_cte AS (
  SELECT HOST, 
         e.name, 
         e.severity, 
         FROM_UNIXTIME(e.clock) AS clock_date, 
         e.clock,
         ROW_NUMBER() OVER(PARTITION BY HOST ORDER BY e.clock DESC) AS Rn
  FROM   hosts
  LEFT JOIN host_inventory
       ON host_inventory.hostid = hosts.hostid
  LEFT JOIN items i 
       ON i.hostid = hosts.hostid
  LEFT JOIN functions f 
       ON f.itemid = i.itemid
  INNER JOIN triggers t 
       ON t.triggerid = f.triggerid AND t.value=1
  INNER JOIN events e 
       ON e.objectid = t.triggerid
)

SELECT HOST, 
       name, 
       severity, 
       clock_date, 
       clock
FROM   hosts_cte
WHERE  Rn = 1

SQL Server LAST_VALUE() Function By Practical Examples, scalar_expression is an expression evaluated against the value of the last row in an ordered partition of the result set. The scalar_expression can be a column,� sql - last value from a column and clock value. Viewed 60 times 0. i try to have for each host last value of name in showing last clock, i tried : SELECT distinct


LAST_VALUE, Returns the expression value from the last row in the window. If your table has null values, you can use the IGNORE NULLS clause to return the last non-null� The LAST_VALUE () function is a window function that returns the last value in an ordered partition of a result set. The following shows the syntax of the LAST_VALUE () function: LAST_VALUE (scalar_expression) OVER ([PARTITION BY partition_expression, ] ORDER BY sort_expression [ASC | DESC],


You can use an window analytic function such as row_number() provided your database version is at least 10.2 :

SELECT host, name, severity, clock_un, clock  
  FROM 
  (
    SELECT HOST, e.name, e.severity, FROM_UNIXTIME(e.clock) as clock_un, e.clock,
           row_number() over (partition by host order by e.clock) as rn
      FROM hosts h
      LEFT JOIN items i
        ON i.hostid = h.hostid
      LEFT JOIN functions f
        ON f.itemid = i.itemid
     INNER JOIN triggers t
        ON t.triggerid = f.triggerid AND t.value = 1
     INNER JOIN events e
        ON e.objectid = t.triggerid
  ) t
 WHERE rn = 1
 ORDER BY clock desc 

Demo

LAST_VALUE (Transact-SQL), Returns the last value in an ordered set of values. Is the value to be returned. scalar_expression can be a column, subquery, or other� LAST RECORD WITH LAST_VALUE. There is a function named LAST_VALUE in SQL Server and other variants of SQL as well. This function gives the last record for each group or a subgroup within a group. EXAMPLE #4: LAST_VALUE. In this example we will fetch the last transaction amount of each student in each of the fee heading. The query is as follows:


How to track the history of data changes using SQL Server 2016 , The Period End time column value of the main temporal table will be current transaction start time based on the system clock, and the row If we manage to delete the last row in the temporal table with DempID equal to 5:� The LAST () function returns the last value of the selected column. SQL LAST () Syntax SELECT LAST (column_name) FROM table_name; Note: The LAST () function is only supported in MS Access.


SQL: SELECT with ORDER BY | Computer programming, value to highest value. To change that to "descending order", specify DESC after the column name. INSERT INTO skyscrapers VALUES (4, "One World 3, Abraj Al-Bait Clock Tower, Saudi Arabia, 601, 120 New SQL script. Created by: � 4. LAST_VALUE and PARTITION BY. Here is an example using PARTITION BY with LAST_VALUE function. Similar to the above example; the result of query divided into two partitions of I.T dept and HR dept and applying LAST_VALUE function to each partition to fetch last value from an ordered set of values.


Standard SQL Functions & Operators | BigQuery, An aggregate function is a function that summarizes the rows of a group into a single value. COUNT , MIN and MAX are examples of aggregate functions. RESPECT NULLS (with LAST_VALUE) = returns the last value, whether or not it is null. If all values are null, NULL is returned. window. a group, cumulative, or moving computation. For Window Aggregate Function syntax, see “Window Aggregate Functions” on page 984.