多个JOIN查询 - MS Access

时间:2012-10-18 12:09:17

标签: ms-access join

请光临我,因为这有点复杂,我将尽力解释它尽可能简单。表结构描述仅供参考,列名与DB相比有所不同

我有一个不寻常的数据库。我有代理表(代理商)具有以下结构..

A_ID(PK)|    AgentName    |    Team
------------------------------------
1       |    Abi          |    New
2       |    Carl         |    New
3       |    James        |    New

我有两个其他表有几个字段,但有问题的字段有......

表:交易

ID(PK)  |    manyColumns  |    Agent_ID    |    OUTCOME
--------------------------------------------------------
96      |    ...          |    1           |    9          
98      |    ....         |    2           |    8       
99      |    .....        |    3           |    1       

表: RAWDATA

ID(PK)  |    manyColumns  |    Agent_ID    |    BANK_OUTCOME
-------------------------------------------------------------
1234    |    ...          |    1           |    3
1114    |    ....         |    2           |    333
1745    |    .....        |    3           |    123

我有一张日志表,其中包含员工工作时间的详细信息..

表: AGENT_ATTENDANCE

ATT_ID  |    Hours Worked |    Agent_ID_FK 
-------------------------------------------
96      |    7.5          |    1            
98      |    7.5          |    2             
99      |    7.5          |    3           

我还有员工薪酬的详细信息,合同时间......

表: AGENT_DETAILS

DT_ID   |    Hours WK      |    Pay    |    Agent_ID_FK 
-------------------------------------------------------
96      |    37.5          |    15600  |    1            
98      |    37.5          |    15600  |    2             
99      |    37.5          |    15600  |    3           

我创建了单独的查询,但我希望显示为一个..因为计算依赖于彼此..我不知道该怎么做..以下是单独的查询..如何将它们作为一个加入??

查询 - 1:

PARAMETERS [EnterStartDate:] DateTime, [EnterEndDate:] DateTime;

SELECT Agents.[Agent Name], 
Sum(IIf((([TRANSACTIONS].[BankOutcome]=3) Or ([TRANSACTIONS].[BankOutcome]=333)),1,0)) AS PositiveOutcome, 
Count(RAWDATA.Outcome) AS TotalRecordsUsed

FROM (RAWDATA LEFT JOIN Agents ON RAWDATA.[AGENT_ID] = Agents.A_ID) LEFT JOIN
TRANSACTIONS ON RAWDATA.ID = TRANSACTIONS.RAW_ID 

WHERE (((RAWDATA.DialedDate) Between [EnterStartDate:] And [EnterEndDate:]))

GROUP BY Agents.[Agent Name], RAWDATA.AGENT_ID

HAVING (((RAWDATA.AGENT_ID) Is Not Null));

查询 - 2:

PARAMETERS [EnterStartDate:] DateTime, [EnterEndDate:] DateTime;

SELECT Agents.[Agent Name], AGENT_DETAILS.WK_TARGET_HOURS,
Sum(AGENT_ATTENDENCE.HoursWorked_Day) AS [Actual Hours Worked], 
[WK_TARGET_HOURS]-[Actual Hours Worked] AS [Hours Lost], AGENT_DETAILS.SALARY, 
Round([SALARY]/252/([WK_TARGET_HOURS]/5),2) AS [Hourly Rate], [Hourly Rate]*[Actual Hours Worked] AS [Staff Cost]

FROM (Agents INNER JOIN AGENT_DETAILS ON Agents.A_ID = AGENT_DETAILS.AGENT_ID_fk)
INNER JOIN AGENT_ATTENDENCE ON Agents.A_ID = AGENT_ATTENDENCE.AGENT_ID_fk

WHERE (((AGENT_ATTENDENCE.WorK_Date) Between [EnterStartDate:] And [EnterEndDate:]))

GROUP BY Agents.[Agent Name], AGENT_DETAILS.WK_TARGET_HOURS, AGENT_DETAILS.SALARY, 
Agents.Active, Agents.Team

HAVING (((Agents.Active)=True) AND ((Agents.Team)<>"Manager" And (Agents.Team)<>"Cust. Ser."));

如果可以,可以附加文件,但我不知道如何...

1 个答案:

答案 0 :(得分:0)

我没有进入你的代码,但大致你有3个选择:

  • 如果两个查询都有相同的字段(显然不是你的情况),你可以使用UNION查询将两个查询的数据都放在1​​个数据集中。
  • 否则,您可以使用query1作为query2的来源
  • 最后,您可以构建query1和query2,然后使用第三个查询来加入前2个