如何在SQL Server

时间:2018-03-06 18:45:33

标签: sql sql-server datamart

我完成了这个任务,我必须从care_team_member_name表中选择DIM_CARE_TEAM_MEMBER。条件是对于特定care_team_member_Engagement_id,我们需要选择活动的成员,即其care_team_member_end_date为空,对于非活动成员,我们为该特定{{1}选择最大care_team_member_end_date }}

我能够使用以下查询实现第二部分

care_team_member_Engagement_id

但是我无法实现我选择select [care_team_member_name], [care_team_member_Engagement_id], [care_team_member_end_date], B.A from (select care_team_member_name,care_team_member_end_date, [care_team_member_Engagement_id], row_number() over(partition by [care_team_member_Engagement_id] order by care_team_member_end_date) A from DIM_CARE_TEAM_MEMBER) B where B.A = 1 作为特定参与ID

的第一部分

例如:

care_team_member_name

从这张表中,我必须选择结束日期为空的名称

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了这个问题,但请尝试一下:

SELECT tm.*
FROM (
    SELECT care_team_member_Engagement_id id, 
         NULLIF(MAX(coalesce(care_team_member_end_date,'29991231')),'29991231') end_date 
    FROM DIM_CARE_TEAM_MEMBER
    GROUP BY care_team_member_Engagement_id 
) ids
INNER JOIN DIM_CARE_TEAM_MEMBER tm ON
        tm.care_team_member_Engagement_id = ids.id 
    AND tm.care_team_member_end_date = ids.end_date