Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select distinct problem

Posted on 2003-11-17
5
Medium Priority
?
325 Views
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?
0
Comment
Question by:tomrwilson
  • 2
  • 2
5 Comments
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 120 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
GROUP BY guid
ORDER BY guid
0
 

Author Comment

by:tomrwilson
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.
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 80 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

Hilaire


PS: the alias should resolve the rs issue
0
 
LVL 70

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
GROUP BY guid
ORDER BY guid
0
 

Author Comment

by:tomrwilson
ID: 9764789
That did it.  Thanks!!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

824 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