数据求和问题

gswuliu 2012-04-25 11:41:37
加入有一个表如下:
OrderID wareID city money
10001 112 上海 100
10001 112 北京 100
10002 114 上海 100
10002 115 上海 100


我想对money求和,条件是orderid相同但是wareID不同。如果OrderID和wareID都相同则不求和,
最后得出的字段只要OrderID和求和后的money,如上表最后得出的结果为
OrderID money
10001 100
10001 100
10002 200
请问怎么写SQL,多谢大家。
...全文
139 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
十三门徒 2012-04-26
  • 打赏
  • 举报
回复
with tb as (select distinct orderid,wareid,money from gsw )

select B.orderid,c.money from(select orderid from tb group by orderid having count(orderid)=1) b
join gsw c on b.orderid=c.orderid
union all
select orderid,sum(money) from tb a group by orderid having count(wareID) >1
wxwlll 2012-04-26
  • 打赏
  • 举报
回复
反正肯定不能用Group by.

其次在连接操作时,要注意同一个表中的不同记录的同一属性之比较时,首先应对此基本表用两个别名。然后用这两个别名分别引用此属性,去比较!
唐诗三百首 2012-04-26
  • 打赏
  • 举报
回复

create table gsw
(OrderID int, wareID int,
city varchar(6), money int)

insert into gsw
select 10001, 112, '上海', 100 union all
select 10001, 112, '北京', 100 union all
select 10002, 114, '上海', 100 union all
select 10002, 115, '上海', 100

with t as
(select b1.OrderID,b1.wareID
from gsw b1 inner join gsw b2
on b1.OrderID=b2.OrderID and b1.wareID<>b2.wareID
)
select a.OrderID,a.money
from gsw a left join t b
on a.OrderID=b.OrderID and a.wareID=b.wareID
where b.OrderID is null and b.wareID is null
union all
select a.OrderID,sum(a.money) 'money'
from gsw a inner join t b
on a.OrderID=b.OrderID and a.wareID=b.wareID
group by a.OrderID


OrderID money
----------- -----------
10001 100
10001 100
10002 200

(3 row(s) affected)
唐诗三百首 2012-04-26
  • 打赏
  • 举报
回复

OrderID money
----------- -----------
10001 100
10001 100
10002 200

(3 row(s) affected)
风飘扬 2012-04-26
  • 打赏
  • 举报
回复
顶2楼
gswuliu 2012-04-26
  • 打赏
  • 举报
回复
如果有两条orderid相同但是wareid不同的数据您这样是正确的,但是如果有三条则会重复计算。比如
10002 114 上海 100
10002 115 上海 100
10002 116 上海 100
则最后数据是600.即最终数据=正确数据*(记录数-1),还请您帮忙查看,谢谢。

[Quote=引用 2 楼 的回复:]

SQL code

create table gsw
(OrderID int, wareID int,
city varchar(6), money int)

insert into gsw
select 10001, 112, '上海', 100 union all
select 10001, 112, '北京', 100 union all
select 10002, 11……
[/Quote]
wujianfeng32 2012-04-26
  • 打赏
  • 举报
回复
and wareID<>wareID

想想都不行。。。
裸奔在上海 2012-04-26
  • 打赏
  • 举报
回复
2楼正确,学习了
  • 打赏
  • 举报
回复
select OrderID,wareid,money=money+isnull((select sum(money) from tb where orderid=t.orderid and wareid<>t.wareid),0)
from tb t
lingxiao200 2012-04-26
  • 打赏
  • 举报
回复
select orderID,sum(money) from table where orderid=orderid and wareID<>wareID

不晓得对不
唐诗三百首 2012-04-26
  • 打赏
  • 举报
回复

create table gsw
(OrderID int, wareID int,
city varchar(6), money int)

insert into gsw
select 10001, 112, '上海', 100 union all
select 10001, 112, '北京', 100 union all
select 10002, 114, '上海', 100 union all
select 10002, 115, '上海', 100 union all
select 10002, 116, '上海', 100


with t as
(select distinct b1.OrderID,b1.wareID
from gsw b1 inner join gsw b2
on b1.OrderID=b2.OrderID and b1.wareID<>b2.wareID
)
select a.OrderID,a.money
from gsw a left join t b
on a.OrderID=b.OrderID and a.wareID=b.wareID
where b.OrderID is null and b.wareID is null
union all
select a.OrderID,sum(a.money) 'money'
from gsw a inner join t b
on a.OrderID=b.OrderID and a.wareID=b.wareID
group by a.OrderID

OrderID money
----------- -----------
10001 100
10001 100
10002 300

(3 row(s) affected)

[Quote=引用 7 楼 的回复:]
如果有两条orderid相同但是wareid不同的数据您这样是正确的,但是如果有三条则会重复计算。比如
10002 114 上海 100
10002 115 上海 100
10002 116 上海 100
则最后数据是600.即最终数据=正确数据*(记录数-1),还请您帮忙查看,谢谢。
[/Quote]

34,590

社区成员

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

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