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)
 Return Distinct Value

Author  Topic 

steamngn
Constraint Violating Yak Guru

306 Posts

Posted - 2009-05-09 : 15:51:14
Hi all,
I have a table with two columns like so:

STK_LOC_ID | LOC_GRP_ID
-----------------------
NULL | MOBILE
REMOTE | MOBILE
MAIN | TESTGROUP
REMOTE | TESTGROUP

What I need to do is
select distinct(loc_grp_id)
where stk_loc_id does not have NULL

IE return just TESTGROUP.
Can someone give some insight for me?
TIA,
Andy

There's never enough time to type code right,
but always enough time for a hotfix...

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-09 : 16:13:50
select loc_grp_id
from table1
group by loc_grp_id
having max(case when stk_loc_id is null then 1 else 0 end) = 0



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

steamngn
Constraint Violating Yak Guru

306 Posts

Posted - 2009-05-09 : 16:20:58
Peso!
I haven't posted on this site in quite some time, and the last time I did you saved the day for me! Now again I tip my hat to your eloquent and functioning code!
Thank you!
Andy

There's never enough time to type code right,
but always enough time for a hotfix...
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-09 : 19:25:24
Thenk you for the feedback.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-10 : 02:35:06
Another method

SELECT DISTINCT t.LOC_GRP_ID
FROM YourTable t
OUTER APPLY (SELECT COUNT(*) AS NullCnt
FROM YourTable
WHERE LOC_GRP_ID=t.LOC_GRP_ID
AND STK_LOC_ID IS NULL) t1
WHERE ISNULL(NullCnt,0)=0


Also

select loc_grp_id
from table1
group by loc_grp_id
having count(*) - count(STK_LOC_ID)=0
Go to Top of Page
   

- Advertisement -