SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On



Coalesce on inner queries Expand / Collapse
Author
Message
Posted Monday, January 25, 2010 12:00 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, February 16, 2010 3:45 AM
Points: 39, Visits: 116
Hi All,

I have a where condition like

where Fk_Event in
(COALESCE((select fk_event from workscope_event where Fk_user=@userId),Fk_Event)))

actually my requirement is ..if the user is having event it should get only those events other wise all the events.

here the problem is i may get more than one fk_event from inner query , please tell me how can i solve this...Please try to avoid if else statements coz already my query is depending on so many conditions.
Post #852853
Posted Monday, January 25, 2010 12:07 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:06 AM
Points: 17,125, Visits: 12,226
Please post table definitions, sample data and desired output. Read this to see the best way to post this to get quick responses.
http://www.sqlservercentral.com/articles/Best+Practices/61537/

If may also be worth reading http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/ and http://sqlinthewild.co.za/index.php/2009/09/15/multiple-execution-paths/



Gail Shaw

We walk in the dark places no others will enter
We stand on the bridge and none may pass

Post #852855
Posted Monday, January 25, 2010 12:31 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, February 16, 2010 3:45 AM
Points: 39, Visits: 116
Hi,

I have a table workscope_event where i will be having event id and user id mappings,

say for user id --> 1 mapped to events 1,2,3 where i have total 10 events

now i need a query such that if user with Id 1 login to the system it should show only 1,2,3 events but if user with other id logs into the system it shud give all the 10 events.

so as i told from workscope_events i will get mappings of user and event where i need to filter from event table with those events of workwscope_events depending on user login.
Post #852864
Posted Monday, January 25, 2010 12:38 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:06 AM
Points: 17,125, Visits: 12,226
I'm assuming you didn't read that article. Table definitions, sample data and desired output please.


Gail Shaw

We walk in the dark places no others will enter
We stand on the bridge and none may pass

Post #852865
Posted Monday, January 25, 2010 12:57 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, February 16, 2010 3:45 AM
Points: 39, Visits: 116
k my tables are

package_event (Id int,name nvarchar(50),fk_event int)
workscope_event (fk_user int,fk_event int)

data : Package_Event

Fk_Package, Fk_Event
1 1
2 2
3 3
4 4
5 5
6 6
6 1

data : workscope_event

Fk_User, Fk_Event
1 1
1 2
1 3

now if the user 1 logs in then the out put should be

Fk_Event , Fk_Package
1 1
2 2
3 3

for other users all the events and their packages should come.
Post #852868
Posted Monday, January 25, 2010 1:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:06 AM
Points: 17,125, Visits: 12,226
I'm assuming you didn't read that article.

Ok, so based on that sample data, what do you want the query to return. (actual values please, not a description)



Gail Shaw

We walk in the dark places no others will enter
We stand on the bridge and none may pass

Post #852869
« Prev Topic | Next Topic »


Permissions Expand / Collapse