How to convert this Python code to SQL query using only a select statement?

Viewed 258

I have created a data frame in Python like given below

    df = pd.DataFrame({'status': ["Applied", "Applied", "NotApplied", "Applied", "NotApplied", "Applied", "Applied"],
    'date1':   ["2022-02-02", "2022-03-10", np.nan, np.nan, "2022-02-25", "2022-01-16", np.nan],
    'date2':["2022-02-02", "2022-03-10", "2022-02-14", "2022-01-05", "2022-02-25", "2022-01-16", "2022-02-16"]})
    df['date1'] = pd.to_datetime(df['date1'], format='%Y-%m-%d')
    df['date2'] = pd.to_datetime(df['date2'], format='%Y-%m-%d')
status date1 date2
0 Applied 2022-02-02 00:00:00 2022-02-02 00:00:00
1 Applied 2022-03-10 00:00:00 2022-03-10 00:00:00
2 NotApplied NaN 2022-02-14 00:00:00
3 Applied NaN 2022-01-05 00:00:00
4 NotApplied 2022-02-25 00:00:00 2022-02-25 00:00:00
5 Applied 2022-01-16 00:00:00 2022-01-16 00:00:00
6 Applied NaN 2022-02-16 00:00:00

And then applied the below code

df.loc[ (df['status'] == 'Applied') , 'date1'] = df['date2']
df = df[df['status'] == 'Applied']
status date1 date2
0 Applied 2022-02-02 00:00:00 2022-02-02 00:00:00
1 Applied 2022-03-10 00:00:00 2022-03-10 00:00:00
3 Applied 2022-01-05 00:00:00 2022-01-05 00:00:00
5 Applied 2022-01-16 00:00:00 2022-01-16 00:00:00
6 Applied 2022-02-16 00:00:00 2022-02-16 00:00:00

Question

How should I write the above 2 lines of code in SQL using only a select statement (I don't want to alter or update the current table in SQL).

3 Answers
select status, date2 as 'date1', date2 from table where status = 'Applied'

Just put your table name and run these query on your sql editor

SELECT status,date2 as date1,date2 FROM `Your Table Name` WHERE status='Applied'

try this

"select * from table_name where status = 'Applied'"

just replace table_name with your table name

Related