Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13981

update rows with adjacent dates with same location

$
0
0

want to update the dates eg if the dates are as below then update the end date of the date to2024-03-19 16:27:00.000 for record 1 and delete record 2

  1. 2024-03-17 22:59:00.000 2024-03-19 06:59:00.000
  2. 2024-03-18 18:27:00.000 2024-03-19 16:27:00.000
import pyodbcimport pandas as pdcnxn = pyodbc.connect('DRIVER=ODBC Driver 17 for SQL Server;SERVER=server;DATABASE=database;UID=username;PWD=Password')query = "SELECT [Contract],[Unit],[Type],[ScheduleStart],[ScheduleEnd],[ActualStart],[ActualEnd],[ReasonCode],[Location],[Deleted] FROM [dbo].[ugtx_MaintenanceEvent];"sql_data = pd.read_sql(query, cnxn)for i, current_row in sql_data.iterrows():    current_start_date = current_row['ScheduleStart']    current_end_date = current_row['ScheduleEnd']    current_location = current_row['Location']    for j, other_row in sql_data.iterrows():        if i != j:            other_start_date = other_row['ScheduleStart']            other_end_date = other_row['ScheduleEnd']            other_location = other_row['Location']            if current_end_date >= other_start_date and current_end_date <= other_end_date  and current_location == other_location:                sql_data.at[i, 'ScheduleEnd'] = other_end_date                sql_data = sql_data.drop(j)                breakprint(sql_data)

Viewing all articles
Browse latest Browse all 13981

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>