-1

If data of table like this,

[co1:varchar][col2:varchar][col3:varchar][col4:timestamp]

And, Follow is data example.

224 / 755 / 542 / 2019-05-23 10:12:23
230 / 741 / 530 / 2019-05-23 10:12:23
221 / 769 / 567 / 2019-05-23 10:12:23
230 / 794 / 521 / 2019-05-23 10:12:23
...

I want to get one row with max value of each column for the same time rows. Like,

230 / 768 / 542 / 2019-05-23 10:12:23

how can I do for It?

  • 1
    why are numbers in varchar columns? I assumed you want a numerical maximum. – danblack May 23 at 1:20
  • Have you tried the max() function? – mustaccio May 23 at 1:34
  • What queries have you tried? It should take too much into a tutorial to find and answer here. – danblack May 23 at 1:34
  • Never store numbers in varchar columns. – a_horse_with_no_name May 23 at 1:39
  • Of course, I did not design this table. – user3920450 May 23 at 5:53
2

This would be as simple as using max() on every column:

select max(col1), max(col2), max(col3), max(col4)
from the_table;

However, due to your wrong data design, you have to cast the varchar values to a number before you can do that:

select max(col1::integer), max(col2::integer), max(col3::integer), max(col4)
from the_table;

Note that this will fail if you accidentally stored something in those columns that isn't actually a number.

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.