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
 General SQL Server Forums
 New to SQL Server Programming
 Case with count -- help tsql

Author  Topic 

Nylren
Starting Member

7 Posts

Posted - 2011-02-19 : 18:42:08
Hi, guys,

I cant figure out how to solve this sql. Please, assist me :)

THANKS


select (case
when count(*) = 1 then Url
else select t2.Url from Site t2 where t2.ID = 2 and t2.Nome = 'PackUrl' end) Url
from Site t
where t.ID = 2

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2011-02-20 : 06:12:40
Not sure what are you trying to achieve. It looks like you want to display the only url of site specified. In case there are multiple urls from the site you want to display one named 'PackUrl'. However, what if there are multiple urls and there is no one named 'PackUrl'?

Mirko

My blog: http://mirko-marovic-eng.blogspot.com/
Go to Top of Page

Nylren
Starting Member

7 Posts

Posted - 2011-02-20 : 09:02:44
You got the logic right! That's exactly what I need.

When inserting a new site, if there is more than 1 Url, the admin needs to enter the PackUrl.

Please, help me out
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2011-02-21 : 15:10:47
Assuming there is never more then one "PackUrl":

select MAX(case
when s.url is null then t.url
else s.url
end
) URL
from Site t
left join site s on t.id = s.id and s.nome = 'PackUrl'
where t.ID = 2



Mirko

My blog: http://mirko-marovic-eng.blogspot.com/
Go to Top of Page
   

- Advertisement -