我在python中使用sql炼金术使用connection.execute()时收到以下错误

时间:2016-12-13 07:03:12

标签: python sql

  

ProgrammingError:(psycopg2.ProgrammingError)语法错误或附近   " FROM"第1行:.... id = h.sender_user_id和情绪IN(" +"," - ")   来自"用户......

[SQL: 'SELECT u.id as userid, CASE WHEN h.receiver_user_id = u.id THEN "Given" ELSE "Received" END AS Type, h.sentiment as sentiment, h.context as context,"2016-08-06 00:00:00" as time_period WHERE u.id = h.receiver_user_id OR u.id = h.sender_user_id AND sentiment IN ("+","-") FROM "User" u, "HoorahTransaction" h GROUP BY (userid,Type,sentiment,context)']

1 个答案:

答案 0 :(得分:0)

排序sql查询

SELECT u.id AS userid,
       CASE
           WHEN h.receiver_user_id = u.id
                AND h.sender_user_id IS NOT NULL THEN 'Received'
           WHEN h.sender_user_id=u.id THEN 'Given'
           ELSE NULL
       END AS TYPE,
       h.sentiment AS sentiment,
       h.context AS context,
       '{0}' AS time_period,
       COUNT(*) AS value
FROM "User" u,
     "HoorahTransaction" h
WHERE (u.id= h.receiver_user_id
       OR u.id=h.sender_user_id)
  AND sentiment IN ('+',
                    '-')
  AND h.created>'{0}'
  AND h.created<'{1}'
GROUP BY userid,
         TYPE,
         sentiment,
         context