Convert SQL into Python code with if-else statement

Viewed 59

I am trying to convert SQL code to python. SQL code is given below:

select count(*),sum(park) into :A, :B
from _fx_bfr_wgt
where park=1 and tickt=1 and input(zip,8.) in (700007:73599);
select sum(sum(Col1),sum(Col12),sum(Col3),
            sum(Col4),sum(Col5)) into :Total_sum
        from _fx_weights;

I was trying to develop a python code based on the above SQL code. Python code is given below:

A = _fx_bfr_wgt.shape[0]
print(A)
B = _fx_bfr_wgt['park'].sum()
print(B)


if fx_bfr_wgt[(park==1 and tickt==1) and (zip in range(700007:73599))]:
    A = _fx_bfr_wgt.shape[0]
else:
    np.NaN
if fx_bfr_wgt[(park==1 and tickt==1) and (zip in range(700007:73599))]:
    B = _fx_bfr_wgt['park'].sum()
else:
    np.NaN
 _fx_weights

Total_sum=_fx_weights['Col1'].sum()+_fx_weights['Col2'].sum()+_fx_weights['Col3'].sum()+_fx_weights['Col4'].sum()+_fx_weights['Col5'].sum()

While ruiing the code I am getting an error NameError: name 'park' is not defined

I am not so sure about the logic that I have developed to convert the SQL into python. I am new in python and will be needing help to convert this SQL code into python.

1 Answers

Yes Python interprets it as a variable name but it is a string, simply change it to

'park'==1 and 'tickt'==1
Related