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
Solved

Select distinct problem

Posted on 2003-11-17
5
315 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: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
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: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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

829 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