accounts_logs = session.query(log_custom.updated_at,log_custom.amount,log_custom.receivable,log_custom.custom,log_custom.accounts_id,log_custom.user_id).filter(log_custom.custom==custom).all()如何根据时间倒序
时间: 2023-07-30 08:08:47 浏览: 51
你可以在查询时使用`order_by`方法来实现根据时间的倒序排列。具体来说,你可以将`order_by`方法传递给查询对象的参数,并按照`updated_at`字段进行排序,如下所示:
```
accounts_logs = session.query(log_custom.updated_at, log_custom.amount, log_custom.receivable, log_custom.custom, log_custom.accounts_id, log_custom.user_id)\
.filter(log_custom.custom == custom)\
.order_by(log_custom.updated_at.desc())\
.all()
```
这将按照`updated_at`字段的降序排列结果。
相关问题
优化sql:SELECT we.organization_id ,we.wip_entity_id ,case when wl.line_id is null then we.wip_entity_name else '' end wip_entity_name ,we.primary_item_id ,mtt.transaction_type_name ,mmt.transaction_date ,bd.department_code ,mmt.inventory_item_id ,mmt.subinventory_code ,mta.reference_account ,br.resource_code ,lu2.meaning as line_type_name ,mta.base_transaction_value ,mta.cost_element_id ,flv.meaning as cost_element ,wdj.class_code job_type_code ,ml.meaning job_type_name FROM (select * from gerp.mtl_material_transactions where substr(transaction_date,1,7) >= '2023-06' and transaction_source_type_id = 5) mmt inner join gerp.wip_entities we on mmt.organization_id = we.organization_id inner join gerp.mtl_transaction_accounts mta on mta.transaction_source_id = we.wip_entity_id and mta.transaction_id = mmt.transaction_id and mta.transaction_source_type_id = 5 inner join gerp.mtl_transaction_types mtt on mtt.transaction_type_id = mmt.transaction_type_id inner join mfg_lookups lu2 on lu2.lookup_code = mta.accounting_line_type and lu2.lookup_type = 'CST_ACCOUNTING_LINE_TYPE' inner join gerp.mtl_system_items_b msi on msi.inventory_item_id = mmt.inventory_item_id and msi.organization_id = mta.organization_id left join gerp.bom_departments bd on bd.department_id = mmt.department_id left join gerp.bom_resources br on br.resource_id = mta.resource_id left join gerp.wip_lines wl on wl.line_id = mmt.repetitive_line_id left join gerp.wip_discrete_jobs wdj on wdj.wip_entity_id = mta.transaction_source_id left join gerp.fnd_lookup_values_vl flv on cast(mta.cost_element_id as string) = flv.lookup_code and flv.lookup_type = 'CST_COST_CODE_TYPE' left join mfg_lookups ml on ml.lookup_code = wdj.job_type and ml.lookup_type = 'WIP_DISCRETE_JOB' 。其中mmt,we,mta,msi,wdj数据量很大
针对这个SQL语句,可以优化的地方有:
1. 使用JOIN语句时,可以根据数据量大小,将数据量大的表放在后面,这样可以避免在连接时出现性能问题。
2. 使用子查询时,可以在子查询中添加条件筛选,减少返回的数据量。
3. 可以对查询中的日期条件进行优化,例如使用日期范围查询时,可以使用BETWEEN关键字代替大于等于和小于等于的运算符。
4. 可以对大表进行分区或分片,以加快查询速度。
5. 可以对JOIN语句中的字段添加索引,以提高查询效率。
下面是对原SQL语句的优化:
```
SELECT
we.organization_id,
we.wip_entity_id,
CASE WHEN wl.line_id is null THEN we.wip_entity_name ELSE '' END wip_entity_name,
we.primary_item_id,
mtt.transaction_type_name,
mmt.transaction_date,
bd.department_code,
mmt.inventory_item_id,
mmt.subinventory_code,
mta.reference_account,
br.resource_code,
lu2.meaning as line_type_name,
mta.base_transaction_value,
mta.cost_element_id,
flv.meaning as cost_element,
wdj.class_code job_type_code,
ml.meaning job_type_name
FROM
gerp.wip_entities we
INNER JOIN (
SELECT
*
FROM
gerp.mtl_material_transactions
WHERE
transaction_date BETWEEN '2023-06-01' AND '2023-06-30'
AND transaction_source_type_id = 5
) mmt ON mmt.organization_id = we.organization_id
INNER JOIN gerp.mtl_transaction_accounts mta ON mta.transaction_source_id = we.wip_entity_id
AND mta.transaction_id = mmt.transaction_id
AND mta.transaction_source_type_id = 5
INNER JOIN gerp.mtl_transaction_types mtt ON mtt.transaction_type_id = mmt.transaction_type_id
INNER JOIN mfg_lookups lu2 ON lu2.lookup_code = mta.accounting_line_type AND lu2.lookup_type = 'CST_ACCOUNTING_LINE_TYPE'
INNER JOIN gerp.mtl_system_items_b msi ON msi.inventory_item_id = mmt.inventory_item_id
AND msi.organization_id = mta.organization_id
LEFT JOIN gerp.bom_departments bd ON bd.department_id = mmt.department_id
LEFT JOIN gerp.bom_resources br ON br.resource_id = mta.resource_id
LEFT JOIN gerp.wip_lines wl ON wl.line_id = mmt.repetitive_line_id
LEFT JOIN gerp.wip_discrete_jobs wdj ON wdj.wip_entity_id = mta.transaction_source_id
LEFT JOIN gerp.fnd_lookup_values_vl flv ON cast(mta.cost_element_id as string) = flv.lookup_code
AND flv.lookup_type = 'CST_COST_CODE_TYPE'
LEFT JOIN mfg_lookups ml ON ml.lookup_code = wdj.job_type AND ml.lookup_type = 'WIP_DISCRETE_JOB';
```
在优化后的SQL语句中,将子查询中的日期范围查询放在了WHERE语句中,将数据量较大的表放在了后面,左连接的表也放在了后面。同时,可以根据具体情况对需要添加索引的字段进行索引优化。
#查询历史记录 def Get_history(): # 连接数据库 conn = pyodbc.connect('DRIVER={SQL Server};SERVER=DESKTOP-JM5K5CS;DATABASE=bank;UID=sa;PWD=1') # 获取游标 cursor = conn.cursor() # 创建窗口 window = tk.Tk() window.title('查询历史记录') # 创建标签和输入框 label_account_number = tk.Label(window, text='卡号:') label_account_number.grid(row=0, column=0) entry_account_number = tk.Entry(window) entry_account_number.grid(row=0, column=1) label_password = tk.Label(window, text='密码:') label_password.grid(row=1, column=0) entry_password = tk.Entry(window, show='*') entry_password.grid(row=1, column=1) label_start_date = tk.Label(window, text='交易日期:') label_start_date.grid(row=2, column=0) entry_start_date = tk.Entry(window) entry_start_date.grid(row=2, column=1) label_transaction_type = tk.Label(window, text='交易方式:') label_transaction_type.grid(row=3, column=0) transaction_var = tk.StringVar(value='所有') transaction_radios = [ ('所有', '所有'), ('存款', '存款'), ('取款', '取款'), ('转入', '转入'), ('转出', '转出') ] for i, (text, value) in enumerate(transaction_radios): radio = tk.Radiobutton(window, text=text, variable=transaction_var, value=value) radio.grid(row=i + 4, column=1) label_password = tk.Label(window, text='备注:') label_password.grid(row=4, column=0) entry_password = tk.Entry(window, show='*') entry_password.grid(row=4, column=1) # 创建按钮和文本框 text_history = tk.Text(window) text_history.grid(row=2, column=0, rowspan=6) def get_history(): # 查询数据 account_number = entry_account_number.get() password = entry_password.get() transaction_date = entry_start_date.get() transaction_type = None if transaction_var.get() != '所有': transaction_type = transaction_var.get() # 查询卡号和密码是否匹配 cursor.execute("SELECT * FROM Accounts WHERE account_number = ? AND password = ?",
这段代码看起来是一个查询银行交易历史记录的功能。它连接了一个名为 "bank" 的数据库,然后创建了一个窗口,里面包含了卡号、密码、交易日期、交易方式、备注等输入框和标签,并且创建了一个文本框用于显示查询结果。在函数 get_history() 中,会根据用户输入的信息来查询数据库,找到匹配的记录,然后在文本框中显示出来。