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 2008 Forums
 Transact-SQL (2008)
 specific select , please help me

Author  Topic 

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-08-08 : 13:12:53


hi there i need to do a select but i have 2 possible cases

and i need your help please

first case

i have no problem with this case

table called "products"

idproduct name
1 aple
2 cherry
3 strawberry

this case is easy beacuase i do a select this way and i get all the items

select idproduct , name
from products

and i get this

idproduct name
1 aple
2 cherry
3 strawberry


now i show you the other case when in my table products where i dont have a record with idproduct=1

table called "products"

idproduct name

2 cherry
3 strawberry

and i need to do a select that gives me this result


idproduct name
1 strawberry
2 cherry

as you can see ( that i need its when the idproduct=1 doesnt exists the idproduct = 3 takes the idproduct=1)

any ideas please

many thanks in advanced

kind regards

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-08 : 13:49:07
[code]
select idproduct,name
from
(
select case when sum(case when idproduct=1 then 1 else 0 end) over () = 0 and idproduct=3 then 1 else idproduct end as idproduct,
name
from products
)p
where idproduct=1
[/code]

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

Go to Top of Page

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-08-08 : 18:31:59
thanks a lot

great query

thanks for always helping people
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-08 : 18:56:59
welcome

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

Go to Top of Page
   

- Advertisement -