如何将联合查询与最大查询组合?

时间:2016-02-22 19:27:29

标签: sql ms-access

首先,我的数据来自以下表格:

  • tblProjects(字段ProjectID(密钥)和ProjectTitle
  • tblDocuments(字段ProjectID(已关联),DocID(密钥)和DocTitle
  • tblRevisions(字段DocID(已关联),SubDateRevReturnDate

我的目标是结合下面提到的 A (UNION查询)和 B (MAX查询)的结果,并使它们协同工作(请参阅 C ):

A)联合查询:

SQL代码:

    SELECT ProjectID,0 as [0/DocID],ProjectTitle
    FROM tblProjects

    UNION ALL SELECT ProjectID,DocID,DocTitle
    FROM tblDocuments

    ORDER BY ProjectID, [0/DocID];

结果:

    ProjectID    | 0/DocID | Project/DocTitle 
    ------------------------------------------
    1            | 0       | Project 1        
    1            | 1       | Document 1.1     
    1            | 2       | Document 1.2
    2            | 0       | Project 2
    2            | 3       | Document 2.1
    2            | 4       | Document 2.2

B)最大查询:

SQL代码:

    SELECT tblDocuments.DocID, Max(tblRevisions.SubDate) AS MaxOfSubDate
    FROM tblDocuments LEFT JOIN tblRevisions ON tblDocuments.DocID = tblRevisions.DocID
    GROUP BY tblDocuments.DocID;

结果:

    DocID    | MaxOfSubDate 
    -----------------------
    1        | 22.02.2016        
    2        | 20.02.2016     
    3        | (blank)
    4        | 21.02.2016

C)结合A和B:

(注意!我并不一定意味着我想将A和B中的代码编译成一个单独的查询。它也可以通过一组不同的查询:即从A和B收集数据的第三个查询)。

SQL代码:

    ? I have absolutely no clue ?

期望的结果:

    ProjectID    | 0/DocID | Project/DocTitle | MaxOfSubDate | Rev  | ReturnDate 
    ---------------------------------------------------------------------------------
    1            | 0       | Project 1        | 0            |      | 
    1            | 1       | Document 1.1     | 12.02.2016   |  03  | 22.02.2016
    1            | 2       | Document 1.2     | 10.02.2016   |  04  | 15.02.2016
    2            | 0       | Project 2        | 0            |      |
    2            | 3       | Document 2.1     | (blank)      | (bl) | (blank)
    2            | 4       | Document 2.2     | 21.02.2016   |  02  | (blank)
  • 注意:上面列出的字段RevClientRev来自tblRevisions。我必须以某种方式将tblRevisions中的这些(以及更多)字段添加到混合中。在其中一个查询中? /或许在查询B中某种程度?

2 个答案:

答案 0 :(得分:1)

完全按照你说的做。创建第一个查询A和第二个查询B,然后创建第三个查询C,它将加入两个结果。

SELECT A.*, B.MaxOfSubDate, B.MaxOfOurRev, B.MaxOfClientRev
FROM A
LEFT OUTER JOIN B On A.[0/DocID] = B.DocID

答案 1 :(得分:0)

创建一个新查询(B2),要求tblRevisions'关联数据以查询B的maxOfSubDate。 查询B2:“qryLatestRevDetailsAll”(使用设计视图创建):

SELECT tblDocuments.DocID, tblDocuments.DocTitle, tblDocuments.OurDocNo, tblDocuments.ClientDocNo, tblRevisions.OurRev, tblRevisions.ClientRev, tblRevisions.SubDate, tblRevisions.SubCode, tblRevisions.RetDate, tblRevisions.RetCode, tblRevisions.Comments, tblRevisions.Field1
FROM tblProjects 
INNER JOIN ((qryMAXSubDate 
  INNER JOIN tblDocuments ON qryMAXSubDate.DocID = tblDocuments.DocID) 
  INNER JOIN tblRevisions ON (qryMAXSubDate.MaxOfSubDate = tblRevisions.SubDate) 
  AND (tblDocuments.DocID = tblRevisions.DocID)) ON tblProjects.ProjectID = tblDocuments.ProjectID;

步骤2:将联合查询(A)与B2结合:

 SELECT quniProjectsDocuments.ProjectID, quniProjectsDocuments.[0/DocID], quniProjectsDocuments.ProjectTitle, quniProjectsDocuments.OurDocNo, quniProjectsDocuments.ClientDocNo, [qryLatestRevDetailsAll].OurRev, [qryLatestRevDetailsAll].SubDate, [qryLatestRevDetailsAll].RetDate
 FROM quniProjectsDocuments 
 LEFT JOIN qryLatestRevDetailsAll ON quniProjectsDocuments.[0/DocID]=[qryLatestRevDetailsAll].DocID
 ORDER BY quniProjectsDocuments.ProjectID, quniProjectsDocuments.[0/DocID];

非常感谢@ randyh22指出我正确的方向!