34,590
社区成员
发帖
与我相关
我的任务
分享
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
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)
OrderID money
----------- -----------
10001 100
10001 100
10002 200
(3 row(s) affected)
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)