日期:2014-05-17 浏览次数:20473 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-02-19 07:46:10
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[T]
if object_id('[T]') is not null drop table [T]
go
create table [T]([Cus_Mobile] bigint,[JOIN_DATE_1] datetime,[JOIN_DATE_2] datetime)
insert [T]
select 13800123545,'2012-01-01','2013-01-01' union all
select 13101055858,'2014-01-01','2014-02-01' union all
select 18912345678,null,'2013-01-02' union all
select 18102012345,null,null
--------------开始查询--------------------------
select [Cus_Mobile],CASE WHEN [JOIN_DATE_1]>=[JOIN_DATE_2] THEN [JOIN_DATE_1] ELSE [JOIN_DATE_2] END MAX_JOIN_DATE
from [T]
----------------结果----------------------------
/*
Cus_Mobile MAX_JOIN_DATE
-------------------- -----------------------
13800123545 2013-01-01 00:00:00.000
13101055858 2014-02-01 00:00:00.000
18912345678 2013-01-02 00:00:00.000
18102012345 NULL
*/