views:

76

answers:

1

I'm trying to find all coverage_set_id with more than one benefit_id attached summary_attribute (value=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. Just trying to get duplicate coverage_set_id.

Pass-Through Query via OBDC database:

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. Thank you.


::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
+1  A: 

The coverage_set_id in both the HAVING and count part of the SELECT should be benefit_id.

Since benefit_id is also in table a you can do the following

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. The only remaining need for the other 2 tables is to filter out data based on criteria, which is in the WHERE clause.

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.

DaveWilliamson
Fiona
Look at the way the 3 tables are joined. Make sure that A -> B is joined on the full set of fields that make the record unique in either A or B. Do the same for C -> B.Based on your first sentence in the question it sounds like you do want to Group by and filter out with the Having.There must be something with the join relationship that is causing the inflated results. Not know what the table(s) unique keys are make is difficult from here.
DaveWilliamson
This seems to work, but what is the COUNT part counting? Number of records affected with the duplication of benefit_short_desc?Updated original post.
Fiona
I'll take a look at how the tables are joined.
Fiona
COUNT is counting the number of records in the result set for each coverage_set_id in the result set.Based on your last edit showing the counts 4,4,8,8,32 it is definitely looking like a JOIN issue.
DaveWilliamson
coverage_set_id and request_id are unique values. Perhaps I'm not JOINing them correctly? I tried to move the links down to the WHERE section, but still get the same results:FROM coverage_set_detail_view a, contracts_by_sub_group_view b, request cWHERE a.coverage_set_id = b.coverage_set_id AND b.request_id = c.request_id AND b.valid_from_date BETWEEN to_date('10/01/2010','mm/dd/yyyy')AND to_date('12/01/2010','mm/dd/yyyy')...
Fiona
On the joins you want a INNER JOIN for both.
DaveWilliamson
Answer updated to reflect inner joins and ? put in place for you to think about which table is the main record table for that field (i.e. in a 1 to many relationship you want the 1 side).
DaveWilliamson
Thanks Dave. Let me try this.
Fiona
Okay, I did some more research and found that one coverage_set_id can be linked to many request_id. Will continue trying with that note.
Fiona
That fits with the joins you havecoverage_set_detail_view (one) --> (many) contracts_by_sub_group_view (many) <-- (one) requestWhat table is "benefit_id" in (a, b, or c)?
DaveWilliamson
benefit_id is from Table a.
Fiona
Updated answer given knowledge that both benefit_id and coverage_set_id live in table a.
DaveWilliamson
I've added another SELECT field to see what results I was getting.(COVERAGE_SET_ID, BENEFIT_SHORT_DESC, COUNT): (4168390, $75 PER DAY, 4), (4173346, $75 PER DAY, 4), (4173350, $75 PER DAY, 4), (4174168, $50 PER DAY, 4), (4174168, $75 PER DAY, 4), (4174172, $50 PER DAY, 4), (4174172, $75 PER DAY, 4). I'm expecting only 4174168 and 4174172 to show up. It's getting really close. Let me look a bit more into this to see if I can come up with anything.
Fiona
Did you get any further narrowing down the Where clause more?
DaveWilliamson
Yes, I was looking at it in the wrong way -- thanks to your suggestions. The main problem was getting a distinct set of coverage set ids and summary attribute but not distinct benefit id. Thank you for all the help! Solution posted above below my original query attempt.
Fiona
Your welcome. Thanks for sticking with it.
DaveWilliamson