在Sqlalchemy(0.8.2)我试图选择CASE GROUP BY CASE 类似的东西:
query = """
SELECT count([HR_PERSONNEL].[S_HR_PERSONNEL_ID]) AS value,
CASE
WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / 365.25) < 15) THEN 15
WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / 365.25) BETWEEN 15 AND 19) THEN 19
ELSE 100 END AS anon_1
FROM [HR_PERSONNEL]
GROUP BY CASE
WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / 365.25) < 15) THEN 15
WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / 365.25) BETWEEN 15 AND 19) THEN 19
ELSE 100 END"""
可以使用SA执行纯文本SQL查询而不会出现问题:
>>> for i in mssql_engine.execute(query):
print i
(11, 15)
(1097, 19)
(63878, 100)
但我在SQLAlchemy ORM中遇到同一个查询的问题:
>>> age_case_groups =case([( floor_function < 15, 15)\
,(between(floor_function,15,19), 19)],else_=100)
>>> vols_by_age = session.query(func.count(HrPersonnel.S_HR_PERSONNEL_ID).label('value'),age_case_groups)\
.group_by(age_case_groups)
>>> vols_by_age.all()
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
vols_by_age.all()
File "build\bdist.win32\egg\sqlalchemy\orm\query.py", line 2104, in all
return list(self)
File "build\bdist.win32\egg\sqlalchemy\orm\query.py", line 2216, in __iter__
return self._execute_and_instances(context)
File "build\bdist.win32\egg\sqlalchemy\orm\query.py", line 2231, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "build\bdist.win32\egg\sqlalchemy\engine\base.py", line 662, in execute
params)
File "build\bdist.win32\egg\sqlalchemy\engine\base.py", line 761, in _execute_clauseelement
compiled_sql, distilled_params
File "build\bdist.win32\egg\sqlalchemy\engine\base.py", line 874, in _execute_context
context)
File "build\bdist.win32\egg\sqlalchemy\engine\base.py", line 1024, in _handle_dbapi_exception
exc_info
File "build\bdist.win32\egg\sqlalchemy\util\compat.py", line 163, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "build\bdist.win32\egg\sqlalchemy\engine\base.py", line 867, in _execute_context
context)
File "build\bdist.win32\egg\sqlalchemy\engine\default.py", line 324, in do_execute
cursor.execute(statement, parameters)
ProgrammingError :( ProgrammingError)('42000',“[42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]列 'HR_PERSONNEL.D_DATE_OF_BIRTH'在选择列表中无效,因为 它不包含在聚合函数或GROUP BY中 条款。 (8120)(SQLExecDirectW); [42000] [Microsoft] [ODBC SQL Server 驱动程序] [SQL Server]列'HR_PERSONNEL.D_DATE_OF_BIRTH'无效 选择列表,因为它不包含在聚合中 函数或GROUP BY子句。 (8120); [42000] [Microsoft] [ODBC SQL 服务器驱动程序] [SQL Server]列'HR_PERSONNEL.D_DATE_OF_BIRTH'是 在选择列表中无效,因为它不包含在任何一个中 聚合函数或GROUP BY子句。 (8120); [42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]语句不能 做好准备(8180)“)你的意思 count([HR_PERSONNEL]。[S_HR_PERSONNEL_ID])AS值,CASE WHEN (楼层(dateiff(天,[HR_PERSONNEL]。[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP)/?)&lt; ?) 然后 ?什么时候(楼(日期) [HR_PERSONNEL]。[D_DATE_OF_BIRTH],CURRENT_TIMESTAMP)/?)BETWEEN? 然后 ? ELSE?结束为anon_1 \ nFROM [HR_PERSONNEL] GROUP BY CASE 什么时候(楼层(日期,日期,[HR_PERSONNEL]。[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP)/?)&lt; ?) 然后 ?什么时候(楼(日期) [HR_PERSONNEL]。[D_DATE_OF_BIRTH],CURRENT_TIMESTAMP)/?)BETWEEN? 然后 ? ELSE?结束'(365.25,15,15,365.25,15,19,19,100, 365.25,15,15,365.25,15,19,19,100)
看起来像MSSQL / ODBC bug ???