Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python在双for循环中遍历CTE?_Python_Postgresql_Flask_Sqlalchemy_Common Table Expression - Fatal编程技术网

python在双for循环中遍历CTE?

python在双for循环中遍历CTE?,python,postgresql,flask,sqlalchemy,common-table-expression,Python,Postgresql,Flask,Sqlalchemy,Common Table Expression,我有两个for循环。对于loop1中的每一行“A”、“B”、“C”,我需要访问层次结构树以查找loop2中组“X”的所有父级。这使我使用CTE,其中我需要分别找到每一行的路径。在循环中使用CTE并不是一个解决方案,我无法确定在哪里可以匹配每个组id。参考了此链接,但无法了解太多 使用flask framework的cron作业的代码段: s = select([rt_issues]).\ where( and_( rt_issues.c.s

我有两个for循环。对于loop1中的每一行“A”、“B”、“C”,我需要访问层次结构树以查找loop2中组“X”的所有父级。这使我使用CTE,其中我需要分别找到每一行的路径。在循环中使用CTE并不是一个解决方案,我无法确定在哪里可以匹配每个组id。参考了此链接,但无法了解太多

使用flask framework的cron作业的代码段:

    s = select([rt_issues]).\
    where(
        and_(
            rt_issues.c.status !='Closed',
            rt_issues.c.assigned_to != None
        ))
rs = conn.execute(s)
if rs.rowcount > 0:
    s4 = text('with recursive rec_grp as(select id, parent_id, name, head, 1 as level, array[id] as path_info from groups union all select grp1.id, grp1.parent_id, grp1.name, grp1.head, rc.level + 1, rc.path_info||grp1.id from groups grp1 join rec_grp rc on grp1.id = rc.parent_id) select distinct id, parent_id, name, head, path_info from rec_grp order by id')

    rs4 = conn.execute(s4)

    for r in rs:
        head_list = []
        hierarchical_grps = []
        for rr in rs4:
            if ((rr['path_info'][0] == r[rt_issues.c.assignee_group])):
                for g in rr['path_info']:
                    hierarchical_grps.append(g)
        hierarchical_grps = list(set(hierarchical_grps))            
        send_pending_mail(hierarchical_grps, r['id'])
        print hierarchical_grps, 'hierarchical_grps'

    exit(0)
我需要将邮件发送给问题层次结构中受让人小组的所有小组负责人。如何才能做到这一点。如何正确使用循环?我只使用sqlalchemy核心、postgresql、python和flask。我需要同样的代码

下面的代码片段有效:

 mgroup = None
 s = select([rt_issues]).\
     where(
         and_(
             rt_issues.c.status !='Closed',
             rt_issues.c.assigned_to != None
         ))
 rs = conn.execute(s)
 if rs.rowcount > 0:
     for r in rs:
         head_list = []
         hierarchical_grps = []
         mgroup = r[rt_issues.c.assignee_group]
         s4 = text('with recursive rec_grp as(select id, parent_id, name, head, 1 as level, array[id] as path_info from groups where id=' +str(mgroup) + 'union all select grp1.id, grp1.parent_id, grp1.name, grp1.head, rc.level + 1, rc.path_info||grp1.id from groupsgrp1 join rec_grp rc on grp1.id = rc.parent_id) select distinct id,parent_id, name, head, path_info from rec_grp order by id')

     rs4 = conn.execute(s4)
     for rr in rs4:
         if ((rr['path_info'][0] == r[rt_issues.c.assignee_group])):
             for g in rr['path_info']:
                 hierarchical_grps.append(g)
     hierarchical_grps = list(set(hierarchical_grps))
     print hierarchical_grps, 'hierarchical_grps'
     send_pending_mail(hierarchical_grps, r['id'])
 exit(0)

假设
head
列为布尔值,这将收集设置了
head
标志的组:

rs4 = con.execute(s4)
for rr in rs4:
    if rr['head']:
        head_list.append(rr['id'])

print 'group heads:', head_list
这是假设使用了第二个示例中的查询(注意对from子句的更正,“from groupsgrp1”应该是“from grp1”):


你能展示一下桌子的结构吗?在此处或中,您仅使用了特定id。我想要的是id列表。这将列出问题组中设置了
head
标志的所有父组。您是需要多个问题的列表,还是需要单个问题的列表?对于多个问题。是否有必要针对每个匹配问题始终执行CTE-这是可以避免的。我想知道如我所述执行对性能的影响。我想问的是,有没有更好的替代方案?你是否关心问题的数量,或群体的数量?最简单的方法是一次循环一个问题,并每次将层次结构扁平化。我不会担心性能,直到它成为一个问题(等等)。我怀疑你的邮件流程将是瓶颈,而不是邮件列表的解析。
WITH RECURSIVE rec_grp AS (
  SELECT
    id,
    parent_id,
    name,
    head,
    1          AS level,
    ARRAY [id] AS path_info
  FROM groups
  WHERE id = 4
  UNION ALL
  SELECT
    grp1.id,
    grp1.parent_id,
    grp1.name,
    grp1.head,
    rc.level + 1,
    rc.path_info || grp1.id
  FROM groups grp1
    JOIN rec_grp rc ON grp1.id = rc.parent_id
)
SELECT DISTINCT
  id,
  parent_id,
  name,
  head,
  path_info
FROM rec_grp
ORDER BY id;