We help IT Professionals succeed at work.
Get Started

MS SQL Select distinct

390 Views
Last Modified: 2012-05-09
Hello,

I have following table with data.

service_id  service_start           service_end             updated_by last_updated
----------- ----------------------- ----------------------- ---------- -----------------------
1           2010-06-30 00:00:00.000 2010-06-30 00:00:00.000 test       2010-06-09 14:45:31.367
2           2010-06-30 00:00:00.000 2010-06-30 00:00:00.000 test       2010-06-09 14:47:17.163
3           2010-07-31 00:00:00.000 2010-07-31 00:00:00.000 test       2010-06-09 14:49:16.210
4           2010-07-01 00:00:00.000 2010-07-01 00:00:00.000 test       2010-06-09 14:49:16.210
6           2010-07-01 00:00:00.000 2010-07-01 00:00:00.000 test       2010-06-10 14:49:16.210
7           2010-07-31 00:00:00.000 2010-07-31 00:00:00.000 test       2010-06-09 14:53:16.210

I would like to select distinct service_start with latest update. So when I do a select it should look like

service_id  service_start           service_end             updated_by last_updated
----------- ----------------------- ----------------------- ---------- -----------------------
2           2010-06-30 00:00:00.000 2010-06-30 00:00:00.000 test       2010-06-09 14:47:17.163
6           2010-07-01 00:00:00.000 2010-07-01 00:00:00.000 test       2010-06-10 14:49:16.210
7           2010-07-31 00:00:00.000 2010-07-31 00:00:00.000 test       2010-06-09 14:53:16.210

When I tried a query "Select DISTINCT service_id, service_start, service_end, updated_by, last_updated from services order by last_updated desc" gives me all the records.

Thanks for help in advance.
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE