Aug-20-2021, 03:45 AM
(Aug-19-2021, 07:13 PM)deanhystad Wrote: I don't understand your question. Could you provide an example of real data?
I have the data in the .csv file as below. I have attached the .csv file in the first post of this thread.
DB SCH TAB COL
DEV SCOTT EMP EMPNO
DEV SCOTT EMP ENAME
DEV SCOTT EMP DEPTNO
DEV SCOTT DEPT DEPTNO
DEV SCOTT DEPT DNAME
DEV SCOTT DEPT LOC
And I have wrote the below code and almost got the output except a minor one in the print
df = pd.read_csv("view.csv") #print(df) df1=df.groupby(['DB','SCH','TAB'])['COL'].apply(','.join) # groupby_db = df.groupby("DB") # print(groupby_db) # grouped_lists = groupby_db["COL"].apply(list) # print(grouped_lists) #print(df1) df2 = df1.reset_index(name = 'COLS') print (df2) for ind,row in df2.iterrows() : if ind > len(df2): break else: f=open(row["TAB"]+ '_V.txt','w') f.write("CREATE OR REPLACE VIEW " + row["DB"] + "." + row["SCH"]+ "." + row["TAB"]+ "_V \nAS\nSELECT\n"+row["COLS"] + "\nFROM\n"+ row["DB"] + "." + row["SCH"]+ "." + row["TAB"]+";") f.close() ind+=1 print("CREATE OR REPLACE VIEW " + row["DB"] + "." + row["SCH"]+ "." + row["TAB"]+ "_V \nAS\nSELECT\n"+row["COLS"] + "\nFROM\n"+ row["DB"] + "." + row["SCH"]+ "." + row["TAB"]+";")All I want to get is
CREATE OR REPLACE VIEW DEV.SCOTT.EMP_V
AS
SELECT
EMPNO,
ENAME,
DEPTNO
FROM
DEV.SCOTT.EMP;
What I m getting is
CREATE OR REPLACE VIEW DEV.SCOTT.EMP_V
AS
SELECT
EMPNO,ENAME,DEPTNO
FROM
DEV.SCOTT.EMP;
All I need is this minor change to my code or else any far better approach.
Attached Files
view.csv (Size: 143 bytes / Downloads: 76)