Microsoft Access行合并

时间:2014-05-26 18:11:31

标签: sql merge ms-access-2010

我希望通过查询在Microsoft Access上合并数据方面获得一些帮助。目前,我收到一个包含许多记录的数据转储。大多数这些记录是针对相同的工作,即相同的工作ID。我想把它们组合成一排。这可能吗?

输入:

jobid   jobdescription  processid   processdescription  processcreateddate
1234    Compliance  124               Accept              03/04/2014 10:32
1234    Compliance  135                Hold           03/07/2014 14:04
1234    Compliance  164                Review             03/04/2014 10:33
1234    Compliance  178               Complete             03/11/2014 8:21

输出:

jobid   jobdescription processdescription processcreateddate processdesc2  Proccreatedate2
1234    Compliance      Accept             03/04/2014 10:32    Hold       03/07/2014 14:04

等...

谢谢,

瑞克

2 个答案:

答案 0 :(得分:0)

从我所看到的情况看,每个流程描述看起来应该是数据库中的字段名称。

每个流程描述都应该包含一个日期。

职位ID - 职位描述 - AcceptDate - HoldDate - ReviewDate - CompDate

1234 ----符合------ ----- 14年1月10日14年1月11日14年1月12日--- ----- 15年1月1日

答案 1 :(得分:0)

基本上,您需要一个将行转换为列的查询,或者从长到宽的重塑。

考虑以下查询,其中子查询和派生表嵌套在聚合查询中。内部查询计算每jobid个进程的数量,然后使用计算的计数按jobidjobdescription有条件地聚合外部查询:

SELECT jobid, jobdescription,    
       Max(IIF(processcount=1, pdesc, NULL)) As processdesc1,
       Max(IIF(processcount=1, pdate, NULL)) As prcoesscreateddate1,
       Max(IIF(processcount=2, pdesc, NULL)) As processdesc2,
       Max(IIF(processcount=2, pdate, NULL)) As processcreateddate2,
       Max(IIF(processcount=3, pdesc, NULL)) As processdesc3,
       Max(IIF(processcount=3, pdate, NULL)) As processcreateddate3,
       Max(IIF(processcount=4, pdesc, NULL)) As processdesc4,
       Max(IIF(processcount=4, pdate, NULL)) As processcreateddate4

FROM
       (SELECT t1.jobid, t1.jobdescription, t1.processid As pid,
               t1.processdescription As pdesc, t1.processcreatedate As pdate,
               (SELECT Count(*) FROM Processes t2 
                WHERE t1.jobid=t2.jobid AND t1.processcreatedate >= t2.processcreatedate) 
                As processcount
        FROM processes_dump t1) As derivedTable

GROUP BY t1.jobid, jobdescription;

<强>输出

jobid   jobdescription  processdesc1    prcoesscreateddate1   processdesc2  processcreateddate2  processdesc3   processcreateddate3 processdesc4    processcreateddate4
1234       Compliance        Access     3/4/2014 10:32:00 AM       Review   3/4/2014 10:33:00 AM        Hold    3/7/2014 2:04:00 PM     Complete    3/11/2014 8:21:00 AM