title: Need a query in Sqlalchemy with GROUP BY CASE
body: <p>All i need is a simple query with GROUP BY CASE like</p>
  
  <pre><code>SELECT 
    COUNT([Personnel].[PERSONNEL_ID]) AS value,
    CASE 
      WHEN ([Personnel].[T_FIRSTNAME] = 'John') THEN 'John' 
      ELSE 'somethingelse' 
    END AS somelabel 
  FROM [Personnel] 
  GROUP BY 
    CASE 
      WHEN ([Personnel].[T_FIRSTNAME] = 'John') THEN 'John' 
      ELSE 'somethingelse' 
    END
  </code></pre>
  
  <p>in Sqlalchemy my query looks like this:</p>
  
  <pre><code>case_groups = case(
      [(HrPersonnel.T_FIRSTNAME=='John', 'John')],
      else_='somethingelse'
  )
  
  volunteers = session_mssql.query(
      func.count(HrPersonnel.S_HR_PERSONNEL_ID).label('value'),
      case_groups.label('somelabel'))\
      .group_by(case_groups)\
      .all()
  </code></pre>
  
  <p>mssql server executes the query without any problem (engine.connect().execute(text_query))</p>
  
  <p>but Sqlalchemy ORM query gives me an error.</p>
  
  <blockquote>
    <p>File
    "c:\projects\sa_test\eggs\sqlalchemy-0.9.2-py2.7.egg\sqlalchemy\engine\default.py",
    line 425, in do_execute cursor.execute(statement, parameters)
    sqlalchemy.exc.ProgrammingError: (ProgrammingError) ('42000', "[42000]
    [Microsoft][ODBC SQL Server Driver][SQL Server] Column
    'Personnel.T_FIRSTNAME' is invalid in the select list because it is
    not contained in either an aggregate function or the GROUP BY clause.
    (8120) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server
    Driver][SQL Server]Statement(s) could not be prepared. (8180)") 
    u'SELECT count([Personnel].[PERSONNEL_ID]) AS value, CASE WHEN
    ([Personnel].[T_FIRSTNAME] = ?) THEN ? ELSE ? END AS somelabel \nFROM
    [Personnel] GROUP BY CASE WHEN ([Personnel].[T_FIRSTNAME] = ?) THEN ?
    ELSE ? END' ('John', 'John', 'somethingelse', 'John', 'John',
    'somethingelse')</p>
  </blockquote>
  
  <p>Why 'Personnel.T_FIRSTNAME' is invalid in the select list?</p>
  
  <p>any suggestions are welcome.
  Thanks,</p>