Questions tagged [max]

Maximum value. Largest, biggest, greatest.

max

max() is a math library function available in many programming environments which returns the entity of greatest value from two or more candidate values.

Do not use for questions about the maximal value of a type, e.g. Long.MAX_VALUE, Integer.MAX_VALUE, etc.

Examples

SQL

SQL MAX() function is one of aggregate functions. It returns the largest value of column.

SELECT MAX(column_name) FROM table_name;

Python

In Python, the native max function identifies the largest element of a set (e.g. a list).

>> max([1, 2, 3])
3

Java

In Java we can use java.lang.Math class to compute maximum value of two arguments.

int maxElement = Math.max(x, y);

To compute maximum element in collection we can use, max() method from java.util.Collections class.

int maxElement = Collections.max(list);

Ruby

In Ruby to compute maximum element of collection we simply use max function.

[4,7].max

Clojure

In Clojure we use max function in the following way.

(max 1 2 3 4)

In above code max is the function name and numbers 1..4 are arguments passed to it. We can also apply the max function to lists.

(apply max [1 2 3 4])

R

In R we have function max.

x <- c(1, 2, 3)
y <- c(4, 5)
z <- c(6, NA)
max(x)
max(y)
max(z)
max(z, na.rm = TRUE)
max(x, y, z, na.rm = TRUE)  ## as same as max(c(x, y, z), na.rm = TRUE)

Rust

In Rust we can use std::cmp::max to compute the maximum value of two arguments.

let max = std::cmp::max(x, y);

We can compute the maximum element in a collection by transforming it into an Iterator and then calling the max method on the iterator.

let max = collection.into_iter().max();
8982 questions
2
votes
1 answer

SQL - joining 3 tables and choosing newest logged entry per id

I got rather complicated riddle to solve. So far I'm unlocky. I got 3 tables which I need to join to get the result. Most important is that I need highest h_id per p_id. h_id is uniqe entry in log history. And I need newest one for given point…
Bartek K
  • 41
  • 3
2
votes
1 answer

Extract max of a multiindex pandas dataframe with strings and NaN

I've got the following multiindex dataframe: first bar baz foo second one two one two one two first second …
NicoH
  • 1,240
  • 3
  • 12
  • 23
2
votes
2 answers

MAX Date for each worker

Consider two tables. Worker table has fields like W_ID, W_Name. Course table has fields like C_ID, C_Name, C_Date. A Worker can take many courses and a course can be taken by many Workers. Therefore, there is a many to many relationship between the…
Qatrelnada
  • 45
  • 6
2
votes
2 answers

How to use min() only with first column of 2-dimensional list

I have 2-dimensional list in which each entry has a timestamp (int) and a second value that can be an int, string or object. I'm using the min() function to get the entry with the lowest timestamp: a = [[2, "start C"], [6, 2484], [15,…
2
votes
1 answer

How to mark the min and the max values in an Open Office Calc column?

I have a table like this: | A | B | |---|---| | a | 5 | <- max, should be red | b | 1 | <- min, should be green | c | 0 | <- zero, should not count | d | 1 | <- min, should be green | e | 3 | | f | 5 | <- max, should be red | g | 4 | | h | 0 | <-…
automatix
  • 14,018
  • 26
  • 105
  • 230
2
votes
2 answers

Function to remove columns with max value less than a given value,

I'm doing initial data clean up with 34,000 columns in a dataframe and in order to do that, i have to remove columns whose max value is less than 2. I'm clueless as to how to remove columns with maxvalue less than 2 but for just getting max values,…
Heena
  • 113
  • 1
  • 2
  • 6
2
votes
1 answer

How to derive max date on LISTAGG Oracle 12c

Oracle 12c installation. I am having trouble organizing Oracle listagg() to show the count of cars column of my query to display the results of only the max(inventory_date) data per location. I was thinking to create a view to aggregate the…
Gilly
  • 45
  • 7
2
votes
2 answers

SQL get all names that have their MAX() where month='jan'

I'm searching a SQl query that gives me all values that have their max() at a certain time. This is my simplified table: +---------+-------+-------+ | name | value | month | +---------+-------+-------+ | joe | 10 | jan …
JoergP
  • 1,349
  • 2
  • 13
  • 28
2
votes
4 answers

MySQL MAX() is running pretty slow

I'm running a website that allows users to scrape and analyse their Instagram competitors / personal accounts, to monitor their growth and see how things are going and if they're on track with their growth targets. For this specific portion of the…
Pr4w
  • 103
  • 10
2
votes
1 answer

SQL Multiple inner joins with max() for latest recorded entry

Attempting to build SQL with INNER JOIN's. The INNER JOIN's work ok, now I need to add the MAX() function for limiting the rows to just most recent. Added this INNER JOIN client_diagnosis_record ON SELECT cr.PATID, cr.date_of_diagnosis,…
manager_matt
  • 395
  • 4
  • 19
2
votes
4 answers

Microsoft SQL Server need only the highest results in a query

I created the query below but there's no way I have been able to return only the highest value from the column b.new_versionnumber. Whatever is the highest value on that column, I want to return all the rows that contain that highest value. SELECT …
snowman
  • 21
  • 1
2
votes
2 answers

Min() and Max() based on partition in sql server

I want to use min & max function but on certain criteria. Create Table #Test (Id Int Identity(1,1), Category Varchar(100), DateTimeStamp DateTime) Insert into #Test (Category,DateTimeStamp) values ('c1','2019-08-13 01:00:13.503') …
Hardik Parmar
  • 1,053
  • 3
  • 15
  • 39
2
votes
1 answer

Find the maximum absolute value by row in an R data frame

I am hoping to find a vectorized approach to get the absolute maximum value from multiple columns in a data frame. Basically is there an equivalent to the pmax function for getting absolute maximums. test_df <- tibble( some_identifier =…
cavamic
  • 69
  • 7
2
votes
2 answers

Get max value from varchar column in MySQL

I need to get max value from a column with varchar type. The value not too complicated, just simple values like CAR 001 (without space), CAR 002 (without space), CAR 003 (without space). I tried any answer from others question similar to this, but…
2
votes
2 answers

How to give value to the one which is not equal to max value in each row?

I have a data frame which contains some values. And now I would like to keep only the max value in each row and give 0 to the rest column, like this: df <- data_frame(a= c(1,2,3,4,5),b= c(2,5,3,9,7),c= c(40,6,2,1,7)) df$rowmax <- apply(df,1,max) # …
Codezy
  • 662
  • 5
  • 17