GROUP BY不起作用?

时间:2018-04-16 11:09:31

标签: sorting group-by jinja2 flask-sqlalchemy

我有一个返回记录但不是GROUP的查询。 我已经尝试了几个SELECT BY和GROUP选项,没有工作??

这会为每个s.surname返回多条记录?

sql_staff = """SELECT s.surname, s.forename, s.status, s.staff_id, 
    s.Staff_status, 
    g.id,          
    g.cluster_id as g_cluster_id, 
    gc.cluster_id as gc_cluster_id        
    FROM staff s INNER JOIN local_grant_investigators lgi
    ON s.staff_id = lgi.staff_id

    INNER JOIN grants g
    ON g.id = lgi.grant_id        

    INNER JOIN grant_clusters gc
    ON g.id=gc.grant_id
    WHERE s.Staff_status=1
    GROUP BY s.surname, s.forename, s.status, s.staff_id, s.Staff_status,
    g.id, g.cluster_id, 
    gc.cluster_id HAVING s.Staff_status=1
    ORDER BY s.surname, s.forename;    
"""
r_s = list(conn.execute(text(sql_staff)))

下面这段代码会抛出一个错误??

sql_staff = """SELECT s.surname, s.forename, s.status, s.staff_id, 
    s.Staff_status,
    g.id,          
    g.cluster_id as g_cluster_id, 
    gc.cluster_id as gc_cluster_id        
    FROM staff s INNER JOIN local_grant_investigators lgi
    ON s.staff_id = lgi.staff_id 
    GROUP BY s.surname, s.forename, lgi.staff_id        

    INNER JOIN grants g
    ON g.id = lgi.grant_id        

    INNER JOIN grant_clusters gc
    ON g.id=gc.grant_id
    WHERE s.Staff_status=1
    GROUP BY s.surname, s.forename, s.status, s.staff_id, s.Staff_status,
    g.id, g.cluster_id, 
    gc.cluster_id;    
"""

sqlalchemy.exc.OperationalError:(pymssql.OperationalError)(156,b“关键字'INNER'.DB-Lib错误消息20018附近的语法错误,严重级15:\ n一般SQL Server错误:检查来自SQL Server的消息\ n“)[SQL:'SELECT DISTINCT s.surname,s.forename,s.status,s.staff_id,s.Staff_status,\ n g.id,\ n g.cluster_id as g_cluster_id,\ n gc.cluster_id as gc_cluster_id \ n FROM工作人员INNER JOIN local_grant_investigators lgi \ n ON s.staff_id = lgi.staff_id \ n \ t \ tGROUP by s.surname,s.forename,lgi.staff_id \ t \ t \ n \ n INNER JOIN授予g \ n ON g.id = lgi.grant_id \ n INNER JOIN grant_clusters gc \ n ON g.id = gc.grant_id \ n WHERE s.Staff_status = 1 \ n GROUP BY s.surname,s.forename,s.status, s.staff_id,s.Staff_status,\ n g.id,g.cluster_id,\ n gc.cluster_id; \ n']

我将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的小组:

<input id="date1" size="60" type="date" format="MM/DD/YYYY" placeholder="MM/DD/YYYY" />

包含多个项目,因此每个姓氏可能有多个条目。因此,如果你的小组是: GROUP BY s.surname, s.forename, s.status, s.staff_id, s.Staff_status, g.id, g.cluster_id, gc.cluster_id

然后每个姓氏将获得一个条目。但是我不确定这正是你要找的。你的问题不清楚。你想要每个姓氏一个条目或是错误信息,你想澄清的问题是什么?