如何在Access 2010中使用动态子查询?

时间:2014-08-18 22:42:09

标签: sql subquery ms-access-2010

我正在使用的数据样本:

数据记录用户执行知识库搜索时,然后该用户是否在72小时内联系支持。我正在尝试使用这些数据来帮助我确定哪些搜索字词最常导致客户联系(希望能够改进未来的知识库)。

UserName | Search Terms | Event Triggered | Returned Results | Search Date | Search Time | Contact Date | Contact Time | Contact Type
-------------------------------------------------------------------------------------------------------------------------------------
John     | get receipt  |                 | 42               | 1/1/2014    | 00:10       |              |              |
John     | get receipt  |                 | 42               | 1/1/2014    | 00:10       | 1/1/2014     | 01:41        | CHAT
John     | billing      |                 | 87               | 1/1/2014    | 00:12       |              |              |
John     | billing      |                 | 87               | 1/1/2014    | 00:12       | 1/1/2014     | 01:41        | CHAT
Mary     | quickstart   |                 | 24               | 1/1/2014    | 00:31       |              |              |
Mary     | quickstart   |                 | 24               | 1/1/2014    | 00:31       | 1/1/2014     | 02:08        | EMAIL
Mary     | quickstart   |                 | 24               | 1/1/2014    | 00:31       | 1/1/2014     | 03:33        | CHAT
Mary     | quickstart   |                 | 24               | 1/1/2014    | 00:31       | 1/1/2014     | 04:26        | PHONE
约翰搜索了两次,但联系了一次。玛丽搜索了一次,但联系了3次。不幸的是,这使得无法对[Contact Type]列进行简单的计数以获取我正在寻找的信息。此外,Mary的多个联系人更可能是支持失败的结果,而不是知识库的失败。所以,我写了一个关于数据的查询,试图生成一个额外的列,它会给我一个十进制数,以确定每个搜索引起的联系人​​数(每次搜索限制一个联系人)。

我的查询代码(当我尝试保存或运行时崩溃Access 2010):

SELECT
    [Search Events 2014].UserName,
    [Search Events 2014].[Search Terms],
    [Search Events 2014].[Event Triggered],
    [Search Events 2014].[Returned Results],
    [Search Events 2014].[Search Date],
    [Search Events 2014].[Search Time],
    [Search Events 2014].[Contact Date],
    [Search Events 2014].[Contact Time],
    [Search Events 2014].[Contact Type],
    IIf((Count(SELECT [UserName], [Search Terms], [Search Date], [Search Time]
               FROM [Search Events 2014]
               WHERE [UserName] = [Search Events 2014].[UserName]
                 AND [Search Terms] = [Search Events 2014].[Search Terms]
                 AND [Search Date] = [Search Events 2014].[Search Date]
                 AND [Search Time] = [Search Events 2014].[Search Time]
                 AND [Contact Type] <> "") / Count(SELECT [UserName], [Search Terms], [Search Date], [Search Time]
                                                   FROM [Search Events 2014]
                                                   WHERE [UserName] = [Search Events 2014].[UserName]
                                                     AND [Contact Date] = [Search Events 2014].[Search Date]
                                                     AND [Contact Time] = [Search Events 2014].[Search Time])) > 1,
        1,
        (Count(SELECT [UserName], [Search Terms], [Search Date], [Search Time]
               FROM [Search Events 2014]
               WHERE [UserName] = [Search Events 2014].[UserName]
                 AND [Search Terms] = [Search Events 2014].[Search Terms]
                 AND [Search Date] = [Search Events 2014].[Search Date]
                 AND [Search Time] = [Search Events 2014].[Search Time]
                 AND [Contact Type] <> "") / Count(SELECT [UserName], [Search Terms], [Search Date], [Search Time]
                                                   FROM [Search Events 2014]
                                                   WHERE [UserName] = [Search Events 2014].[UserName]
                                                     AND [Contact Date] = [Search Events 2014].[Search Date]
                                                     AND [Contact Time] = [Search Events 2014].[Search Time]))
        ) AS [Contacts Generated]
FROM [Search Events 2014];

理想情况下,查询应该为[Search Events 2014]表(数据最初来自哪里)的每一行运行子查询,以计算新[Contacts Generated]列中我需要的值。我已经对StackOverFlow做了一些研究,似乎Access 2010不喜欢SELECT语句中的子查询,而是希望子查询放在FROM语句中。但是......我认为这需要我为数据库中的每个搜索词创建一个单独的手动查询! (该数据库有超过600,000行和超过85,000个唯一搜索术语。)

如果有更好,更聪明的方法来解决我的困境(例如Visual Basic宏),请告诉我!

0 个答案:

没有答案