0

如您所见,我有一个选择多个字段的视图。查询的主要焦点是 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))
4

3 回答 3

2

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

假设分支表中的字段名为start_page,然后试试这个:(第三行有 case 语句)

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)
于 2013-09-03T20:24:54.697 回答
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)

您也可以使用 aCOALESCE而不是 a ,CASE WHEN具体取决于您设置联接的方式,但如果dbo.people.status_id不是1在其他情况下,这可能不会为您提供所需的结果:

SELECT  dbo.people.people_id
        , dbo.people.role_id
        , dbo.people.company_id
        , COALESCE(dbo.branches.start_page, dbo.xro_roles.start_page) 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
            AND dbo.people.status_id = 1    -- This JOIN may not do what you want and is included only as an example.
WHERE   dbo.people.status_id IN (0, 1, 4)
于 2013-09-03T20:27:59.297 回答
1
  (case when people.role_id=1 Then Branches.start_page 
  else xro_roles.start_page end)<ColumnName>
于 2013-09-03T20:11:29.913 回答