求助,求和

zhanglin_5 2011-10-11 04:15:51
添加一个字段列达到求表中同一编号的总和作用,谢谢!
例如:原从表A中查出的结果如下
合同编号 物料 数量
111 B 1
112 B 2
112 C 1
达到目的:
合同编号 物料 数量 同编号总数量
111 B 1 1
112 B 2 3
112 C 1 3
...全文
194 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
areswang 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhanglin_5 的回复:]
引用 6 楼 areswang 的回复:

SQL code

create table #t(合同编号 int, 物料 varchar(10), 数量 int)
go
insert into #t
select 111, 'B', 1
union all
select 112, 'B', 2
union all
select 112, 'C', 1

select 合同……
[/Quote]
over除了跟分析函数一起使用外,还可以跟聚合函数一起使用,如SUM,COUNT,AVG等。
其实over不是关键,关键的是partition by后跟的字段是group by 的字段。
cxmcxm 2011-10-11
  • 打赏
  • 举报
回复
--设表名为a
select a.*,b.[数量] as [同编号总数量]
from a,(select [合同编号],sum([数量]) as [数量] from a group by [合同编号]) b
where a.[合同编号]=b.[合同编号]
勿勿 2011-10-11
  • 打赏
  • 举报
回复
select *,
同编号总数量=(select sum(数量) from 表A where 合同编号=a.合同编号)
from 表A a
zhanglin_5 2011-10-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 areswang 的回复:]

SQL code

create table #t(合同编号 int, 物料 varchar(10), 数量 int)
go
insert into #t
select 111, 'B', 1
union all
select 112, 'B', 2
union all
select 112, 'C', 1

select 合同编号, 物料, 数量,SUM(数量)over(partition……
[/Quote]


谢谢你的方法。
请教个问题,我在了解OVER关键字时,搜索出来的都说“over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用。”。很疑惑,这里明显没用分析函数,你能帮解惑下吗?我想了解这个关键字的用法,谢谢!
pengxuan 2011-10-11
  • 打赏
  • 举报
回复

if object_id('tb','U') is not null
drop table tb
go
create table tb
(
合同编号 int,
物料 varchar(10),
数量 int
)
go
insert into tb
select 111,'B',1 union all
select 112,'B',2 union all
select 112,'C',1
go
select *,同编号总数量=(select sum(数量) from tb where 合同编号=a.合同编号) from tb a
areswang 2011-10-11
  • 打赏
  • 举报
回复

create table #t(合同编号 int, 物料 varchar(10), 数量 int)
go
insert into #t
select 111, 'B', 1
union all
select 112, 'B', 2
union all
select 112, 'C', 1

select 合同编号, 物料, 数量,SUM(数量)over(partition by 合同编号 ) 同编号总数量 from #t

---------------
合同编号 物料 数量 同编号总数量
111 B 1 1
112 B 2 3
112 C 1 3
--小F-- 2011-10-11
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-10-11 16:24:40
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([合同编号] int,[物料] varchar(1),[数量] int)
insert [tb]
select 111,'B',1 union all
select 112,'B',2 union all
select 112,'C',1
--------------开始查询--------------------------
select
a.*,b.num as 同编号总数量
from
tb a,
(select 物料,sum( 数量) as num from tb group by 物料)b
where
a.物料=b.物料
----------------结果----------------------------
/* 合同编号 物料 数量 同编号总数量
----------- ---- ----------- -----------
111 B 1 3
112 B 2 3
112 C 1 1

(3 行受影响)

*/
--小F-- 2011-10-11
  • 打赏
  • 举报
回复
select
a.*,b.num as 同编号总数量
from
tb a,
(select 物料,sum( 数量) as num from tb group by 物料)b
where
a.物料=b.物料
-晴天 2011-10-11
  • 打赏
  • 举报
回复
create table tb(合同编号 int,物料 nvarchar(10),数量 int)
insert into tb select 111,'B',1
insert into tb select 112,'B',2
insert into tb select 112,'C',1
go
select *,(select sum(数量) from tb where 合同编号=a.合同编号) as 同编号总数量 from tb a
/*
合同编号 物料 数量 同编号总数量
----------- ---------- ----------- -----------
111 B 1 1
112 B 2 3
112 C 1 3

(3 行受影响)

*/
go
drop table tb

快溜 2011-10-11
  • 打赏
  • 举报
回复
select *,
同编号总数量=(select sum(数量) from 表A where 合同编号=a.合同编号)
from 表A a
-晴天 2011-10-11
  • 打赏
  • 举报
回复
selet *,(select sum(数量) from tb where 合同编号=a.合同编号) as 同编号总数量 from tb a

34,575

社区成员

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

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