Making a Condition to create a new table which outputs 1 or 0 based on Timediff values Query SQL

52 Views Asked by At

https://i.stack.imgur.com/DLQiC.png. SQL QUERY. So for my query here according to another table I found the timediff between two fields and then created 2 new column that will output the diff between those 2 fields (start and end time). For the second part of my code I want to take those diff's and create new columns that basically tells if the diffs are > 0 then that its 1 and if its = or < 0 then its 0. So basically the last 2 column for the result will only have 1s or 0s...how do I make the condition for this? Just to make sure there will be a total of 6 columns when running after that condition: Expected_Start_Time, Actual_Start_Time, Difference_Start_Time, Difference_End_Time, new column with start time 1s or 0s, and column with end time 1s or 0s.

1

There are 1 best solutions below

0
On

Last two columns added as Start_time_Flag and End_time_Flag. Please execute and share the feedback.


SELECT Expected_Start_Time, Actual_Start_Time,
TIMESTAMP_DIFF (Actual_Start_Time, Expected_Start_Time, MINUTE) AS Difference_Start_Time,
TIMESTAMP_DIFF (Actual_End_Time, Expected_End_Time, MINUTE) AS Difference_End_Time,
if(safe_cast(TIMESTAMP_DIFF (Actual_Start_Time, Expected_Start_Time, MINUTE) as numeric) <= 0 ,0,1) as Start_time_Flag,
if(safe_cast(TIMESTAMP_DIFF (Actual_End_Time, Expected_End_Time, MINUTE) as numeric) <= 0,0,1) as End_time_Flag
FROM “np-inventory-planning-thd.IPP_SLA.sla_dummy_data’