TSQL决策声明

时间:2013-09-03 20:05:55

标签: sql-server sql-server-2008 tsql

我有一个视图,您可以看到选择多个字段。但查询的主要焦点是xro_roles.start_page。此字段包含要引用的存储过程的起始页链接。但我有一个第二个表,也有一些链接(这个表称为分支)。如果dbo.people.role_id = 1,那么我需要它来使用分支表作为start_page列。如果role_id不是1,那么它应该引用xro_roles.start_page。

我不确定如何在这种情况下使用CASE语句。如果我没有提供足够的信息,请告诉我。

由于

 SELECT dbo.people.people_id, dbo.people.role_id, dbo.people.company_id, 
           dbo.xro_roles.start_page, dbo.areas.area_id,
FROM 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
WHERE (dbo.people.status_id IN (0, 1, 4))

3 个答案:

答案 0 :(得分:2)

首先清理你的sql。使用表别名!不要混合左右外连接。在执行无序连接的连接时使用括号(尽管在这种情况下,您可以,如下所示,重新组织连接,以便不需要)

假设分支表中的字段名为start_page,请尝试以下操作: (第三行有案例陈述)

Select p.people_id, p.role_id, p.company_id, p.department_id, p.lang_id, 
    p.password, p.user_name, p.display_name, r.start_page, p.first_name, 
    case p.role_id when 1 then b.start_page else r.frame_start_page end startPage,, 
    p.full_name, r.role, p.security_question, 
    dbo.sp_getTimeZoneUSEstOffset(GETDATE(), z.time_zone_id) offset, 
    r.timeout, p.show_help_tips, p.currency_id, p.status_id, p.last_name, 
    p.country_id, a.region_id, a.area_id, p.group_id, d.division_id, 
    p.section_id, p.unit_id, ISNULL(d.stylesheet, c.stylesheet) stylesheet, 
    p.owner_company_id, p.owner_country_id, p.owner_division_id, p.owner_department_id, 
    ISNULL(d.logo_image_file_name, c.logo_image_file_name) logo_image_file, 
    p.subunit_id, r.role_type, p.onsite_company_id, p.onsite_division_id, 
    p.onsite_department_id, p.email_confirm, p.email_notify, c.company_type_id, 
    p.time_zone_id, c.company, p.date_updated_password, p.building_id,
    p.failed_logins, p.login_locked, c.logoff_page, p.external_id, 
    p.onsite_building_id, p.kpi_all_access KAA, p.data_entry_mode, 
    c.force_password_change, c.password_change_days,
    c.allow_password_recycle, c.login_attempts_allowed, c.custom_vocab, 
    p.team_id, p.subteam_id, p.btn_click_disable, 
    dbo.sp_getTimeZoneABR(GETDATE(), z.time_zone_id) tmz_abr
From dbo.people p 
    Left Join dbo.xro_roles r On r.role_id = p.role_id
    Left Join dbo.divisions d On d.division_id = p.division_id 
    Left Join dbo.areas a On a.area_id = d.area_id 
    Left Join dbo.companies c ON c.company_id = p.company_id 
    Left Join dbo.xro_time_zones z ON z.time_zone_id = p.time_zone_id
    Left Join dbo.branches b ON b.branch_id = p.division_id
Where p.status_id IN (0, 1, 4)

答案 1 :(得分:2)

CASE WHEN放入查询的SELECT部分:

SELECT  dbo.people.people_id
        , dbo.people.role_id
        , dbo.people.company_id
        , CASE WHEN dbo.people.role_id = 1
            THEN    dbo.branches.start_page     -- assuming the column name is "start_page"
            ELSE    dbo.xro_roles.start_page
          END AS some_field_name
        , dbo.areas.area_id
FROM    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
WHERE   dbo.people.status_id IN (0, 1, 4)

您还可以使用COALESCE代替CASE WHEN,具体取决于您设置联接的方式,但如果dbo.people.status_id不是{{1},则可能无法获得所需的结果在其他情况下:

1

答案 2 :(得分:1)

  (case when people.role_id=1 Then Branches.start_page 
  else xro_roles.start_page end)<ColumnName>