简体   繁体   English

MS Access Pass Through Query 使用多个表查找重复项

[英]MS Access Pass Through Query find duplicates using multiple tables

I'm trying to find all coverage_set_id with more than one benefit_id attached summary_attribute (value=2004687).我试图找到所有coverage_set_id并附有一个以上的benefit_id附加summary_attribute (值=2004687)。

The query seems to be working fine without the GROUP BY & HAVING parts, but once I add those lines in (for the COUNT) my results are incorrect.在没有 GROUP BY & HAVING 部分的情况下,查询似乎工作正常,但是一旦我将这些行添加到(对于 COUNT)中,我的结果就不正确。 Just trying to get duplicate coverage_set_id .只是想获得重复的coverage_set_id

Pass-Through query via OBDC database:通过 OBDC 数据库传递查询:

SELECT DISTINCT
    b.coverage_set_id,
    COUNT (b.coverage_set_id) AS "COUNT"

FROM
    coverage_set_detail_view a
    JOIN contracts_by_sub_group_view b ON b.coverage_set_id = a.coverage_set_id
    JOIN request c ON c.request_id = b.request_id

WHERE
    b.valid_from_date BETWEEN to_date('10/01/2010','mm/dd/yyyy')
    AND to_date('12/01/2010','mm/dd/yyyy')
    AND c.request_status = 1463
    AND summary_attribute = 2004687
    AND benefit_id <> 1092333

GROUP BY
    b.coverage_set_id

HAVING
    COUNT (b.coverage_set_id) > 1

My results look like this:我的结果是这样的:

-----------------------
COVERAGE_SET_ID | COUNT
-----------------------
4193706         | 8
4197052         | 8
4193926         | 112
4197078         | 96
4174168         | 8

I'm expecting all the COUNTs to be 2.我希望所有的 COUNT 都是 2。


::EDIT:: ::编辑::

Solution:解决方案:

SELECT
c.coverage_set_id AS "COVERAGE SET ID",
c1.description AS "Summary Attribute",
count(d.benefit_id) AS "COUNT"

FROM (
SELECT DISTINCT coverage_set_id

FROM contracts_by_sub_group_view

WHERE
    valid_from_date BETWEEN '01-OCT-2010' AND '01-DEC-2010'
    AND request_id IN (
        SELECT request_id
        FROM  request
        WHERE request_status = 1463)
) a

JOIN coverage_set_master e ON e.coverage_set_id = a.coverage_set_id
JOIN coverage_set_detail c ON c.coverage_set_id = a.coverage_set_id
JOIN benefit_summary d ON d.benefit_id = c.benefit_id
 AND d.coverage_type = e.coverage_type
JOIN codes c1 ON c1.code_id = d.summary_attribute

WHERE
d.summary_attribute IN (2004687, 2004688)
AND summary_structure = 1000217

GROUP BY c.coverage_set_id, c1.description

HAVING COUNT(d.benefit_id) > 1

ORDER BY c.coverage_set_id, c1.description

And these were the results:结果如下:

COVERAGE SET ID  |  SUMMARY ATTRIBUTE  |  COUNT
-------------------------------------------------
4174168          |  INPATIENT          |   2
4174172          |  INPATIENT          |   2
4191828          |  INPATIENT          |   2
4191832          |  INPATIENT          |   2
4191833          |  INPATIENT          |   2
4191834          |  INPATIENT          |   2
4191838          |  INPATIENT          |   2
4191842          |  INPATIENT          |   2
4191843          |  INPATIENT          |   2
4191843          |  OUTPATIENT         |   2
4191844          |  INPATIENT          |   2
4191844          |  OUTPATIENT         |   2

The coverage_set_id in both the HAVING and count part of the SELECT should be benefit_id. SELECT 的 HAVING 和 count 部分中的coverage_set_id 应该是benefit_id。

Since benefit_id is also in table a you can do the following由于benefit_id也在表a中,您可以执行以下操作

SELECT  
    a.coverage_set_id,  
    COUNT (a.benefit_id) AS "COUNT"  

FROM  
    coverage_set_detail_view a  

WHERE  
    a.coverage_set_id in (
        SELECT b.coverage_set_id 
        FROM contracts_by_sub_group_view b
        WHERE b.valid_from_date BETWEEN to_date('10/01/2010','mm/dd/yyyy') AND to_date('12/01/2010','mm/dd/yyyy'))
    AND a.coverage_set_id in (
        SELECT b2.coverage_set_id
        FROM contracts_by_sub_group_view b2
        INNER JOIN request c on c.request_id=b2.request_id
        WHERE c.request_status = 1463)
    AND ?.summary_attribute = 2004687  
    AND a.benefit_id <> 1092333  

GROUP BY  
    a.coverage_set_id  

HAVING  
    COUNT (a.benefit_id) > 1  

This removes the JOIN magnification that was occurring on the FROM since those tables are not needed to pull coverage_set_id and benefit_id.这将删除在 FROM 上发生的 JOIN 放大,因为不需要这些表来拉出coverage_set_id 和benefit_id。 The only remaining need for the other 2 tables is to filter out data based on criteria, which is in the WHERE clause.其他 2 个表的唯一剩余需求是根据条件过滤掉数据,这在 WHERE 子句中。

I'm not sure what table summary_attribute lives in but it would follow a similar pattern to valid_from_date, request_status, or benefit_id.我不确定summary_attribute 位于哪个表中,但它会遵循与valid_from_date、request_status 或benefit_id 类似的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM