晚上好,朋友们,自从上次查询SQL Alchemy以来,我已经进化得很好了。
目前我有以下查询:
# Subquery to get the maximum DtReference value for each IdProduct
stockCurrent = session.query(StockCompany.IdProduct, func.max(StockCompany.DtReference).label("max_DtReference"), StockCompany.QtyStock)\
.group_by(StockCompany.IdProduct, StockCompany.QtyStock)\
.subquery()
# Query that joins the subquery with the original query
query = session.query(ProductCode.CallCode, Product.ProductNm, ProductCompany.CompanyCode, CurrentStock.c.StockQty, CurrentStock.c.max_DtReference)\
.join(ProductCode, Product.IdProduct == ProductCode.IdProduct)\
.join(ProductCompany, Product.IdProduct == ProductCompany.IdProduct)\
.join(CurrentStock, (Product.IdProduct == CurrentStock.c.IdProduct) & (CompanyStock.DtReference == CurrentStock.c.max_DtReference))\
.filter(ProductCompany.CdCompany == 4)\
.order_by(desc(ProductCode.CdCall))
- 思路是只列出stockCompany表中最新的记录,然后与其他表进行联接。**
- 问题是退货**
追溯(最近一次调用):文件"C:\Users\EliasPai\kivy_venv\lib\site-packages\sqlalchemy\engine\base.py",line 1900,in_execute_context www.example.com_execute(文件"C:\Users\EliasPai\kivy_venv\lib\site-packages\sqlalchemy\engine\default.py",line 736,in do_execute cursor. execute(statement,parameters)pyodbc. ProgrammingError:self.dialect.do[42000][Microsoft][ODBC Driver 17 for SQL Server][SQL Server]无法准备一个或多个语句。(8180)') ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The multipart identifier "InventoryCompany.DtReference" could not be associated. (4104) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Could not prepare one or more statements. (8180)')
上述异常是以下异常的直接原因:
追溯(最近一次调用):文件“c:\Users\EliasPai\Downloads\codes_sv(1)\fone.py ",line 53,in result = query. all()File" C:\Users\EliasPai\kivy_venv\lib\site-packages\sqlalchemy\orm\query.py ",line 2773,in all return self._iter(). all()File" C:\Users\EliasPai\kivy_venv\lib\site-packages\sqlalchemyorm\query.py ",line 2916,in_iter result = self. session. execute(File" C:\Users\EliasPai\kivy_venv\lib\site-packages\sqlalchemyorm\session.py",line 1714,in execute result = conn._execute_20(statement,params or {},execution_options)File" C:\Users\EliasPai\kivy_venv\lib\site-packages\sqlalchemy\engine\base.py",line 1705,in _execute_20 return meth(self,args_10style,kwargs_10style,execution_options)File" C:\UsersEliasPai\kivy_venv\lib\site-packages\sqlalchemy\sql\elements.py",line 334,in_execute_on_connection return connection._execute_on_connection(File" C:\Users\EliasPai\kivy_venv\lib\site-packages\sqlalchemy\sql\engine\base.py",line 1572,in_execute_cl
1条答案
按热度按时间mzillmmw1#