I have below two dataframes and want to populate the final dataframe by joining two two input dataframes.
df1 i.e table1
id | code | name | location | val | date
1000 | 1 | 'A' | 'AABB' | 1 | 2021-01-01
1000 | 2 | 'B' | 'BBCC' | 3 | 2021-01-01
1000 | 3 | 'C' | 'CCDD' | 4 | 2021-01-01
1000 | 4 | 'D' | 'DDEE' | 1 | 2021-01-01
2000 | 1 | 'E' | 'EEFF' | 5 | 2021-03-01
2000 | 2 | 'F' | 'XXYY' | 4 | 2021-03-01
2000 | 3 | 'G' | 'YYZZ' | 2 | 2021-03-01
2000 | 4 | 'H' | 'ZZAA' | 1 | 2021-03-01
2000 | 4 | 'I' | 'IIII' | 1 | 2021-03-01
df1.createOrReplaceTempView('df1')
df2 i.e table2
id | city | dist | state | count | tot_sum | date
1000 | null | null | null | null | null | 2021-01-01
2000 | null | null | null | null | null | 2021-03-01
df2.createOrReplaceTempView('df2')
df3 i.e table3
id | city | dist | state | count | tot_sum | date
1000 | 'AABB' | 'BBCC' | 'CCDD' | 1 | 9 | 2021-01-01
2000 | 'EEFF' | 'XXYY' | 'YYZZ' | 2 | 13 | 2021-03-01
Logic:
when code =1 then consider location as city
when code =2 then consider location as dist
when code =3 then consider location as state
when code =4 then count the total number of records for that code for that id i.e in case of id 1000 we have only one record with code 4, in case of id 2000 we have 2 records
with code 4 sum of all vals for that id is the tot_sum i.e for id 1000 it will be 1+3+4+1=9, for id 2000 it will be 5+4+2+1+1=13
trying something like below however it didn't work
select d2.id as id,
d2.date as date,
CASE WHEN d1.code=1 then d1.location else null end as city,
CASE WHEN d1.code=2 then d1.location else null end as dist,
CASE WHEN d1.code=3 then d1.location else null end as state
FROM df1 d1 join df2 d2 on d1.id=d2.id
select d2.id,
d2.date
CASE WHEN d1.code=1 then state=d1.location,
CASE WHEN d1.code=2 then dist=d1.location,
CASE WHEN d1.code=3 then CityName=d1.location
FROM df1 d1 join df2 d2 on d1.id=d2.id
Any suggestions?
Note: Looking for a SQL Query(considering two input tables)/Pyspark dataframes/Pandas dataframes
DF1:

