有一sql求助

czwily 2010-10-13 01:58:10
表数据
id workid barcode cusid(客户编号)
1 1(发送) 0001 1
2 7(回收) 0001 1
3 1 0001 1
4 7 0001 2
5 1 0002 1
6 7 0002 3


barcode是条码,一个条码发送给一个客户1后,无论他是从其他客户2或3手上回来的,
只要它回来了,就算在客户一头上


要求查询汇总结果
cusid 发送次数 回收次数
1 3 3
2 0 0
3 0 0
...全文
122 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fpzgm 2010-10-13
  • 打赏
  • 举报
回复

--更正一下,借用的数据
--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (id int,workid int,barcode varchar(4),cusid int)
insert into #tb
select 1,1,'0001',1 union all
select 2,7,'0001',1 union all
select 3,1,'0001',1 union all
select 4,7,'0001',2 union all
select 5,1,'0002',1 union all
select 6,7,'0002',3



with cte as
(select * from #tb where id%2=1
union
select a.id,a.workid,a.barcode,b.cusid
from #tb a,#tb b where a.id=b.id+1,a.barcode=b.barcode)

select cusid,sum(case when workid=1 then 1 else 0 end) as 发送次数,
sum(case when workid=7 then 1 else 0 end) as 回收次数
from cte group by cusid


cusid 发送次数 回收次数
----------- ----------- -----------
1 3 3
2 0 0
3 0 0

(3 行受影响)

fpzgm 2010-10-13
  • 打赏
  • 举报
回复

--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (id int,workid int,barcode varchar(4),cusid int)
insert into #tb
select 1,1,'0001',1 union all
select 2,7,'0001',1 union all
select 3,1,'0001',1 union all
select 4,7,'0001',2 union all
select 5,1,'0002',1 union all
select 6,7,'0002',3



with cte as
(select * from [table] where id%2=1
union
select a.id,a.workid,a.barcode,b.cusid
from [table] a,[table] b where a.id=b.id+1,a.barcode=b.barcode)

select cusid,sum(case when workid=1 then 1 else 0 end) as 发送次数,
sum(case when workid=7 then 1 else 0 end) as 回收次数
from cte group by cusid


cusid 发送次数 回收次数
----------- ----------- -----------
1 3 3
2 0 0
3 0 0

(3 行受影响)


SQL2088 2010-10-13
  • 打赏
  • 举报
回复
--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (id int,workid int,barcode varchar(4),cusid int)
insert into #tb
select 1,1,'0001',1 union all
select 2,7,'0001',1 union all
select 3,1,'0001',1 union all
select 4,7,'0001',2 union all
select 5,1,'0002',1 union all
select 6,7,'0002',3



select a.cusid,[发送次数],[回收次数]=isnull([回收次数],0) from
(
select cusid,
[发送次数]=sum(case when workid=1 then 1 else 0 end)
from #tb t
group by cusid
)a
left join
(
select t.cusid ,
[回收次数]= count(*)
from
(select distinct barcode,cusid from #tb where workid=1)t , #tb b
where t.barcode=b.barcode
and b.workid=7
group by t.cusid
)c
on a.cusid=c.cusid

cusid 发送次数 回收次数
----------- ----------- -----------
1 3 3
2 0 0
3 0 0

(3 行受影响)
fpzgm 2010-10-13
  • 打赏
  • 举报
回复

with cte as
(select * from [table] where id%2=1
union
select a.id,a.workid,a.barcode,b.cusid
from [table] a,[table] b where a.id=b.id+1,a.barcode=b.barcode)
select cusid,sum(case when workid=1 then 1 else 0 end) as 发送次数,
sum(case when workid=7 then 1 else 0 end) as 回收次数
from cte group by cusid
Rotel-刘志东 2010-10-13
  • 打赏
  • 举报
回复
楼主的结果很奇怪呀~!?
czwily 2010-10-13
  • 打赏
  • 举报
回复
条码0001,发送给客户2次,回收的时候一次是从客户1手上回收,另一次是从客户2手上回收,但因为都是发送给客户1的,所以无论最终这个回收都算在客户1头上

这个操作因为不正规,其实应该发给谁,从谁手上回收;但有时候气站会忘记去做记录,其实少了一个从客户1回收再发给客户2的操作
jwdream2008 2010-10-13
  • 打赏
  • 举报
回复
结果怎么来的?
czwily 2010-10-13
  • 打赏
  • 举报
回复
一个条码,发给客户1,不管最后回来是从哪个客户手上回来的,回收都算在客户1头上
SQLCenter 2010-10-13
  • 打赏
  • 举报
回复
看不懂要的结果

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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