More Efficient SQL Query to get Distinct List

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
Currently I have data that has a few fields. The field that I am interested in is called Procedure Code. I am looking for a way to determine all of the Accession ID's that contain two distinct Procedure Codes. For example if I have an Accession that has procedure codes "88141" and "88175" I would like to include it in my query. THe issue is that I need to look at multiple combinations. In the below query I have two combinations:

88141 & 88175
111 & 222

My query works fine for this small dataset, THe issue is that I actually have a total of 546 combinations of codes and 5,000,000 records. I have been able to get my method to work on a total of 4 combinations of codes, however, it took 7 minutes to run. Is there a more efficient way to determine which Acession ID's contain combinations of these Procedure Codes? The reason I use the Distinct is because there is also the possiblity of overlap....

Code:
SELECT DISTINCT REALBIGQUERY.[Accession ID]
FROM 
(SELECT CALCQuery.*
FROM (SELECT qryGCodeUnder300Detail.[Accession ID], Sum(IIf([qryGCodeUnder300Detail].[Procedure Code]="88141",1,0)) AS FIRSTGCODE, Sum(IIf([qryGCodeUnder300Detail].[Procedure Code]="88175",1,0)) AS SECONDGCODE FROM qryGCodeUnder300Detail 
GROUP BY qryGCodeUnder300Detail.[Accession ID]) AS CALCQuery
WHERE FIRSTGCODE >= 1 AND SECONDGCODE >= 1
UNION
SELECT CALCQuery.*
FROM (SELECT qryGCodeUnder300Detail.[Accession ID], Sum(IIf([qryGCodeUnder300Detail].[Procedure Code]="111",1,0)) AS FIRSTGCODE, Sum(IIf([qryGCodeUnder300Detail].[Procedure Code]="222",1,0)) AS SECONDGCODE FROM qryGCodeUnder300Detail 
GROUP BY qryGCodeUnder300Detail.[Accession ID])  AS CALCQuery
WHERE FIRSTGCODE >= 1 AND SECONDGCODE >= 1) AS REALBIGQUERY;

Excel 2012
ABCDEF
1IDAccession IDPrimary Payor IDProcedure CodeUnits PaidPosted Paid Amount
21CY1500389BSSC*N/M*00.00
32CY1500389BSSC8814100.00
43CY1500389BSSC8817500.00
54CY1500389BSSC8814100.00
65CY1500389BSSC8817500.00
76CY1500389BSSC*N/M*00.00
87CY1500389ABSSC*N/M*00.00
98CY1500389ABSSC8762400.00
109CY1500390BSSC8814100.00
1110CY1500390BSSC8814100.00
1211CY1500390BSSC11100.00
1312CY1500390BSSC8817500.00
1413CY1500390BSSC22200.00
1514CY1500390BSSC8817500.00
1615CY1500390ABSSC8762500.00
1716CY1500390ABSSC8762400.00
1817CY1500390ABSSC*N/A*00.00
1918CY1500391ATCSC*N/A*00.00
2019CY1500391ATCSC88142121.67
2120CY1500391AATCSC8814100.00
2221CY1500391AATCSC88175138.21
2322CY1500392ABS8814200.00
qryGcodeUnder300Detail


Any assistance in getting my query more efficient would be much appreciated. Thank you for looking into my situation.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
try this
Code:
SELECT DISTINCT 
  REALBIGQUERY.[Accession ID]
from
(
  
    SELECT 
      qryGCodeUnder300Detail.[Accession ID], 
      qryGCodeUnder300Detail.[Procedure Code] as [pcode],
      count(*) as number_of_records
    FROM 
      qryGCodeUnder300Detail 
    GROUP BY 
      qryGCodeUnder300Detail.[Accession ID],
      qryGCodeUnder300Detail.[Procedure Code]
) as REALBIGQUERY 
where 
(
  (
    pcode in ( '88141', '88175' )
    and
    number_of_records > 0
  )
  or 
  (
    pcode in ( '111', '222' )
    and
    number_of_records > 0
  )
)

you might even be able to shorter and do this

Code:
SELECT DISTINCT 
  REALBIGQUERY.[Accession ID]
from
(
  
    SELECT 
      qryGCodeUnder300Detail.[Accession ID], 
      qryGCodeUnder300Detail.[Procedure Code] as [pcode]
    FROM 
      qryGCodeUnder300Detail 
    GROUP BY 
      qryGCodeUnder300Detail.[Accession ID],
      qryGCodeUnder300Detail.[Procedure Code]
) as REALBIGQUERY 
where 
(
  (
    pcode in ( '88141', '88175' )
  )
  or 
  (
    pcode in ( '111', '222' )
  )
)
 
Upvote 0
james_lankford,

Thank you for the help. I just tried the two queries you provided. Unfortunately they both pulled in these two values that do not meet the criteria:

Excel 2013
ABCDEF
4948CY1500395TRIS88142125.13
5049CY1500395TRIS88141130.07
5150CY1500395TRIS*N/A*00.00
5251CY1500396GRW8814200.00
5352CY1500396GRW8814100.00
5453CY1500396GRW*N/A*00.00
5554CY1500396GRW*N/M*00.00

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
qryGcodeUnder300Detail




This looks promising though. If it would be helpful for me to put the values in another table called tblCriteria. I could do that:

Excel 2013
AB
1GCode1GCode2
28814188175
3111222

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
tblCriteria
 
Upvote 0
take this
Code:
SELECT 
  qryGCodeUnder300Detail.[Accession ID], 
  qryGCodeUnder300Detail.[Procedure Code] as [pcode],
  count(*) as number_of_records
FROM 
  qryGCodeUnder300Detail 
GROUP BY 
  qryGCodeUnder300Detail.[Accession ID],
  qryGCodeUnder300Detail.[Procedure Code]
and save as
REALBIGQUERY

then try this
Code:
SELECT 
  REALBIGQUERY.[Accession ID], 
  REALBIGQUERY.pcode, 
  REALBIGQUERY_1.pcode,
  REALBIGQUERY.number_of_records,
  REALBIGQUERY_1.number_of_records
FROM 
(
  REALBIGQUERY 
  INNER JOIN 
  REALBIGQUERY AS REALBIGQUERY_1 
    ON 
    REALBIGQUERY.[Accession ID] = REALBIGQUERY_1.[Accession ID]
) 
INNER JOIN 
tblCriteria 
  ON 
  (
    REALBIGQUERY_1.pcode = tblCriteria.GCode2
  ) 
  AND 
  (
    REALBIGQUERY.pcode = tblCriteria.GCode1
  )
WHERE 
(
  (
    REALBIGQUERY.pcode <> REALBIGQUERY_1.pcode 
  )
)

but with 500000 records it still may take forever
 
Upvote 0
This ended up working great! Thanks for the help. Only took about 3 or 4 minutes, which is alot faster then I expected. :)
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top