sql server - TSQL decision statement -
i have view select number of fields can see. main focus of query though xro_roles.start_page. field holds start page link stored procedure reference. have second table holds links (this table called branches). if dbo.people.role_id = 1 need use branches table start_page column. if role_id not 1 should reference xro_roles.start_page.
i not sure how use case statement in scenario. if not giving enough information let me know.
thanks
select dbo.people.people_id, dbo.people.role_id, dbo.people.company_id, dbo.xro_roles.start_page, dbo.areas.area_id, dbo.areas right outer join dbo.divisions on dbo.areas.area_id = dbo.divisions.area_id right outer join dbo.people left outer join dbo.xro_roles on dbo.people.role_id = dbo.xro_roles.role_id on dbo.divisions.division_id = dbo.people.division_id left outer join dbo.companies on dbo.people.company_id = dbo.companies.company_id left outer join dbo.xro_time_zones on dbo.people.time_zone_id = dbo.xro_time_zones.time_zone_id left outer join dbo.branches on dbo.branches.branch_id = dbo.people.division_id (dbo.people.status_id in (0, 1, 4))
put case when
in select
portion of query:
select dbo.people.people_id , dbo.people.role_id , dbo.people.company_id , case when dbo.people.role_id = 1 dbo.branches.start_page -- assuming column name "start_page" else dbo.xro_roles.start_page end some_field_name , dbo.areas.area_id dbo.areas right outer join dbo.divisions on dbo.areas.area_id = dbo.divisions.area_id right outer join dbo.people on dbo.divisions.division_id = dbo.people.division_id left outer join dbo.xro_roles on dbo.people.role_id = dbo.xro_roles.role_id left outer join dbo.companies on dbo.people.company_id = dbo.companies.company_id left outer join dbo.xro_time_zones on dbo.people.time_zone_id = dbo.xro_time_zones.time_zone_id left outer join dbo.branches on dbo.branches.branch_id = dbo.people.division_id dbo.people.status_id in (0, 1, 4)
you use coalesce
instead of case when
depending on how set joins, although may not give desired results if dbo.people.status_id
not 1
in other cases:
select dbo.people.people_id , dbo.people.role_id , dbo.people.company_id , coalesce(dbo.branches.start_page, dbo.xro_roles.start_page) some_field_name , dbo.areas.area_id dbo.areas right outer join dbo.divisions on dbo.areas.area_id = dbo.divisions.area_id right outer join dbo.people on dbo.divisions.division_id = dbo.people.division_id left outer join dbo.xro_roles on dbo.people.role_id = dbo.xro_roles.role_id left outer join dbo.companies on dbo.people.company_id = dbo.companies.company_id left outer join dbo.xro_time_zones on dbo.people.time_zone_id = dbo.xro_time_zones.time_zone_id left outer join dbo.branches on dbo.branches.branch_id = dbo.people.division_id , dbo.people.status_id = 1 -- join may not want , included example. dbo.people.status_id in (0, 1, 4)
Comments
Post a Comment