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 2005 Forums
 Transact-SQL (2005)
 Using DISTINCT ON A SINGLE FIELD

Author  Topic 

CXXXV
Starting Member

12 Posts

Posted - 2010-01-08 : 13:27:31
The following statement still returns multiple SESSION_ID records

SELECT DISTINCT(SESSION_ID),ID
FROM RES_SCHED
WHERE SUB_PROG_ID = 10


It appears that SQL SERVER does not support this syntax. Is there some syntax that will make this work. I've searched for quite some time now.

DP978
Constraint Violating Yak Guru

269 Posts

Posted - 2010-01-08 : 13:29:44
do you have multiple IDs per session ID?

Have you tried CROSS APPLY yet?

Edit: You should give an example of your table and expected vs Actual results, unless you put a max/MIN ID you will still have multiple lines.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-01-08 : 13:30:58
How about GROUP BY?

SELECT MAX(SESSION_ID) AS SESSION_ID, ID
FROM RES_SCHED
WHERE SUB_PROG_ID = 10
GROUP BY ID

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-08 : 13:42:40
you may be better off posting some data to give us an idea of how you want values to be retrieved.
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2010-01-08 : 16:00:41
It will return multiple session_id records, probably because there are multiple combinations of :session_id and id. Which , in effect is the same as :
SELECT SESSION_ID,ID
FROM RES_SCHED
WHERE SUB_PROG_ID = 10
GROUP BY SESSION_ID,ID

Jack Vamvas
--------------------
http://www.ITjobfeed.com
Go to Top of Page
   

- Advertisement -