日期:2014-05-18 浏览次数:20750 次
create table t1(name varchar(10),[date] datetime,value int) insert t1 select 'aaa', '2012-01-01', 1 union all select 'bbb', '2012-01-01', 2 union all select 'aaa', '2012-01-02', 3 union all select 'bbb', '2012-01-02', 4 union all select 'ccc', '2012-01-02', 5 union all select 'aaa', '2012-01-03', 6 union all select 'bbb', '2012-01-03', 7 union all select 'aaa', '2012-01-04', 8 union all select 'bbb', '2012-01-04', 9 go ;with cte as( select name='aaa',[date],vaule=null from t1 union all select name='bbb',[date],vaule=null from t1 union all select name='ccc',[date],vaule=null from t1 union all select * from t1 ) select name,[date],value=MAX(vaule) from cte a group by name,[date] /* name date vaule --- --- ---- aaa 2012-01-01 00:00:00.000 1 bbb 2012-01-01 00:00:00.000 2 ccc 2012-01-01 00:00:00.000 NULL aaa 2012-01-02 00:00:00.000 3 bbb 2012-01-02 00:00:00.000 4 ccc 2012-01-02 00:00:00.000 5 aaa 2012-01-03 00:00:00.000 6 bbb 2012-01-03 00:00:00.000 7 ccc 2012-01-03 00:00:00.000 NULL aaa 2012-01-04 00:00:00.000 8 bbb 2012-01-04 00:00:00.000 9 ccc 2012-01-04 00:00:00.000 NULL */ go drop table t1