SQL Query to get data for specific columns using python

Viewed 328

I have a table with 1000 columns and I have a list of 100 columns that I want to pull from that table.

I have tried

df = db.query("SELECT * FROM rpt_SalesHistoryMAST where GLPostDate>'2017-08-31' order by GLPostDate;")

but its getting all the data. Is there any way to pull only specific columns that I have in list?

1 Answers

The string join() method is useful - assuming you have a list of 100 columns ready (and in the order layout you prefer

Cols100 = ['Col1', 'Col2', ... 'Col99', 'Col100']
Col100ForQuery = ', '.join(Cols100)
DBSQLQuery = "SELECT " + Col100ForQuery + " FROM rpt_SalesHistoryMAST where GLPostDate>'2017-08-31' order by GLPostDate;"
df = db.query(DBSQLQuery)
Related