Solved

Select distinct problem

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Generate Scripts Fails 5 36
create an aggregate function 9 36
SQL Query assistance 16 27
How to use three values with DATEDIFF 3 26
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

803 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