SELECT DATE,VALUE1=SUM(ISNULL(VALUE1,0)),VALUE2=SUM(ISNULL(VALUE2,0)) FROM TB GROUP BY DATE
select Date, value1=max(case when classid=0 then value1), value2=max(case when classid=1 then value2) from T group by Date
select convert(varchar(10),date,120) date, max(value1) value1, max(value2) value2 from tb group by convert(varchar(10),date,120)
declare @tb table(classid int,date datetime,v1 varchar(10),v2 varchar(10)) insert into @tb select 0,'2007-03-26','.9300',null insert into @tb select 0,'2007-06-28','.7800',null insert into @tb select 0,'2007-09-25','.7600',null insert into @tb select 0,'2007-12-20','.3600',null insert into @tb select 1,'2007-03-26',null,'.9800' insert into @tb select 1,'2007-06-28',null,'.8300' insert into @tb select 1,'2007-09-25',null,'.9600' insert into @tb select 1,'2007-12-20',null,'.6900'select a.date,a.v1,b.v2 from @tb a join @tb b on convert(varchar(10),a.date,120)=convert(varchar(10),b.date,120) where a.v2 is null and b.v1 is nulldate v1 v2 2007-03-26 00:00:00.000 .9300 .9800 2007-06-28 00:00:00.000 .7800 .8300 2007-09-25 00:00:00.000 .7600 .9600 2007-12-20 00:00:00.000 .3600 .6900
SQL CODE declare @t table(date_time datetime,value1 float,value2 float) insert @t select '2007-03-26', .9300, NULL union all select '2007-06-28', .7800, NULL union all select '2007-09-25', .7600, NULL union all select '2007-12-20', .3600, NULL union all select '2007-03-26', NULL, .9800 union all select '2007-06-28', NULL, .8300 union all select '2007-09-25', NULL, .9600 union all select '2007-12-20', NULL, .6900 select date_time,sum(value1),sum(value2) from @t group by date_time
create table tb(classid int,date datetime,v1 varchar(10),v2 varchar(10)) insert into tb select 0,'2007-03-26','.9300',null insert into tb select 0,'2007-06-28','.7800',null insert into tb select 0,'2007-09-25','.7600',null insert into tb select 0,'2007-12-20','.3600',null insert into tb select 1,'2007-03-26',null,'.9800' insert into tb select 1,'2007-06-28',null,'.8300' insert into tb select 1,'2007-09-25',null,'.9600' insert into tb select 1,'2007-12-20',null,'.6900' --方法1 select convert(varchar(10),a.date,120) as date,a.v1,b.v2 from tb a join tb b on convert(varchar(10),a.date,120)=convert(varchar(10),b.date,120) where a.v2 is null and b.v1 is null --方法2 select convert(varchar(10),date,120) as date,max(v1) as v1,max(v2) as v2 from tb group by convert(varchar(10),date,120)date v1 v2 2007-03-26 .9300 .9800 2007-06-28 .7800 .8300 2007-09-25 .7600 .9600 2007-12-20 .3600 .6900
declare @tb table(classid int,date datetime,VALUE1 decimal(10,4),VALUE2 decimal(10,4)) insert into @tb select 0,'2007-03-26',0.9300,null union all select 0,'2007-06-28',0.7800,null union all select 0,'2007-09-25',0.7600,null union all select 0,'2007-12-20',0.3600,null union all select 1,'2007-03-26',null,0.9800 union all select 1,'2007-06-28',null,0.8300 union all select 1,'2007-09-25',null,0.9600 union all select 1,'2007-12-20',null,0.6900 SELECT DATE,VALUE1=SUM(ISNULL(VALUE1,0)),VALUE2=SUM(ISNULL(VALUE2,0)) FROM @tb GROUP BY DATE/* DATE VALUE1 VALUE2 ------------------------------------------------------ ---------------------------------------- ---------------------------------------- 2007-03-26 00:00:00.000 .9300 .9800 2007-06-28 00:00:00.000 .7800 .8300 2007-09-25 00:00:00.000 .7600 .9600 2007-12-20 00:00:00.000 .3600 .6900(所影响的行数为 4 行) /*
value1=max(case when classid=0 then value1),
value2=max(case when classid=1 then value2)
from
T
group by Date
max(value1) value1,
max(value2) value2
from tb
group by convert(varchar(10),date,120)
insert into @tb select 0,'2007-03-26','.9300',null
insert into @tb select 0,'2007-06-28','.7800',null
insert into @tb select 0,'2007-09-25','.7600',null
insert into @tb select 0,'2007-12-20','.3600',null
insert into @tb select 1,'2007-03-26',null,'.9800'
insert into @tb select 1,'2007-06-28',null,'.8300'
insert into @tb select 1,'2007-09-25',null,'.9600'
insert into @tb select 1,'2007-12-20',null,'.6900'select a.date,a.v1,b.v2
from @tb a join @tb b on convert(varchar(10),a.date,120)=convert(varchar(10),b.date,120)
where a.v2 is null and b.v1 is nulldate v1 v2
2007-03-26 00:00:00.000 .9300 .9800
2007-06-28 00:00:00.000 .7800 .8300
2007-09-25 00:00:00.000 .7600 .9600
2007-12-20 00:00:00.000 .3600 .6900
declare @t table(date_time datetime,value1 float,value2 float)
insert @t select
'2007-03-26', .9300, NULL union all select
'2007-06-28', .7800, NULL union all select
'2007-09-25', .7600, NULL union all select
'2007-12-20', .3600, NULL union all select
'2007-03-26', NULL, .9800 union all select
'2007-06-28', NULL, .8300 union all select
'2007-09-25', NULL, .9600 union all select
'2007-12-20', NULL, .6900
select date_time,sum(value1),sum(value2) from @t group by date_time
insert into tb select 0,'2007-03-26','.9300',null
insert into tb select 0,'2007-06-28','.7800',null
insert into tb select 0,'2007-09-25','.7600',null
insert into tb select 0,'2007-12-20','.3600',null
insert into tb select 1,'2007-03-26',null,'.9800'
insert into tb select 1,'2007-06-28',null,'.8300'
insert into tb select 1,'2007-09-25',null,'.9600'
insert into tb select 1,'2007-12-20',null,'.6900'
--方法1
select convert(varchar(10),a.date,120) as date,a.v1,b.v2
from tb a join tb b on convert(varchar(10),a.date,120)=convert(varchar(10),b.date,120)
where a.v2 is null and b.v1 is null
--方法2
select convert(varchar(10),date,120) as date,max(v1) as v1,max(v2) as v2
from tb
group by convert(varchar(10),date,120)date v1 v2
2007-03-26 .9300 .9800
2007-06-28 .7800 .8300
2007-09-25 .7600 .9600
2007-12-20 .3600 .6900
insert into @tb select 0,'2007-03-26',0.9300,null
union all select 0,'2007-06-28',0.7800,null
union all select 0,'2007-09-25',0.7600,null
union all select 0,'2007-12-20',0.3600,null
union all select 1,'2007-03-26',null,0.9800
union all select 1,'2007-06-28',null,0.8300
union all select 1,'2007-09-25',null,0.9600
union all select 1,'2007-12-20',null,0.6900
SELECT DATE,VALUE1=SUM(ISNULL(VALUE1,0)),VALUE2=SUM(ISNULL(VALUE2,0)) FROM @tb GROUP BY DATE/*
DATE VALUE1 VALUE2
------------------------------------------------------ ---------------------------------------- ----------------------------------------
2007-03-26 00:00:00.000 .9300 .9800
2007-06-28 00:00:00.000 .7800 .8300
2007-09-25 00:00:00.000 .7600 .9600
2007-12-20 00:00:00.000 .3600 .6900(所影响的行数为 4 行)
/*