在 Sqlalchemy (0.8.2) 中,我正在尝试SELECT 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)
[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) 无法准备。(8180)") u'SELECT count([HR_PERSONNEL].[S_HR_PERSONNEL_ID]) 作为值,CASE WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) < ?) THEN ? WHEN ( floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) BETWEEN ? AND ?) THEN ? ELSE ? END AS anon_1 \nFROM [HR_PERSONNEL] GROUP BY CASE WHEN (floor(datediff(day, [HR_PERSONNEL] .[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) < ?) THEN ? WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) BETWEEN ? AND ?) THEN ? ELSE ? END' (365.25, 15, 15, 365.25, 15, 19, 19, 100, 365.25, 15, 15, 365.25, 15, 19, 19, 100) CASE WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) < ?) THEN ? WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) BETWEEN ? AND ?) THEN ? 别的 ?END AS anon_1 \nFROM [HR_PERSONNEL] GROUP BY CASE WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) < ?) THEN ? WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) BETWEEN ? AND ?) THEN ? 别的 ?结束' (365.25, 15, 15, 365.25, 15, 19, 19, 100, 365.25, 15, 15, 365.25, 15, 19, 19, 100) CASE WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) < ?) THEN ? WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) BETWEEN ? AND ?) THEN ? 别的 ?END AS anon_1 \nFROM [HR_PERSONNEL] GROUP BY CASE WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) < ?) THEN ? WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) BETWEEN ? AND ?) THEN ? 别的 ?结束' (365.25, 15, 15, 365.25, 15, 19, 19, 100, 365.25, 15, 15, 365.25, 15, 19, 19, 100) CURRENT_TIMESTAMP) / ?) 之间?接着 ?别的 ?END AS anon_1 \nFROM [HR_PERSONNEL] GROUP BY CASE WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) < ?) THEN ? WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) BETWEEN ? AND ?) THEN ? 别的 ?结束' (365.25, 15, 15, 365.25, 15, 19, 19, 100, 365.25, 15, 15, 365.25, 15, 19, 19, 100) CURRENT_TIMESTAMP) / ?) 之间?接着 ?别的 ?END AS anon_1 \nFROM [HR_PERSONNEL] GROUP BY CASE WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) < ?) THEN ? WHEN (floor(datediff(day, [HR_PERSONNEL].[D_DATE_OF_BIRTH], CURRENT_TIMESTAMP) / ?) BETWEEN ? AND ?) THEN ? 别的 ?结束' (365.25, 15, 15, 365.25, 15, 19, 19, 100, 365.25, 15, 15, 365.25, 15, 19, 19, 100)
看起来像 MSSQL/ODBC 错误???