473,385 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Duplicates query problem

I have a complicated query that is giving me duplicate records for some
of the returned records. Underwriter name is what I don't want to
duplicate. Any clues? Thanks for any help!

SELECT qryUnderwriters.UWID, qryUnderwriters.UWName,
qryUnderwriters.JobTitle, qryUnderwriters.BUName,
qryUnderwriters.EmployHireDate, qryUnderwriters.ApptStartDate,
qryUnderwriters.TermDate, qryUnderwriters.EmployTypeCode,
qryUnderwriters.Active, qryDL_WatchFinal_2005.TotalLoans AS DL_Loans,
qryDL_WatchFinal_2005.TotalLoss AS DL_Loss, qryDL_WatchFinal_2005.Watch
AS DL_Watch, qryDL_WatchFinal_2005.Refer AS DL_Refer,
qryLR_WatchFinal_2005.TotalLoans AS LR_Loans,
qryLR_WatchFinal_2005.TotalPenalty AS LR_Penalty,
qryLR_WatchFinal_2005.PercCorrect AS LR_Correct,
qryLR_WatchFinal_2005.LR_Watch, qryLR_WatchFinal_2005.LR_Refer,
CalcFileReqs([CSAudits_2005],[CS2005_Perc],[CSAudits_12MoCum],[CS12Mo_Perc],[CS_SigFinds],[DLPriorLosses],[DLNewLosses])
AS AuditLevel, qry2005_FileReqInfo.CSAudits_2004,
qry2005_FileReqInfo.CS2004_Perc, qry2005_FileReqInfo.CSAudits_2005,
qry2005_FileReqInfo.CS2005_Perc, qry2005_FileReqInfo.CSAudits_12MoCum,
qry2005_FileReqInfo.CS12Mo_Perc, qry2005_FileReqInfo.CS_SigFinds,
qry2005_FileReqInfo.DLPriorLosses, qry2005_FileReqInfo.DLNewLosses
FROM ((qryUnderwriters LEFT JOIN qry2005_FileReqInfo ON
qryUnderwriters.UWID = qry2005_FileReqInfo.UWID) LEFT JOIN
qryDL_WatchFinal_2005 ON qryUnderwriters.UWID =
qryDL_WatchFinal_2005.UWID) LEFT JOIN qryLR_WatchFinal_2005 ON
qryUnderwriters.UWID = qryLR_WatchFinal_2005.NewUWID
WHERE (((qryDL_WatchFinal_2005.Watch)=True)) OR
(((qryDL_WatchFinal_2005.Refer)=True)) OR
(((qryLR_WatchFinal_2005.LR_Watch)=True)) OR
(((qryLR_WatchFinal_2005.LR_Refer)=True));

Jun 6 '06 #1
3 1734
If your dataset contains records that have groups of underwriters
(multiple records with the same underwriter) and you only want to
retrieve one record from each group of underwriters you can try a query
like this one:

SELECT * FROM sourceTable t1 WHERE EXISTS
(SELECT * FROM (SELECT TOP 1 * FROM sourceTable t2 WHERE t2.RecordId =
t1.RecordId) t1 WHERE t1.ID = t1.ID)

In this example, the RecordID field would be a field that separates each
Underwriter. In your case you could use the UnderwriteName's field as
your RecordID field. The "ID" field is the unique key field in the
table. The "ID" field uniquely identifies each row in the sourceTable.

the sourceTable can also be a query (queries are tables).

What I am doing here is aliasing the table name of sourceTable with the
aliases t2 and t1. Basically I am doing self joins here. This query
assumes you have a unique key field in the table.

If you don't have a unique key but you can uniquely identify a row based
on a combination of fields that would be non repeating you can modify
the above query as follows:

SELECT *
FROM sourceTable t1 WHERE EXISTS
(SELECT * FROM
(SELECT TOP 1 * FROM sourceTable t2
WHERE t2.Underwriter = t1.Underwriter) t3
WHERE t3.fld1 = t1.fld1
AND t3.fld2 = t1.fld2
AND t3.fld3 = t1.fld3
AND t3.fld4 = t1.fld4
)

Here I am separating each group of underwriters and using 4 fields to
uniquely identify each row in each group of underwriters (I am making
this up of course). You could get away with 2 fields if you know you
could uniquely identify each row in each group of underwriters with just
2 fields.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Jun 6 '06 #2
I see the SELECT TOP logic, kinda, but in my query how can I set an
ALIAS for the specific query (or table) and in this case I'm assuming I
need to produce two different ALIASES for my origination query?
Rich P wrote:
If your dataset contains records that have groups of underwriters
(multiple records with the same underwriter) and you only want to
retrieve one record from each group of underwriters you can try a query
like this one:

SELECT * FROM sourceTable t1 WHERE EXISTS
(SELECT * FROM (SELECT TOP 1 * FROM sourceTable t2 WHERE t2.RecordId =
t1.RecordId) t1 WHERE t1.ID = t1.ID)

In this example, the RecordID field would be a field that separates each
Underwriter. In your case you could use the UnderwriteName's field as
your RecordID field. The "ID" field is the unique key field in the
table. The "ID" field uniquely identifies each row in the sourceTable.

the sourceTable can also be a query (queries are tables).

What I am doing here is aliasing the table name of sourceTable with the
aliases t2 and t1. Basically I am doing self joins here. This query
assumes you have a unique key field in the table.

If you don't have a unique key but you can uniquely identify a row based
on a combination of fields that would be non repeating you can modify
the above query as follows:

SELECT *
FROM sourceTable t1 WHERE EXISTS
(SELECT * FROM
(SELECT TOP 1 * FROM sourceTable t2
WHERE t2.Underwriter = t1.Underwriter) t3
WHERE t3.fld1 = t1.fld1
AND t3.fld2 = t1.fld2
AND t3.fld3 = t1.fld3
AND t3.fld4 = t1.fld4
)

Here I am separating each group of underwriters and using 4 fields to
uniquely identify each row in each group of underwriters (I am making
this up of course). You could get away with 2 fields if you know you
could uniquely identify each row in each group of underwriters with just
2 fields.

Rich

*** Sent via Developersdex http://www.developersdex.com ***


Jun 8 '06 #3
If you have a query that contains all of your query code you can select
that into a table that would serve as a temp table:

select * into tempTbl from yourFinalQuery

Now you can query tempTbl using a group by on your underwriter field to
see all the groups of underwriters. Pick one underwriter for testing.

Select * from temptbl where underwriter = 'Harry Smith'

Say you have 10 different underwriters and each underwriter has 10 rows
of data per underwriter (the duplicate rows). The goal is to retrieve
just one row per underwriter. You can use the Top operator for this

Select Top 1 * From tempTbl where Underwriter = 'Harry Smith'

This is just to show you what it is that my query is doing for you.

Now you can adapt my query trick - what this does is the same thing as
the line of code above for 'Harry Smith' except for all the underwriters
in one shot:

SELECT *
FROM tempTbl t1 WHERE EXISTS
(SELECT * FROM
(SELECT TOP 1 * FROM tempTbl t2
WHERE t2.Underwriter = t1.Underwriter) t3
WHERE t3.fld1 = t1.fld1
AND t3.fld2 = t1.fld2
AND t3.fld3 = t1.fld3
AND t3.fld4 = t1.fld4
)
Note: you don't have to use 4 fields in the last where clause. Try
using only 2 fields

SELECT *
FROM tempTbl t1 WHERE EXISTS
(SELECT * FROM
(SELECT TOP 1 * FROM tempTbl t2
WHERE t2.Underwriter = t1.Underwriter) t3
WHERE t3.fld1 = t1.fld1
AND t3.fld2 = t1.fld2
)
You might even experiment with leaving the last where clause out
altogether (if all 10 rows are exact rows)

SELECT *
FROM tempTbl t1 WHERE EXISTS
(SELECT * FROM
(SELECT TOP 1 * FROM tempTbl t2
WHERE t2.Underwriter = t1.Underwriter) t3)
But don't forget that last parenthesis. It might not work without the
last where clause. You will have to experiment.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Jun 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: TimG | last post by:
I have a column that has 75 values, 50 are unique/25 are duplicates. I need to be able to bring back a list of the duplicates, listing all rows even if more than two have the same value. I need to...
3
by: Tom Mitchell | last post by:
All: I'm stumped on a query. How do I find duplicates in a table where one of the duplicates has values is a certain field and the other doesn't. For example, I have the following table: ...
2
by: M.Stanley | last post by:
Hi, I have a problem..I'm doing a specific query where I'm joining fields from a table with appednded data (there are duplicate records, except for the date/time), and another query. I want the...
1
by: MHenry | last post by:
Hi, I have a table with duplicate records. Some of the duplicates need to be eliminated from the table and some need not. A duplicate record does not need to be eliminated if the one record...
6
by: Marlene | last post by:
Hi All I have the following scenario, where I have found all the duplicates in a table, based on an order number and a part number (item).I might have something like this: Order PODate Rec...
3
by: Sim Zacks | last post by:
I am using 8.0 beta 1 on an RH 8 Linux server. I have a union query that I am converting from access (where it worked) and it is returning duplicates. The only difference between the two rows is...
16
by: tyrfboard | last post by:
I've been searching for awhile now on how to remove duplicates from a table within an Access db and have found plenty of articles on finding or deleting duplicates. All I want to do is remove them...
3
by: AK | last post by:
Hi Our product uses MS-SQL Server 2000. One of our customer has 10 installations with each installation stroring data in its own database. Now the customer wants to consolidate these databases...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
1
by: KimmyG | last post by:
I'm just starting to use SQL and am much more experienced in Access. Here is what I do in Access Copy a table and rename the new table "copytable" also select structure only. Open "copytable"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.