473,396 Members | 1,703 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,396 software developers and data experts.

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 5881
[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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.