0

我有一个包含几个表的数据库:[Plan Revenue]、[Plan Revenue Expense]、[Support Provider]、[Z Service Codes New]

我已经编写了一个查询,可以让我计算在过去 6 个月的特定服务代码中为每个提供商列出了多少费用记录。唯一的问题是,理想情况下,我希望它还列出过去 6 个月内未记录任何费用的服务代码的 0。

因此,基本上,我想将 [Z Service Codes New] 表与 [Support Providers] 表交叉,以获得提供者和所有服务代码之间的所有可能匹配。然后,在每个服务代码中,我希望它计算过去 6 个月内的费用记录数(嗯,我实际上已经设置了自定义日期,所以它更灵活)。

但是我一直坚持如何做到这一点,因此它不会将所有费用记录与所有服务代码匹配,而是仅将那些属于正确服务代码的费用记录(如果有)匹配。

[Plan Revenue].[Service] 是与 [Z Service Codes New] 匹配的外键。[Service Code] [Plan Revenue].[Rev ID] 是与 [Plan Revenue Expense] 匹配的外键。 [RevID] [Plan Revenue Expense].[SP] 是与 [Support Provider].[ID] 匹配的外键

不要问我为什么为了简单起见,数据库构建器没有让他们的键从一个表匹配到下一个表。就是这样……

我当前的查询看起来像这样:


SELECT [support provider].[sp last],
       [support provider].[sp first],
       [support provider].[sp mi],
       [support provider].[agency],
       [support provider].[de?],
       [support provider].[ic?],
       [support provider].[agency?]     AS [AG?],
       [support provider].[generalbus?] AS [GB?],
       [ExpsToCount].[counted svc],
       Iif(Count([ExpsToCount].[exptocountid]) = 0, 0, Count(
       [ExpsToCount].[exptocountid]))   AS ExpCount
FROM   [support provider]
       LEFT JOIN (SELECT [plan revenue expense].[sp]          AS [Counted SP],
                         [z service codes new].[service code] AS [Counted Svc],
                         [plan revenue expense].[exp id]      AS [ExpToCountID]
                  FROM   [z service codes new]
                         LEFT JOIN ([plan revenue]
                                    LEFT JOIN [plan revenue expense]
                                           ON [plan revenue].[rev id] =
                                              [plan revenue expense].[revid])
                                ON [z service codes new].[service code] =
                                   [plan revenue].[service]
                  WHERE  ( [plan revenue expense].[checkdt] >=
                           Format([period beginning "mm/dd/yyyy"], "short date")
                           AND [plan revenue expense].[checkdt] <=
                 Format([periond ending "mm/dd/yyyy"], "short date") ))
                             AS ExpsToCount
              ON [ExpsToCount].[counted sp] = [support provider].[id]
WHERE  [support provider].[inactive?] = false
       AND ( [support provider].[de?] = true
              OR [support provider].[ic?] = true )
GROUP  BY [support provider].[sp last],
          [support provider].[sp first],
          [support provider].[sp mi],
          [support provider].[agency],
          [support provider].[de?],
          [support provider].[ic?],
          [support provider].[agency?],
          [support provider].[generalbus?],
          [ExpsToCount].[counted svc]
ORDER  BY [ExpsToCount].[counted svc],
          [support provider].[agency],
          [support provider].[sp last],
          [support provider].[sp first],
          [support provider].[sp mi],
          [ExpsToCount].[counted svc]; 

[IC?], [DE?], [Agency?], [GeneralBus?]只是表示支持提供者类型的布尔字段。同样,为什么数据库人员选择这个而不是带有外键的单个字段,所以你只能是一种类型的提供者,我不知道......只是滚动它。

所以,我目前也在使用子查询。

但是,它并没有给我没有费用相关的服务代码零。它只是给我计算实际费用,如果我不关心 0 就好了,我有点这样做......如果只是为了我自己的教化。

我觉得我在最后一天左右一直在玩这个东西,我的大脑是朋友。需要第二双眼睛。

我觉得我需要将 [Support Provider] 与 [Z Service Codes New] 交叉加入,以便获得所有可能的匹配项,然后以某种方式选择性地给出 0(如果没有相关的费用记录)或 COUNT(如果有费用)记录。但无论如何,我都希望每个支持提供商都有一个服务代码。

不确定在 Access 中将 CROSS JOIN 与某种 INNER JOIN 或 OUTER JOIN 组合的语法是什么?对于我的目的来说,最好的组合是什么。我可以在没有子查询的情况下以某种方式逃脱,还是我仍然需要它?与 CROSS JOIN 相比,有没有更好的方法来做到这一点?(我认为这就是 CROSS JOIN 的设计目的......但我可能错了吗?)

希望一切都说得通。想法?哦,我正在使用 Access 2003 Professional SP3。


编辑:

好的,我自己的优雅解决方案(好吧,好吧,我不知道它是否优雅,但它有效!)是在我的主 FROM 行中创建两个自定义 SELECT 语句,然后根据两个匹配条件加入两个 SELECT 语句:


SELECT [ProvidersXCodes].[SP Last], [ProvidersXCodes].[SP First], [ProvidersXCodes].[SP MI], [ProvidersXCodes].[Agency], [ProvidersXCodes].[SvcCode], IIf(COUNT([ExpsToCount].[ExpCountID])=0,0,COUNT([ExpsToCount].[ExpCountID])) AS [Exp Count]

FROM (SELECT [Support Provider].[ID] AS [CodeProvider], [Support Provider].[SP Last], [Support Provider].[SP First], [Support Provider].[SP MI], [Support Provider].[Agency], [Z Service Codes New].[Service Code] AS [SvcCode]

FROM [Support Provider], [Z Service Codes New]

WHERE [Support Provider].[Inactive?]=FALSE
AND ([Support Provider].[DE?]=TRUE OR [Support Provider].[IC?]=TRUE)
AND NOT ([Z Service Codes New].[Service Code] LIKE '111*'
OR [Z Service Codes New].[Service Code] LIKE '222*'
OR [Z Service Codes New].[Service Code] LIKE '333*')

GROUP BY [Support Provider].[ID], [Support Provider].[SP Last], [Support Provider].[SP First], [Support Provider].[SP MI], [Support Provider].[Agency], [Z Service Codes New].[Service Code]) AS [ProvidersXCodes] 

LEFT JOIN (SELECT [Support Provider].[ID] AS [ExpCountProv], [Plan Revenue].[Service] AS [ExpCountSvc], [Plan Revenue Expense].[Exp ID] AS [ExpCountID]

FROM [Plan Revenue], [Plan Revenue Expense], [Support Provider]

WHERE [Plan Revenue].[Rev ID]=[Plan Revenue Expense].[RevID]
AND [Plan Revenue Expense].[SP]=[Support Provider].[ID]
AND [Support Provider].[Inactive?]=FALSE
AND ([Support Provider].[DE?]=TRUE OR [Support Provider].[IC?]=TRUE)
AND ([Plan Revenue Expense].[CheckDt]>=Format([Period Beginning "MM/DD/YYYY"],"Short Date") AND [Plan Revenue Expense].[CheckDt]<=Format([Periond Ending "MM/DD/YYYY"],"Short Date"))

GROUP BY [Support Provider].[ID], [Plan Revenue].[Service], [Plan Revenue Expense].[Exp ID]) AS [ExpsToCount] ON ([ProvidersXCodes].[CodeProvider]=[ExpsToCount].[ExpCountProv] AND [ProvidersXCodes].[SvcCode]=[ExpsToCount].[ExpCountSvc])

GROUP BY [ProvidersXCodes].[SP Last], [ProvidersXCodes].[SP First], [ProvidersXCodes].[SP MI], [ProvidersXCodes].[Agency], [ProvidersXCodes].[SvcCode]

ORDER BY [ProvidersXCodes].[SvcCode], [ProvidersXCodes].[Agency], [ProvidersXCodes].[SP Last], [ProvidersXCodes].[SP First], [ProvidersXCodes].[SP MI];

问题解决了!

我认为必须用语言表达问题有助于解决问题,因为我让自己考虑实际问题是什么,哪些特定信息需要成为交叉产品,哪些信息需要更标准的连接操作以及如何正确链接这两组...

〜MG(我)

4

1 回答 1

1

好吧,我自己看似优雅的解决方案(好吧,好吧,我不知道它是否优雅,但它似乎有效!)是在我的主 FROM 子句中创建两个自定义 SELECT 语句,然后加入两个 SELECT 语句基于两个匹配标准:


SELECT [ProvidersXCodes].[SP Last], [ProvidersXCodes].[SP First], [ProvidersXCodes].[SP MI], [ProvidersXCodes].[Agency], [ProvidersXCodes].[SvcCode], IIf(COUNT([ExpsToCount].[ExpCountID])=0,0,COUNT([ExpsToCount].[ExpCountID])) AS [Exp Count]

FROM (SELECT [Support Provider].[ID] AS [CodeProvider], [Support Provider].[SP Last], [Support Provider].[SP First], [Support Provider].[SP MI], [Support Provider].[Agency], [Z Service Codes New].[Service Code] AS [SvcCode]

FROM [Support Provider], [Z Service Codes New]

WHERE [Support Provider].[Inactive?]=FALSE
AND ([Support Provider].[DE?]=TRUE OR [Support Provider].[IC?]=TRUE)
AND NOT ([Z Service Codes New].[Service Code] LIKE '111*'
OR [Z Service Codes New].[Service Code] LIKE '222*'
OR [Z Service Codes New].[Service Code] LIKE '333*')

GROUP BY [Support Provider].[ID], [Support Provider].[SP Last], [Support Provider].[SP First], [Support Provider].[SP MI], [Support Provider].[Agency], [Z Service Codes New].[Service Code]) AS [ProvidersXCodes] 

LEFT JOIN (SELECT [Support Provider].[ID] AS [ExpCountProv], [Plan Revenue].[Service] AS [ExpCountSvc], [Plan Revenue Expense].[Exp ID] AS [ExpCountID]

FROM [Plan Revenue], [Plan Revenue Expense], [Support Provider]

WHERE [Plan Revenue].[Rev ID]=[Plan Revenue Expense].[RevID]
AND [Plan Revenue Expense].[SP]=[Support Provider].[ID]
AND [Support Provider].[Inactive?]=FALSE
AND ([Support Provider].[DE?]=TRUE OR [Support Provider].[IC?]=TRUE)
AND ([Plan Revenue Expense].[CheckDt]>=Format([Period Beginning "MM/DD/YYYY"],"Short Date") AND [Plan Revenue Expense].[CheckDt]<=Format([Periond Ending "MM/DD/YYYY"],"Short Date"))

GROUP BY [Support Provider].[ID], [Plan Revenue].[Service], [Plan Revenue Expense].[Exp ID]) AS [ExpsToCount] ON ([ProvidersXCodes].[CodeProvider]=[ExpsToCount].[ExpCountProv] AND [ProvidersXCodes].[SvcCode]=[ExpsToCount].[ExpCountSvc])

GROUP BY [ProvidersXCodes].[SP Last], [ProvidersXCodes].[SP First], [ProvidersXCodes].[SP MI], [ProvidersXCodes].[Agency], [ProvidersXCodes].[SvcCode]

ORDER BY [ProvidersXCodes].[SvcCode], [ProvidersXCodes].[Agency], [ProvidersXCodes].[SP Last], [ProvidersXCodes].[SP First], [ProvidersXCodes].[SP MI];

问题解决了!

我认为必须用语言表达问题有助于解决问题,因为我让自己考虑实际问题是什么,哪些特定信息需要成为交叉产品,哪些信息需要更标准的连接操作以及如何正确链接这两组...

〜MG(OP)

于 2012-08-17T06:33:45.717 回答