22,209
社区成员
发帖
与我相关
我的任务
分享
select segment,process,line,wrkstation,model,item,sord,sftdate,sftseq,shift,periodseq,period,
sum(gdqty) 'gdqty',
sum(ngqty) 'ngqty'
from [表名]
group by segment,process,line,wrkstation,model,item,sord,sftdate,sftseq,shift,periodseq,period
select periodseq,
sum(gdqty) 'gdqty',
sum(ngqty) 'ngqty'
from [表名]
group by periodseq
[/quote]
大神,按照您的办法,表中只保留了periodseq 还有另外两个列。。。我想要求全部的列,跟图片的中的一样的,只是做了累加了。。。
我想筛查出来的表包含所有的字段。。。。但是经过累加了。。。
select periodseq,
sum(gdqty) 'gdqty',
sum(ngqty) 'ngqty'
from [表名]
group by periodseq
create table wj
(字段A int, 字段B int)
insert into wj
select 1, 1 union all
select 2, 2 union all
select 2, 5 union all
select 1, 2 union all
select 4, 4
select distinct a.字段A,
(select sum(b.字段B) from wj b
where b.字段A=a.字段A) '字段B'
from wj a
/*
字段A 字段B
----------- -----------
1 3
2 7
4 4
(3 row(s) affected)
*/
select [字段A],sum([字段B])
from [表名]
group by [字段A]
;with tb(字段A,字段B) as
(
select 1,1
union all select 2,2
union all select 2,5
union all select 1,2
union all select 4,4
)
select 字段A,SUM(字段B) as 字段B
from tb
group by 字段A
/*
字段A 字段B
1 3
2 7
4 4
*/
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-09-12 15:17:19
-- Version:
-- Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64)
-- Jun 10 2013 20:09:10
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([字段A] int,[字段B] int)
insert [huang]
select 1,1 union all
select 2,2 union all
select 2,5 union all
select 1,2 union all
select 4,4
--------------开始查询--------------------------
select 字段A,sum(字段B)字段B
from [huang] group by 字段A
----------------结果----------------------------
/*
字段A 字段B
----------- -----------
1 3
2 7
4 4
*/