Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Can DISTINCT be used on individual fields?

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-04-12 : 09:16:12
Matthew writes "I'm trying to select unique rows from a table based on one field only to return only the first row of each job number.

Example data:

JobID Name Other
~~~~~ ~~~~ ~~~~~
5000 Buy things 16
5000 Go home 8
5002 Sumthing 4
5002 Daniel 100


Needed Result:

JobID Name Other
~~~~~ ~~~~ ~~~~~
5000 Buy things 16
5002 Sumthing 4

I've tried using SELECT DISTICT but it checks all the fields which won't work.

Any ideas?

Thanks

Matthew"

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-04-12 : 09:24:38
Somthing like this
Select Job, Max([Name]),Max(Other) From Tbl Group by Job ???

If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-04-12 : 09:25:36
How do u determine out of
-------------------
5000 Buy things 16
5000 Go home 8
-------------------

The one to be selected is
-----------------------
5000 Buy things 16
-----------------------

And not
-----------------------
5000 Go home 8
-------------------

Why I ask this is, in SQL server, it is not always the same order where u see the things, when it comes to different queries. If u want a particular order, u have to tell by which field(s) u want to sort and in ASCending or DESCending.

Also the solution is not that hard if u r happy with any record from a particular Distinct JOBID


Srinika
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-12 : 09:29:05
Refer point 2
http://weblogs.sqlteam.com/mladenp/archive/2005/08/01/7421.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -