Coalesce on inner queries

  • 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.
    ********************************************************/

  • 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, MVP, M.Sc (Comp Sci)
    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
  • 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.
    ********************************************************/

  • I'm assuming you didn't read that article. Table definitions, sample data and desired output please.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    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
  • 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.
    ********************************************************/

  • 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, MVP, M.Sc (Comp Sci)
    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

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply