I have the following dataframe:
col1 col2
1 2020-02-27 15:00:00
1 2020-02-27 15:04:00
I need the output as
col1 col2 col3
1 2020-02-27 15:00
1 2020-02-27 15:04 Y
Based on the maximum timestamp value present in col2, col3 value has to be populated as Y or null.
I have tried the below approach:
df = spark.sql("select col1,col2 from table")
max_ts = df.select(max("col2")).show()
y=(f.when(f.col('col2') == max_ts, "Y"))
df1 = df.withColumn('col3',y)
The above approach yields only null output.
Kindly suggest the possible solution or the mistakes?
TIA.
Edit: I need to perform groupBy on col1 and fetch max value in col2