473,473 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Eliminate Duplicates

I am working with SQL 8.00. I have the following tables
TABLE ClientInfo

CheckNum Account Name Addr1 City State Zip
---------------------------------------------------------------------
12345 11111 John 123 Mary St Miami FL 33139
54321 22222 Mary 321 River Side Clifton NJ 07055
98765 33333 Tom 12 Main St Miami FL 33139
and TABLE ClientAcct

CheckNum Account Cost Credit Notes
---------------------------------------------------------------------
12345 11111 1 Yes Great
12345 11111 11 Yes Well
12345 11111 111 No Bad
54321 22222 2 Yes Fine
54321 22222 22 No OK
98765 33333 3 Yes I like it
This the end result that I want.

CheckNum Account Name Addr1 City State Zip SUM of
Max(Notes)
of Cost or
Min(Notes)
-----------------------------------------------------------------------------
12345 11111 John 123 Mary St Miami FL 33139 123 Great
54321 22222 Mary 321 River St Clifton NJ 07055 24 Fine
98765 33333 Tom 12 Main St Miami FL 33139 3 I like it
I need to avoid duplicate rows(Note only if the Account field are
equal).
I need to get the fields shown above including the SUM of
ClientAcct.Cost and the MAX or Min of ClientAcct.Notes.

I have searched the web and
have tried DISTINCT, UNION, GROUP, COUNT(*) = 1 AND COUNT(*) <> 1 but
I can't get the correct results.

This statement give me too many rows(duplicate)
--------------------------------------
SELECT [Name], ClientInfo.account, addr1, City, State, ZIP, COST,
Notes from ClientInfo JOIN ClientAcct on
ClientInfo.CheckNum=ClientAcct.CheckNum

Does any body have a solution to this?

Thanks in advance
Julio
Jul 20 '05 #1
1 5884
[posted and mailed, please reply in news]

Julio Allegue (ja******@med.miami.edu) writes:
This the end result that I want.

CheckNum Account Name Addr1 City State Zip SUM of
Max(Notes)
of Cost or
Min(Notes)
--------------------------------------------------------------------------
---

12345 11111 John 123 Mary St Miami FL 33139 123 Great
54321 22222 Mary 321 River St Clifton NJ 07055 24 Fine
98765 33333 Tom 12 Main St Miami FL 33139 3 I like it


There is a general rule for this sort of questions what you should post
for this sort of question.

o CREATE TABLE statements for your tables.
o INSERT statements with sample data
o The output given the sample data.

If you do that, it is very easy by means to cut and paste to not only
write a solution, but also to *test* it.

One thing is not wholly clear from your post. Do you want the min/max
of the Note itself, or the Note that goes with the min/max cost? Your
text says the former, but your sample output indicates the latter, and
this is what I have assumed.

Thus, the suggestion that follows is untested, and you will have to refine
it yourself:

SELECT ci.CheckNum, ci.Account, ci.Name, ci.Addr1, ci.City,
ci.State, ci.Zip, s.Costsum, ca.Notes
FROM ClientInfo ci
JOIN (SELECT CheckNum, Account,
Costsum = SUM(Cost), mincost = MIN(Cost)
FROM ClientAcct
GROUP BY CheckNum, Account) as s ON ci.Account = s.Account
AND ci.CheckNum = s.CheckNum
JOIN ClientAcct ca ON ca.Account = ci.Account
AND ca.CheckNum = ci.CheckNum
AND ca.Cost = s.Cost

There is one thing that is disturbing with this solution: if there are
more than two or more rows that are tied for the min value, then you
will get all those rows. Since ClientAcct does not seem to have a unique
key, this is quite difficult to address.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

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

Similar topics

1
by: gilgantic | last post by:
Is there anyway of eliminating duplicate entries as the database loads data using SQLLDR and/or .ctl (Control File)? I use the following command line using SQLLDR and a control file to load my...
2
by: Francesco Moi | last post by:
Hello. I've got a 'books' table with 'id', 'name', 'author' and 'isbn' fields. Due to an error, my books are duplicated, so I've got (e.g.): 430 - Moby Dick - Herman Melville - 0192833855...
3
by: Tony Young | last post by:
Hi, I have a multimap container. I want to eliminate all "duplicate" elements. By duplicate I mean something like (3, 4), (4, 3) and (4, 3), in which I want to eliminate any two of these...
3
by: John Davis | last post by:
I currently have a Table with these fields: Symbol; Price; Date with data that looks like this: IBM; 10; 10/15/2003 IBM; 11; 10/29/2003 GM; 20; 10/15/2003 F; 25; 9/15/2003
1
by: GB | last post by:
Hello: I have a table like this: ID | AccNo -------------------------------- 1 0059 2 0059 3 0059 4 0194 5 0194
3
by: _eddie_ | last post by:
I'm building an array of strings on the fly from a database. What is the best method for eliminating duplicates? (I can do this before or after the strings are added to the array)
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...
3
by: italia | last post by:
I have a database with 2 columns and more than million rows. The first column is the id Example of the data (2 columns)- 04731 CRM 04731 CRM 04731 CRM 04731 RVB 04731 RVB
3
by: ryan.paquette | last post by:
In the table there are 2 fields in which I wish to limit (i.e. No Duplicates) Although I do not want to limit them to "No Duplicates" separately. I need them to be limited to "No Duplicates" as...
1
by: tskmjk55 | last post by:
Recently, I have a requirement to develop a vb.net application wherein the input excel sheet data which has an average of 5000 records should be checked for Internal duplicates (duplicates within the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.