使用内部联接作为联合查询的查询条件

时间:2017-10-12 20:33:51

标签: ms-access ms-access-2010

我有跟踪员工质量保证数据的表格。它跟踪Employee(UserLogin)员工工作日期(NoteDate)和5个是/否字段(以及用于此查询的其他不相关字段以及与以下代码匹配的更多趋势)。一个单独的表跟踪员工数据,包括他们所在的单位(单位)。我有一个Union Query,它绘制了我注意到的字段,目的是创建一个总结精度百分比的交叉表。我现在希望能够使用相同的查询来跟踪一个团队的表现。我尝试使用UserLogin字段对employee表进行内部联接,该字段在两者中都显示为连接。然后,我要求查询使用我在控件中指定的团队来仅查询这些团队成员并汇总他们的数据。这是第一个适用于所有员工的查询。

SELECT            ID AS SourceID
                , UserLogin
                , Year([NoteDate]) AS Yr
                , Format([NoteDate], "mmm") AS Mo
                , "Appeal" AS Trend 
FROM              tblQA 
WHERE             Appeal=False

UNION SELECT      ID
                , UserLogin
                , Year([NoteDate])
                , Format([NoteDate], "mmm")
                , "NRP" 
FROM tblQA 
WHERE             NRP = False

UNION SELECT      ID
                , UserLogin
                , Year([NoteDate])
                , Format([NoteDate], "mmm")
                , "ChurnEscalation" 
FROM              tblQA 
WHERE             ChurnEscalation = False

UNION SELECT      ID
                , UserLogin
                , Year([NoteDate])
                , Format([NoteDate], "mmm")
                , "ProtocolNotFollowed" 
FROM              tblQA 
WHERE             ProtocolNotFollowed = False

UNION SELECT      ID
                , UserLogin
                , Year([NoteDate])
                , Format([NoteDate], "mmm")
                , "Resubmission" 
FROM              tblQA 
WHERE             Resubmission = False

然后我在这里尝试新代码的前几行。

SELECT tblQA.ID AS SourceID, tblQA.UserLogin, Year([NoteDate]) AS Yr, Format([NoteDate], "mmm") AS Mo, "Appeal" AS Trend FROM tblQA  INNER JOIN tblUser ON tblQA.UserLogin = tblUser.UserLogin WHERE Appeal=FALSE AND (((tblUser.Unit)=[Forms]![MainMenu]![btnManagersMenu].[Form]![txtADUnitPT]))
UNION SELECT tblQA.ID AS SourceID, tblQA.UserLogin, Year([NoteDate]) AS Yr, Format([NoteDate], "mmm") AS Mo, "NRP" AS Trend FROM tblQA  INNER JOIN tblUser ON tblQA.UserLogin = tblUser.UserLogin WHERE Appeal=FALSE AND (((tblUser.Unit)=[Forms]![MainMenu]![btnManagersMenu].[Form]![txtADUnitPT]))
UNION SELECT tblQA.ID AS SourceID, tblQA.UserLogin, Year([NoteDate]) AS Yr, Format([NoteDate], "mmm") AS Mo, "ChurnEscalation" AS Trend FROM tblQA  INNER JOIN tblUser ON tblQA.UserLogin = tblUser.UserLogin WHERE Appeal=FALSE AND (((tblUser.Unit)=[Forms]![MainMenu]![btnManagersMenu].[Form]![txtADUnitPT]))
UNION SELECT tblQA.ID AS SourceID, tblQA.UserLogin, Year([NoteDate]) AS Yr, Format([NoteDate], "mmm") AS Mo, "ProtocolNotFollowed" AS Trend FROM tblQA  INNER JOIN tblUser ON tblQA.UserLogin = tblUser.UserLogin WHERE Appeal=FALSE AND (((tblUser.Unit)=[Forms]![MainMenu]![btnManagersMenu].[Form]![txtADUnitPT]))
UNION SELECT tblQA.ID AS SourceID, tblQA.UserLogin, Year([NoteDate]) AS Yr, Format([NoteDate], "mmm") AS Mo, "Resubmission" AS Trend FROM tblQA  INNER JOIN tblUser ON tblQA.UserLogin = tblUser.UserLogin WHERE Appeal=FALSE AND (((tblUser.Unit)=[Forms]![MainMenu]![btnManagersMenu].[Form]![txtADUnitPT]))

我没有获得看似有效的结果 - 趋势的准确率为700%,所有趋势的价值相同。谁能看到我做错了什么?

Here is an image of the results I get with all items tracked 编辑 - 好的我取得了一些进展,但仍然遇到了一些麻烦。我在后续查询(基于June7和DataUNION代码的查询)中意识到,我正在从所有员工而不仅仅是单位绘制我的总QA计数,因此我更改了该代码以添加单位标准。

SELECT    DataUNIONUnitAD.Yr
        , DataUNIONUnitAD.Mo
        , DataUNIONUnitAD.Trend
        , Count(DataUNIONUnitAD.UserLogin) AS CountOfUserLogin
        , ctqADUnitTrends.CntYrMo
        , Count([UserLogin])/[CntYrMo]*100 AS Pct 
FROM    (
         SELECT       Year([NoteDate]) AS Yr
                    , Format([NoteDate],"mmm") AS Mo
                    , Count(tblQA.ID) AS CntYrMo 
         FROM         tblQA 
         WHERE        (((tblUser.Unit)=[Forms]![MainMenu]![btnManagersMenu].[Form]![txtADUnitPT])) 
         GROUP BY     Year([NoteDate])
                    , Format([NoteDate],"mmm")
        ) AS ctqADUnitTrends INNER JOIN DataUNIONUnitAD ON 
            (ctqADUnitTrends.Mo = DataUNIONUnitAD.Mo) AND (ctqADUnitTrends.Yr = DataUNIONUnitAD.Yr) 
GROUP BY  DataUNIONUnitAD.Yr
        , DataUNIONUnitAD.Mo
        , DataUNIONUnitAD.Trend
        , ctqADUnitTrends.CntYrMo;

这给了我正确的数量级百分比,但所有趋势仍然相同。 Image我试图查看是否可以确定我可能无意中改变了某些内容,但我无法弄明白。

1 个答案:

答案 0 :(得分:0)

或者,不要更改UNION查询,调整后续查询(顺便说一句,注意查询名称中的更改):

UNION查询:
SELECT ID AS SourceID, tblQA.UserLogin, Year([NoteDate]) AS Yr, Format([NoteDate], "mmm") AS Mo, "Appeal" AS Trend FROM tblQA WHERE Appeal=True UNION SELECT ID, tblQA.UserLogin, Year([NoteDate]) AS Yr, Format([NoteDate], "mmm") AS Mo, "NRP" FROM tblQA WHERE NRP=True UNION SELECT ID, tblQA.UserLogin, Year([NoteDate]) AS Yr, Format([NoteDate], "mmm") AS Mo, "Churn" FROM tblQA WHERE ChurnEscalation=True UNION SELECT ID, tblQA.UserLogin, Year([NoteDate]) AS Yr, Format([NoteDate], "mmm") AS Mo, "Protocol" FROM tblQA WHERE ProtocolNotFollowed=True UNION SELECT ID, tblQA.UserLogin, Year([NoteDate]) AS Yr, Format([NoteDate], "mmm") AS Mo, "Resub" FROM tblQA WHERE Resubmission=True;

汇总查询:
SELECT QA_UNION.Yr, QA_UNION.Mo, QA_UNION.Trend, QA_UNION.UserLogin, tblUser.Unit, Query3.CntYrMo, Count(*) AS CountOfUser, Count(*)/[CntYrMo]*100 AS Pct FROM tblUser INNER JOIN ((SELECT Year([NoteDate]) AS Yr, Format([NoteDate],"mmm") AS Mo, Count([tblQA].ID) AS CntYrMo FROM tblQA GROUP BY Year([NoteDate]), Format([NoteDate],"mmm")) AS Query3 INNER JOIN QA_UNION ON (Query3.Mo = QA_UNION.Mo) AND (Query3.Yr = QA_UNION.Yr)) ON tblUser.UserLogin = QA_UNION.UserLogin GROUP BY QA_UNION.Yr, QA_UNION.Mo, QA_UNION.Trend, QA_UNION.UserLogin, tblUser.Unit, Query3.CntYrMo;

然后是CROSSTAB:
PARAMETERS [Forms]![MainMenu]![btnManagersMenu].[Form]![txtADUnitPT] Text ( 255 ); TRANSFORM First(QA_COUNT.Pct) AS FirstOfPct SELECT QA_COUNT.Yr, QA_COUNT.Trend, QA_COUNT.Unit FROM QA_COUNT WHERE (((QA_COUNT.Unit)=[Forms]![MainMenu]![btnManagersMenu].[Form]![txtADUnitPT])) GROUP BY QA_COUNT.Yr, QA_COUNT.Trend, QA_COUNT.Unit PIVOT QA_COUNT.Mo In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");