SQL Finding Duplicates with multiple criteria but different ID numbers - sql

I work with claims and I have been trying to write a query that captures different claim numbers with multiple criteria, however, I can not get the desired return. The picture I attached is some what an idea of a table I am working with. I need to return different claim numbers with the following criteria being the same:
Sum(billed), Diagnosis_code, Rev_code, Cpt_Code, POS_Code, Member_ID, Provider_ID, Organization_ID, DOS, Rendering_Provider_ID.
Those criteria need to match exactly and the may not follow the same ascending or descending order as shown in the table. Here is the screen shot
I only want claim_no 101 and 102 to return because they have different claim numbers but match everything else. I do not want claim_no 103 because it does not match all of the above criteria.
I work with SQL Server 2012. Don't know if it maters but DOS data type is datetime. Any help will be greatly appreciated. Thanks.

If you want rows that match another row, you can do something like this:
select t.*
from t
where exists (select 1
from t t2
where t2.claim_no <> t.claim_no and
t2.Diagnosis_code = t.Diagnosis_code and
t2.Rev_code = t.Rev_code and
. . .
);
Fill in the . . . with the conditions that you want.

As per your sample data, below query will be work. If you want to add filter condition you can append with this query.
Select Clime_no, Sum(billed), Member_ID, Provider_ID, Organization_ID, DOS, Rendering_Provider_ID
from table_name
group by Clime_no, Member_ID, Provider_ID, Organization_ID, DOS, Rendering_Provider_ID

Related

Validate that only one value exists

I have a table with two relevant columns. I'll call them EID and MID. They are not unique.
In theory, if the data is set up correctly, there will be many records for each EID and every one of those records should have the same MID.
There are situations where someone may manually update data incorrectly and I need to be able to quickly identify if there is a second MID for any EID.
Ideally, I'd have a query that returns how many MIDs for each EID, but only showing results where there is more than 1 MID. Below is what I'd like the results to look like.
EID Count of Distinct MID values
200345 2
304334 3
I've tried several different forms of queries, but I can't seem to figure out how to reach this result. We're on SQL Server.
You can use the following using COUNT with DISTINCT and HAVING:
SELECT EID, COUNT(DISTINCT MID)
FROM table_name
GROUP BY EID
HAVING COUNT(DISTINCT MID) > 1
demo on dbfiddle.uk

trying to get only one row from each group

I have a problem with DISTINCT ON. I have five different groups of people which include names and surnames. My goal is to get only one name per group (the first one). When try to use DISTINCT ON, I got an error.
SELECT DISTINCT ON (group_A) surname FROM table
I want my table to look like
group_A...surname_a
broup_B...surname_b
.
.
.
Thank you for your help
The syntax in Postgres would be:
SELECT DISTINCT ON (group_A) group_A, surname
FROM table
ORDER BY group_A;

De-Dupe Query in MS Access

I'm using Access to create a report. I have several duplicate records in my Avail_Amt field. I used the Query Wizard to de-dup records from a table, like this.
In (SELECT [DEALLOCALBAL] FROM [TBL_DATA] As Tmp GROUP BY [DEALLOCALBAL] HAVING Count(*)=1 )
What I really want to do, though, is de-dupe the records in my Query, not in my Table. I think it should look like the script below, but it's not working.
In (SELECT [DEALLOCALBAL] FROM [dbo_LIMIT_HIST.Avail_Amt] As Tmp GROUP BY [DEALLOCALBAL] HAVING Count(*)=1 )
dbo_LIMIT_HIST is the Table in SQL Server and Avail_Amt is the Field that I am trying to de-dupe. Any idea what could be wrong here?
My data set looks like this:
Basically, for each Contact ID I could have multiple DEALLOCALBAL amounts. I want to capture each unique amount for each Contact ID, but I don't want dupes.
Thanks!!

How do I identify like sets using SQL?

Using SQL Server, I have a table that looks like this:
What I need to do is write a query to identify scenarios where the Name and Permissions field are equal so that I can give give them a unique Set ID.
For instance, rows 2 and 4 would be a set I can give a SetID as well as rows 6 and 7 are a set that I can give another SetID. But rows 2 and 3 are NOT a set.
So far I have tried using DENSE_RANK () Over(Order by Name) which helps to add an id based on like Names but doesn't take into account matching permissions. And have tried joining the table on itself but with millions of rows of data I end up with unwanted duplicates.
The logic I am following is this:
If (Name and Permissions) of one row = (Name and Permissions) of another row give them a SetID to share.
Please help I have been banging my head against the wall with this one. Ideally a SQL query would accomplish this but am open to anything.
Thank you!
You could do it for example like this:
select
Name,
Permission,
row_number() over (order by Name, Permission) as RN
from (
select distinct
Name,
Permission
from
permissions
) TMP
order by Name, Permission
The inner select gets the distinct combinations, and the outer one assigns the numbers.
SQL Fiddle: http://sqlfiddle.com/#!6/c8319/3
This will probably do something similar to what you want.
SELECT
name,
permissions,
accountname,
ROW_NUMBER() OVER (PARTITION BY name,permissions ORDER By name,permissions) as SetID
FROM table;

access select distinct on certain column

I have a table with some search results. The search results maybe repeated because each result may be found using a different metric. I want to then query this table select only the distinct results using the ID column. So to summarize I have a table with an ID column but the IDs may be repeated and I want to select only one of each ID with MS Access SQL, how should I go about doing this?
Ok I have some more info after trying a couple of the suggestions. The Mins, and Maxes won't work because the column they are operating on cannot be shown. I get an error like You tried to execute a query that does not include the specified expression... I now have all my data sorted, here is what it looks like
ID|Description|searchScore
97 test 1
97 test .95
120 ball .94
97 test .8
120 ball .7
so the problem is that since the rows were put into the table using different search criteria I have duplicated rows with different scores. What I want to do is select only one of each ID sorted by the searchScore descending. Any ideas?
SELECT DISTINCT ID
FROM Search_Table;
Based on the last update to your question, the following query seems appropriate.
SELECT ID, [Description], Max(searchScore)
FROM Search_Table
GROUP BY ID, [Description];
However that's nearly the same as Gordon's suggestion from yesterday, so I'm unsure whether this is what you want.
Here is a way where you can get one of the search criteria:
select id, min(search_criteria)
from t
group by id
This will always return the first one alphabetically. You can also easily get the last one using max().
You could also use:
select id, first(search_criteria)
from t
group by id