I need help in converting the below function into an SQL query:
start_time :- 1649289600
end_time :- 1649375999
test_data = df.withColumn("from_timestamp",to_timestamp(lit(from_unixtime(col("start_time"),'MM-dd-yyyy HH:mm:ss:SSS')), 'MM-dd-yyyy HH:mm:ss:SSS')) \
.withColumn("to_timestamp",to_timestamp(lit(from_unixtime(col("end_time"),"MM-dd-yyyy HH:mm:ss:SSS")), 'MM-dd-yyyy HH:mm:ss:SSS')) \
.withColumn("DiffInSeconds", col("from_timestamp").cast("long") - col("to_timestamp").cast("long")) \
.withColumn("time_diff_hr", abs(ceil(col("DiffInSeconds")/3600))) \
.withColumn("minDiffInHours", col("DiffInSeconds")/3600 - ceil(col("DiffInSeconds")/3600)) \
.withColumn("time_diff_min",abs(ceil(col("minDiffInHours")*60))) \
.withColumn("minDiffInSec", col("minDiffInHours")*60 - ceil(col("minDiffInHours")*60)) \
.withColumn("time_diff_sec",abs(ceil(col("minDiffInSec")*60)))
I have tried quite a few things like:
df2=sqlContext.sql("SELECT start,end,from_unixtime(cast(start as string), 'MM-dd-yyyy HH:mm:ss:SSS') AS start_date,from_unixtime(cast(end as string), 'MM-dd-yyyy HH:mm:ss:SSS') AS end_date from data1")
df2.show()
df2.createOrReplaceTempView('data2')
df4=sqlContext.sql("SELECT start,end,start_date,end_date,(end_date-start_date) as time_diff from data2")
But whenever I try to find the difference it is returning null values.
Edit:
Output for data1
+---+---+----+----------+----------+--------------------+--------------------+
| a| b| c| start| end| start_date| end_date|
+---+---+----+----------+----------+--------------------+--------------------+
| 1|4.0|GFG1|1649289600|1649375999|04-07-2022 00:00:...|04-07-2022 23:59:...|
+---+---+----+----------+----------+--------------------+--------------------+