Solved

Select distinct problem

Posted on 2003-11-17
5
306 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 69

Assisted Solution

by:ScottPletcher
ScottPletcher 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
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 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

Hilaire


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

Expert Comment

by:ScottPletcher
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 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

16 Experts available now in Live!

Get 1:1 Help Now