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)
 filter but also get the ones not associated

Author  Topic 

magmo
Aged Yak Warrior

558 Posts

Posted - 2013-03-12 : 06:45:00
Hi

I have the following query


SELECT DISTINCT TOP (100) PERCENT dbo.tbl_WebbLanguage.LanguageName, dbo.tbl_WebbLanguage.LangID
FROM dbo.tbl_LanguageAccess INNER JOIN
dbo.tbl_WebbLanguage ON dbo.tbl_LanguageAccess.LangID = dbo.tbl_WebbLanguage.LangID
ORDER BY dbo.tbl_WebbLanguage.LanguageName



This give me all the distinct language names which is fine, but in dbo.tbl_LanguageAccess there is also a column called "LoginID" which I would like to use, so if I filter on Login = 3 I should return all the languages that are associated with that LoginID, but also the ones that are not, the ones that are associated should be marked as "InUse", I tried to add this...


SELECT DISTINCT TOP (100) PERCENT dbo.tbl_WebbLanguage.LanguageName, dbo.tbl_WebbLanguage.LangID
FROM dbo.tbl_LanguageAccess INNER JOIN
dbo.tbl_WebbLanguage ON dbo.tbl_LanguageAccess.LangID = dbo.tbl_WebbLanguage.LangID
WHERE (dbo.tbl_LanguageAccess.LoginID = 1) OR
(dbo.tbl_LanguageAccess.LoginID IS NULL)
ORDER BY dbo.tbl_WebbLanguage.LanguageName



But that doesn't give me any rows at all.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-12 : 06:48:31
[code]
SELECT DISTINCT TOP (100) PERCENT dbo.tbl_WebbLanguage.LanguageName, dbo.tbl_WebbLanguage.LangID,
CASE WHEN dbo.tbl_LanguageAccess.LoginID = 1 THEN 'InUse' END
FROM dbo.tbl_LanguageAccess INNER JOIN
dbo.tbl_WebbLanguage ON dbo.tbl_LanguageAccess.LangID = dbo.tbl_WebbLanguage.LangID

ORDER BY dbo.tbl_WebbLanguage.LanguageName
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2013-03-12 : 06:55:34
Thanks for a fast reply, one thing though. If there is associated languages I get 2 results for each languagename, one where "InUse" is true and one where "InUse" is null..
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-12 : 07:24:29
SELECT DISTINCT TOP (100) PERCENT dbo.tbl_WebbLanguage.LanguageName, dbo.tbl_WebbLanguage.LangID,
MAX(CASE WHEN dbo.tbl_LanguageAccess.LoginID = 1 THEN 'InUse' END) OVER(PARTITION BY dbo.tbl_WebbLanguage.LangID)

FROM ..........


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-12 : 07:52:59
quote:
Originally posted by magmo

Thanks for a fast reply, one thing though. If there is associated languages I get 2 results for each languagename, one where "InUse" is true and one where "InUse" is null..


what do you mean by associated languages. show some sample data to illustrate your issue

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2013-03-12 : 08:18:00
quote:
Originally posted by bandi

SELECT DISTINCT TOP (100) PERCENT dbo.tbl_WebbLanguage.LanguageName, dbo.tbl_WebbLanguage.LangID,
MAX(CASE WHEN dbo.tbl_LanguageAccess.LoginID = 1 THEN 'InUse' END) OVER(PARTITION BY dbo.tbl_WebbLanguage.LangID)

FROM ..........


--
Chandu




Excellent, Thank you very much!
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-12 : 08:18:43
quote:
Originally posted by magmo

quote:
Originally posted by bandi

SELECT DISTINCT TOP (100) PERCENT dbo.tbl_WebbLanguage.LanguageName, dbo.tbl_WebbLanguage.LangID,
MAX(CASE WHEN dbo.tbl_LanguageAccess.LoginID = 1 THEN 'InUse' END) OVER(PARTITION BY dbo.tbl_WebbLanguage.LangID)

FROM ..........
--
Chandu

Excellent, Thank you very much!

Welcome

--
Chandu
Go to Top of Page
   

- Advertisement -