0

Below is the table structure:

| Createdtime              | Value
---------------------------|--------------
|2019-06-02 00:25:35.86+08 |7805165.31 Mb
|2019-06-02 00:03:52.645+08|7796650 Mb
|2019-06-02 05:16:30.108+08|7757693.25 Mb
|2019-06-02 05:41:25.737+08|7758570.94 Mb
|2019-06-02 05:32:17.743+08|7758576.31 Mb
|2019-06-02 05:59:59.817+08|7760187.25 Mb
|2019-06-02 05:50:47.463+08|7758511.81 Mb
|2019-06-03 11:19:54.337+08|7764680.25 Mb
|2019-06-03 11:42:35.448+08|7764677.63 Mb
|2019-06-03 11:54:08.578+08|7764681.63 Mb
|2019-06-03 12:18:19.116+08|7764683.63 Mb
|2019-06-03 12:30:52.553+08|7764686.63 Mb
|2019-06-03 22:57:51.778+08|7728657.13 Mb
|2019-06-03 23:12:38.93+08 |7735638.06 Mb
|2019-06-03 23:25:21.318+08|7706719.19 Mb

I want the output like this:

Created date | 1st time                   | 1st value  | last time                  | last value | value diff
-------------+----------------------------+------------+----------------------------+------------+-----------
2019-06-02   | 2019-06-02 00:25:35.86+08  | 7805165.31 | 2019-06-02 05:50:47.463+08 | 7758511.81 | -46653.5
2019-06-02   | 2019-06-03 11:19:54.337+08 | 7764680.25 | 2019-06-03 23:25:21.318+08 | 7706719.19 | -57961.06
2
SELECT DISTINCT 
    DATE_TRUNC('day', Createdtime) "Created date",
    MIN(Createdtime) OVER (PARTITION BY DATE_TRUNC('day', Createdtime)) "1st record",
    FIRST_VALUE(Value)  OVER (PARTITION BY DATE_TRUNC('day', Createdtime)
                              ORDER BY Createdtime ASC) "1st Value",
    MAX(Createdtime) OVER (PARTITION BY DATE_TRUNC('day', Createdtime)) "last record",
    FIRST_VALUE(Value)  OVER (PARTITION BY DATE_TRUNC('day', Createdtime)
                              ORDER BY Createdtime DESC) "last Value",
    FIRST_VALUE(Value)  OVER (PARTITION BY DATE_TRUNC('day', Createdtime)
                              ORDER BY Createdtime ASC)
    -
    FIRST_VALUE(Value)  OVER (PARTITION BY DATE_TRUNC('day', Createdtime)
                              ORDER BY Createdtime DESC) "diff in Value"
FROM sourcetable
  • ERROR: function firts_value(character varying) does not exist LINE 4: FIRTS_VALUE(Value) OVER (PARTITION BY DATE_TRUNC('day',... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. SQL state: 42883 Character: 159 – Sridhar Balaraman Jul 12 at 4:47
  • Thank you So Much... it works.. I'm very new to SQL... – Sridhar Balaraman Jul 12 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.