日期:2014-05-18  浏览次数:20526 次

帮忙写一下SQL!!!!



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,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



select 时间,sum(isnull(数量1,0))数量1,sum(isnull(数量2,0))数量2 from test
group by 时间
/*时间,数量1,数量2
6,40,400
7,60,600

(2 行受影响)
*/

drop table test

------解决方案--------------------
SQL code
declare @T table 
(
时间 int,
数量1 int,
数量2 int
)
insert into @T select 6,10,100
insert into @T select 7,20,200
insert into @T select 6,30,300
insert into @T select 7,40,400
select * from @T
select 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 数量2
from test group by 时间
/*
时间          数量1         数量2
----------- ----------- -----------
6           40          400
7           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
*/
------解决方案--------------------
探讨

SQL code
declare @T table
(
时间 int,
数量1 int,
数量2 int
)
insert into @T select 6,10,100
insert into @T select 7,20,200
insert into @T select 6,30,300
insert into @T select 7,40,400
select * from @T
s……

------解决方案--------------------
select 时间,sum(数量1),sum(数量2) from test group by 时间