create table test
(
时间 int,
数量1 int,
数量2 int
)
insert into test select 6,10,100
insert into test select 7,20,200
insert into test select 6,30,300
insert into test select 7,40,400
select * from test
drop table test
---要得到这样的报表
时间 数量1 数量2
6 40 400
7 60 600
..........
我只能写成这样
select isnull(sum(数量1),0)from test where 时间=6
------解决方案--------------------
select 时间,sum(数量1), sum(数量2)from test
group by 时间
------解决方案--------------------
- SQL code
create table test(时间 int,数量1 int,数量2 int)insert into test select 6,10,100insert into test select 7,20,200insert into test select 6,30,300insert into test select 7,40,400select * from testselect 时间,sum(isnull(数量1,0))数量1,sum(isnull(数量2,0))数量2 from testgroup by 时间/*时间,数量1,数量26,40,4007,60,600(2 行受影响)*/drop table test
------解决方案--------------------
- SQL code
declare @T table (时间 int,数量1 int,数量2 int)insert into @T select 6,10,100insert into @T select 7,20,200insert into @T select 6,30,300insert into @T select 7,40,400select * from @Tselect t.时间,数量1=SUM(t.数量1),数量2=SUM(t.数量2)from @T t group by t.时间
------解决方案--------------------
- SQL code
select 时间,sum(数量1)as 数量1, sum(数量2)as 数量2 from test group by 时间
------解决方案--------------------
- SQL code
select 时间,sum(数量1) as 数量1,sum(数量2) as 数量2from test group by 时间/*时间 数量1 数量2----------- ----------- -----------6 40 4007 60 600*/
------解决方案--------------------
/*
create table test
(
时间 int,
数量1 int,
数量2 int
)
insert into test select 6,10,100
insert into test select 7,20,200
insert into test select 6,30,300
insert into test select 7,40,400
select * from test
drop table test
---要得到这样的报表
时间 数量1 数量2
6 40 400
7 60 600
*/
go
if object_id('test')is not null
drop table test
go
create table test
(
时间 int,
数量1 int,
数量2 int
)
go
insert into test select 6,10,100
insert into test select 7,20,200
insert into test select 6,30,300
insert into test select 7,40,400
select 时间,SUM(ISNULL(数量1,0)) as 数量1,
SUM(ISNULL(数量2,0)) as 数量2 from test
group by 时间
/*
时间 数量1 数量2
6 40 400
7 60 600
*/
------解决方案--------------------
------解决方案--------------------
select 时间,sum(数量1),sum(数量2) from test group by 时间