Solved

SQL 2000 GROUP BY OR DISTINCT CLAUSE

Posted on 2006-06-21
3
856 Views
Last Modified: 2008-03-06
I have a table with several columns. I want to SELECT DISTINCT records based on two of the columns values. I also need to retrieve the value of a third column but I don't want it used in the DISTINCT virtual table.

This query gives me the data I need:

SELECT Column1, Column2
FROM Table1
GROUP BY Column1, Column2

I also need in Column3 in my result. If I use this query I get an error because it's not used in the GROUP BY Statement

SELECT Column1, Column2, Column3
FROM Table1
GROUP BY Column1, Column2

Any suggestions?

0
Comment
Question by:mre531s
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 16950457
you have to use some aggregate function like this:

SELECT Column1, Column2, max(Column3)
FROM Table1
GROUP BY Column1, Column2

or add the column to the group by
or add the column to the group by and filter/merge the duplicates in the front-end
or use some more complex subquery technique depending on the results you want to have
0
 
LVL 4

Expert Comment

by:indu_mk
ID: 16950560
select a.column1, a.column2, a.column3 from table1 a,
(select column1, column2 from table1 group by column1, column2) b
where a.column1 = b.column1
and a.column2 = b.column2
0
 

Author Comment

by:mre531s
ID: 16950585
The MAX FUNCTION Will work

Column1      Column2       Column3
John              Doe                1
John              Doe                2
John              Doe                8

The following Example using MAX() will give me a result of

John             Doe                8

I only need 1 value from column 3 as a return value to update the table

Thanks!!!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now