帮忙写一下SQL!!!!

wjd_test1 2012-02-23 04:51:46



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
...全文
170 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
寡亾 2012-02-25
  • 打赏
  • 举报
回复
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 #test.时间,SUM(#test.数量1),SUM(#test.数量2) from #test group by 时间
drop table #test
07007 2012-02-25
  • 打赏
  • 举报
回复
select 时间,sum(数量1),sum(数量2) from test group by 时间
hepeng_8 2012-02-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wfkmu 的回复:]

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……
[/Quote]
可用
  • 打赏
  • 举报
回复
/*
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
*/
叶子 2012-02-24
  • 打赏
  • 举报
回复

select
时间,sum(数量1) as 数量1,sum(数量2) as 数量2
from test group by 时间
/*
时间 数量1 数量2
----------- ----------- -----------
6 40 400
7 60 600
*/
  • 打赏
  • 举报
回复

select 时间,sum(数量1)as 数量1, sum(数量2)as 数量2
from test
group by 时间
hadis1230 2012-02-24
  • 打赏
  • 举报
回复
3楼方法可行
wfkmu 2012-02-23
  • 打赏
  • 举报
回复
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.时间


时间 数量1 数量2
----------- ----------- -----------
6 40 400
7 60 600

(2 行受影响)
梦想鳖 2012-02-23
  • 打赏
  • 举报
回复
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
mayuanf 2012-02-23
  • 打赏
  • 举报
回复
select 时间,sum(数量1), sum(数量2)from test
group by 时间

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧