我是SQL新手,我正在尝试查询在线数据库的子集。我知道我感兴趣的数据受使用两列的方程式限制,并且我想查询方程式。
如果每次要绑定计算时都重复计算,则查询有效,但我想通过将方程式另存为变量来节省执行时间。
以下是重复方程式的代码:
def rearrange_list(input_list,input_item_to_move,input_item_insert_here):
'''
Helper function to re-arrange the order of items in a list.
Useful for moving column in pandas dataframe.
Inputs:
input_list - list
input_item_to_move - item in list to move
input_item_insert_here - item in list,insert before
returns:
output_list
'''
# make copy for output,make sure it's a list
output_list = list(input_list)
# index of item to move
idx_move = output_list.index(input_item_to_move)
# pop off the item to move
itm_move = output_list.pop(idx_move)
# index of item to insert here
idx_insert = output_list.index(input_item_insert_here)
# insert item to move into here
output_list.insert(idx_insert,itm_move)
return output_list
import pandas as pd
# step 1: create sample dataframe
df = pd.DataFrame({
'motorcycle': ['motorcycle1','motorcycle2','motorcycle3'],'initial_odometer': [101,500,322],'final_odometer': [201,515,463],'other_col_1': ['blah','blah','blah'],'other_col_2': ['blah','blah']
})
print('Step 1: create sample dataframe')
display(df)
print()
# step 2: add new column that is difference between final and initial
df['change_odometer'] = df['final_odometer']-df['initial_odometer']
print('Step 2: add new column')
display(df)
print()
# step 3: rearrange columns
ls_cols = df.columns
ls_cols = rearrange_list(ls_cols,'change_odometer','final_odometer')
df=df[ls_cols]
print('Step 3: rearrange columns')
display(df)
这是我要执行的操作的近似值(这不起作用):
SELECT *
FROM online_database
WHERE rnn < 8
AND mol > 12
AND 1 + 0.5*rnn + 0.1*mol < 6
AND 1 + 0.5*rnn + 0.1*mol > 0.2
任何建议都将不胜感激!