嵌套查询可能

时间:2012-09-07 20:11:57

标签: sql postgresql

我有以下工作就像一个魅力。

SELECT d.decisionName, c.firstname, c.lastname, o.name AS organization_name, s.parent_session_id
    FROM tblDecisions d
        INNER JOIN tblSessions s ON s.decision_id = d.decisionid
        INNER JOIN tblCounselors c ON s.counselor_ck = c.campusid
        INNER JOIN tblCounselor_to_organization co ON co.counselor_id = c.counselorid
        INNER JOIN tblOrganizations o ON o.organizationid = co.organization_id
    AND s.start_time >= '2011-01-01 00:00:00'
    AND s.is_complete = TRUE
    ORDER BY s.start_time, s.last_name, s.first_name

字段parent_session_id(整数)可以存储先前记录的主ID,否则默认为0.如果可能,我想要做的是使用嵌套或子查询来执行以下操作: / p>

  1. 检索以上所有内容,但如果其他主要ID被parent_session_id字段中的其他人使用,则删除任何记录。

  2. 如果多个记录在parent_session_id字段(> 0)中引用相同记录,则只能按时间戳(s.start_time DESC LIMIT 1

  3. 获取最新记录

    我有一种感觉,如果不使查询变得非常复杂,这是不可能的,但我的查询技巧并没有比我上面的更深入。

2 个答案:

答案 0 :(得分:2)

  

1。)检索以上所有内容,但如果在parent_session_id字段中另一个人正在使用其主要ID,则删除任何记录。

假设您的主要ID是tblSessions.session_id

SELECT d.decisionName, c.firstname, c.lastname, o.name AS organization_name
                                              , s.parent_session_id
FROM   tblDecisions                 d
JOIN   tblSessions                  s ON s.decision_id = d.decisionid
JOIN   tblCounselors                c ON c.campusid = s.counselor_ck
JOIN   tblCounselor_to_organization co ON co.counselor_id = c.counselorid
JOIN   tblOrganizations             o ON o.organizationid = co.organization_id
AND    s.start_time >= '2011-01-01 00:00:00'
AND    s.is_complete
AND    NOT EXISTS (
    SELECT 1
    FROM   tblSessions s1
    WHERE  s1.parent_session_id = s.session_id
    )
ORDER  BY s.start_time, s.last_name, s.first_name;

你的第二个问题与第一个问题相矛盾。所以,我会留下它:

  

2.如果多个记录在parent_session_id字段(> 0)中引用相同的记录,则只按时间戳(s.start_time DESC > LIMIT 1

获取最新的记录

答案 1 :(得分:1)

我不认为第二个问题与第一个问题相矛盾。首先,实质上,如果此会话是其他任何会话的父级,请不要将其包含在结果中。第二个问题是,如果多个会话具有相同的父级,则仅包括最新的子级。

这是我的解决方案,包含两个子问题:

SELECT d.decisionName, c.firstname, c.lastname, o.name AS organization_name,  s.parent_session_id
, s.start_time, (SELECT max(start_time)
                      FROM tblSessions s2
                      WHERE s2.parent_session_id = s.parent_session_id)
FROM tblDecisions d
    INNER JOIN tblSessions s ON s.decision_id = d.decisionid
    INNER JOIN tblCounselors c ON s.counselor_ck = c.campusid
    INNER JOIN tblCounselor_to_organization co ON co.counselor_id = c.counselorid
    INNER JOIN tblOrganizations o ON o.organizationid = co.organization_id
AND s.start_time >= '2011-01-01 00:00:00'
AND s.is_complete = 1
AND NOT EXISTS (
  SELECT 1
  FROM   tblSessions s1
  WHERE  s1.parent_session_id = s.sessionid
  )
AND (
     (s.parent_session_id IS NULL)
  OR (s.start_time = (SELECT max(start_time)
                      FROM tblSessions s2
                      WHERE s2.parent_session_id = s.parent_session_id))) 

ORDER BY s.start_time, s.last_name, s.first_name

Here is an SQLFiddle demonstrating it.

请注意,我使用的是SQL Server,而不是MySQL。但 我相信解决方案很容易兑换;我认为只有 is_complete = 1 需要更改为 is_complete = true