Select distinct problem

Posted on 2003-11-17
Last Modified: 2011-09-20
I have a table that is a set of responses to questions.  Each record is stamped with a GUID to identify each record as being part of a set.  So if I do

select distinct GUID from table1

I get a list of unique GUID's.  But I want this ordered by the field Entered (entered date/time)  I can't do this:

select distinct guid,entered from table1 order by entered

cause the sets are no longer distinct if I include 'Entered'.  How can I get the query to return data that is also not distinct like the entered date for each 'GUID distinct' record it returns?
Question by:tomrwilson
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 30 total points
ID: 9763692
You have to decide which date, and other data, you want to see: MIN, MAX or AVG.

For example:

SELECT guid, MAX(entered), MIN(someOtherColumn)
FROM table1

Author Comment

ID: 9763814
Thanks... I tried this:

SELECT guid, MAX(entered) from answers group by guid order by guid

I don't get an error but when I then try this:

Listbox1.AddItem rsAnswers!Entered

I get 'Item cannot be found in the collection...'  I need to return both the distinct GUID and its entered date.
LVL 26

Accepted Solution

Hilaire earned 20 total points
ID: 9763872
You can sort on a computed column like sum, count, max

assuming that you need theresults sorted by time descending
the query would be

select guid, min(entered) as entered
from table1
group by guid
order by min(entered) desc


PS: the alias should resolve the rs issue
LVL 69

Expert Comment

by:Scott Pletcher
ID: 9764274
Yes, I mistakenly left off the alias(es):

SELECT guid, MAX(entered) AS entered, MIN(someOtherColumn) AS someOtherColumn
FROM table1

Author Comment

ID: 9764789
That did it.  Thanks!!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CAST issue with SQL 2000 15 44
SQL Server How to create a List of Values comma separated from a TABLE? 4 44
Removing SCCM 2016 4 53
SQL Query 9 29
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

739 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