获取最小start_Date和最大End_Date

时间:2017-03-02 06:52:54

标签: sql-server sql-server-2008

我正在使用以下查询:

select * from (Select 


                        [CLIENT_ID],
                        [CLIENT_NAME],
                        [SUPER_GROUP_ID],
                        [SUPER_GROUP],
                        [MASTER_GROUP_ID],
                        [MASTER_GROUP],
                        [SALES_GROUP_ID],
                        [SALES_GROUP],
                        [VOTING_ENTITY_ID], 
                        [VOTING_ENTITY],
                        [COVERAGE_SPECIALITY],
                        [COVERAGE_FUND],
                        [MCM_CLASSIFICATION],
                        [START_DATE],
                        IsNull([END_DATE], GetDate())  as END_DATE,
                        ROW_NUMBER()OVER (partition by CLIENT_ID order by START_DATE desc, END_DATE  ) as RNUM
                FROM [mi_s_data].[dbo].[BI_CLIENT_CONTACT_MAPPING]

                ) CCMAP2
                where CCMAP2.RNUM =1 and ccmap2.CLIENT_ID= '1-12W68I'

我想为START_DATE最小且END_DATE最大的记录分配RNUM

2 个答案:

答案 0 :(得分:0)

只需更改START_DATE和END_DATE的顺序,希望这有助于

ROW_NUMBER()OVER (partition by CLIENT_ID order by START_DATE, END_DATE desc ) as RNUM

答案 1 :(得分:0)

 SELECT                            
                             [CLIENT_ID],
                             MIN([START_DATE]) as START_DATE,
                             Max(IsNull([END_DATE], GetDate())) as END_DATE                     
                        FROM [mi_s_data].[dbo].[BI_CLIENT_CONTACT_MAPPING]              

    GROUP BY 
                              [CLIENT_ID]