简体   繁体   中英

How to retrieve data from ms-access where one field is in another table and are on the same date in the other table?

Hello all I am new to Access I am trying to get data from one table that is dependent on another table. So basically I have one table named MUE_Values that stores 2 fields (cpt_code) and (mue_value) . I have another table called Claims... this table holds approximately 75 fields. My question is- from the claims table I want to get all the fields where the (cpt_code) matches the (cpt_code) from the MUE_Values TABLE and the (units) > (mue_value) ,also I want to get all the rows where the (cpt_code) matches the same (cpt_code) in the claims table and was on the same date for the (patient_ssn) whether or not the (units) > (mue_values) .

For Example: Claims Table

ID  patient_ssn cpt_code from_date  units   Field5  Field6  Field7  etc
1   098345567   12345   10/27/2015  1                                       
2   098345567   12345   10/27/2015  1                                       
3   098345567   09786   10/2/2015   5                                       
4   098345567   54321   10/7/2015   3                                       
5   098345567   87654   10/5/2015   2                                       
6   098345567   87654   10/6/2015   3                                       
7   789797979   65432   10/7/2015   1                                       
8   789797979   65432   10/3/2015   2                                       
9   789797979   76543   10/13/2015  5                                       
10  789797979   76541   10/13/2015  1                                       
11  789797979   12345   10/27/2015  1                                       
12  789797979   87654   10/27/2015  0

MUE_values TABLE

cpt_code    mue_value
12345   1
23456   1
34567   1
45678   1
54321   2
56789   4
65432   1
76543   3
87654   2
98765   2

So this query alone gets the patient data with the same date and a count of the dates > 1 with that same (cpt_code) .

SELECT Claims.patient_ssn, Claims.from_date, Count(Claims.from_date)     AS CountOffrom_date
FROM Claims INNER JOIN MUE_values ON Claims.cpt_code =   MUE_values.cpt_code
WHERE (((Claims.cpt_code)=[MUE_values].[cpt_code]))
GROUP BY Claims.patient_ssn, Claims.from_date, MUE_values.cpt_code
HAVING (((Count(Claims.[from_date]))>1))
WHERE (((Claims.units)>[mue_value]));  

But when I put together into one query it returns all the rows with the same from_date instead of just the one from the query above along with the other four from the first subquery

SELECT Claims.*
FROM Claims
WHERE EXISTS(SELECT * 
          FROM MUE_values
          WHERE Claims.cpt_code = MUE_values.cpt_code AND
                Claims.units > MUE_values.[mue_value]
         ) OR
 EXISTS (SELECT Claims.patient_ssn, Claims.from_date,     Count(Claims.from_date) AS CountOffrom_date
FROM Claims INNER JOIN MUE_values ON Claims.cpt_code = MUE_values.cpt_code
WHERE (((Claims.cpt_code)=[MUE_values].[cpt_code]))
GROUP BY Claims.patient_ssn, Claims.from_date, MUE_values.cpt_code
HAVING (((Count(Claims.[from_date]))>1)));

This should be the result:

ID  patient_ssn cpt_code from_date  units   Field5  Field6  Field7 etc...
1   098345567   12345   10/27/2015    1                                     
2   098345567   12345   10/27/2015    1                                     
4   098345567   54321   10/7/2015     3                                     
6   098345567   87654   10/6/2015     3                                     
8   789797979   65432   10/3/2015     2                                     
9   789797979   76543   10/13/2015    5     

Instead I am Getting this result:

ID  patient_ssn cpt_code from_date  units   Field5  Field6  Field7 etc...
1   098345567   12345   10/27/2015    1                                     
2   098345567   12345   10/27/2015    1                                     
3   098345567   09786   10/2/2015     5                                     
4   098345567   54321   10/7/2015     3                                     
5   098345567   87654   10/5/2015     2                                     
6   098345567   87654   10/6/2015     3                                     
7   789797979   65432   10/7/2015     1                                     
8   789797979   65432   10/3/2015     2                                     
9   789797979   76543   10/13/2015    5                                     
10  789797979   76541   10/13/2015    1 
11  789797979   12345   10/27/2015    1                                     
12  789797979   87654   10/27/2015    0             

When I read your requirements, it sounds like EXISTS :

SELECT Claims.*
FROM Claims
WHERE EXISTS (SELECT 1 
              FROM MUE_values
              WHERE Claims.cpt_code = MUE_values.cpt_code AND
                    Claims.units > MUE_values.[mue_value]
             ) OR
      EXISTS (SELECT 1
              FROM MUE_values
              WHERE Claims.cpt_code = MUE_values.cpt_code AND
                    Claims.patent_ssn = MUE_values.patient_ssn
             );

However, I have no idea what "same date" means, because one table definitely does not have a date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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