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


Add to briefcase

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: Friday, June 28, 2013 5:28 AM
Points: 42, Visits: 137
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.


/********************************************************
Technology is just a tool,in terms of getting the kids working together and motivating them,the teacher is the most important.
********************************************************/
Post #852853
Posted Monday, January 25, 2010 12:07 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:32 AM
Points: 40,445, Visits: 36,899
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
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one 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: Friday, June 28, 2013 5:28 AM
Points: 42, Visits: 137
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.


/********************************************************
Technology is just a tool,in terms of getting the kids working together and motivating them,the teacher is the most important.
********************************************************/
Post #852864
Posted Monday, January 25, 2010 12:38 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:32 AM
Points: 40,445, Visits: 36,899
I'm assuming you didn't read that article. Table definitions, sample data and desired output please.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one 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: Friday, June 28, 2013 5:28 AM
Points: 42, Visits: 137
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.


/********************************************************
Technology is just a tool,in terms of getting the kids working together and motivating them,the teacher is the most important.
********************************************************/
Post #852868
Posted Monday, January 25, 2010 1:06 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:32 AM
Points: 40,445, Visits: 36,899
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
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

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

Post #852869
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse