这样的数据怎么抓取?

never2006 2006-12-11 09:49:59
一个表是客户的主表 保存客户的基本信息等
customer表
以cust_id 作为主键
列cust_trade 保存客户的等级
列sales_emp 保存销售人员ID

另外一个表是客户的拜访表
cust_visit表
用cust_id 同 customer进行关联
这个表没有主键
列 INSERT_TIME 保存拜访插入数据的时间
列 EXPECT_BUY 保存此时客户等级
列sales_emp 保存销售人员ID

表三是销售人员的表
RS_YGB
列 emp_id 是主键
同上2个表关联

现在想查询一个汇总的数据
就是在一个时间段内 某个销售人员的 1级客户的升级,降级数量 2级客户的升级,降级数量等

怎么抓出这样的汇总数据呢?
最好是一条SQL语句. 不要用存储过程.也别用组件.

考虑了很久了.确实能力不够.只好请大大门帮忙了
...全文
317 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2006-12-11
  • 打赏
  • 举报
回复
估计是有空值的问题,注意一下正式环境下是否存在customer表的cust_trade列有NULL值的情况。
never2006 2006-12-11
  • 打赏
  • 举报
回复
既然测试数据没问题 就没问题了
出现错误 是偶测试数据没给好 ... 偶的问题

给分.
never2006 2006-12-11
  • 打赏
  • 举报
回复
不知道 - - 正在看 ... 命令执行成功 但是没有返回记录集
子陌红尘 2006-12-11
  • 打赏
  • 举报
回复
What's the matter?
never2006 2006-12-11
  • 打赏
  • 举报
回复
查测试数据没问题 但是查真正的数据就有问题了 =我看看就给分
never2006 2006-12-11
  • 打赏
  • 举报
回复
偶试试 ...
子陌红尘 2006-12-11
  • 打赏
  • 举报
回复
似乎弄反了,等级值越小等级越高?改改:
-----------------------------------------------------------------------------------------
create table customer(cust_id int,cust_trade int,sales_emp int)
insert into customer select 1000,1,1
insert into customer select 1001,2,2

create table cust_visit(cust_id int,INSERT_TIME datetime,EXPECT_BUY int,sales_emp int)
insert into cust_visit select 1000,'2006-11-1',1,1
insert into cust_visit select 1000,'2006-11-2',1,1
insert into cust_visit select 1000,'2006-11-3',2,1
insert into cust_visit select 1001,'2006-11-1',2,2
insert into cust_visit select 1001,'2006-11-2',2,2
insert into cust_visit select 1001,'2006-11-3',5,2
insert into cust_visit select 1001,'2006-11-4',1,2

create table RS_YGB(emp_id int,emp_name varchar(10))
insert into RS_YGB select 1,'销售人员A'
insert into RS_YGB select 2,'销售人员B'
go


declare @sql varchar(8000)
set @sql=''

select @sql=@sql+',['+rtrim(cust_trade)+'级降级客户数]=sum(case when st='+rtrim(cust_trade)+' and st<et then 1 else 0 end)'
+',['+rtrim(cust_trade)+'级升级客户数]=sum(case when st='+rtrim(cust_trade)+' and st>et then 1 else 0 end)'
from customer group by cust_trade order by cust_trade

set @sql='select emp_name'+@sql+' from RS_YGB a,'
+'(select '
+' cust_id,sales_emp,'
+' (select m.EXPECT_BUY from cust_visit m where m.cust_id=t.cust_id and m.INSERT_TIME=(select min(INSERT_TIME) from cust_visit where cust_id=m.cust_id and INSERT_TIME between ''2006-11-01'' and ''2006-11-06'')) as st, '
+' (select m.EXPECT_BUY from cust_visit m where m.cust_id=t.cust_id and m.INSERT_TIME=(select max(INSERT_TIME) from cust_visit where cust_id=m.cust_id and INSERT_TIME between ''2006-11-01'' and ''2006-11-06'')) as et '
+' from '
+' customer t) b where a.emp_id=b.sales_emp group by a.emp_name'

exec(@sql)
go


/*
emp_name 1级降级客户数 1级升级客户数 2级降级客户数 2级升级客户数
---------- ------------- ------------- ------------- -------------
销售人员A 1 0 0 0
销售人员B 0 0 0 1
*/

drop table customer,cust_visit,RS_YGB
go
子陌红尘 2006-12-11
  • 打赏
  • 举报
回复
create table customer(cust_id int,cust_trade int,sales_emp int)
insert into customer select 1000,1,1
insert into customer select 1001,2,2

create table cust_visit(cust_id int,INSERT_TIME datetime,EXPECT_BUY int,sales_emp int)
insert into cust_visit select 1000,'2006-11-1',1,1
insert into cust_visit select 1000,'2006-11-2',1,1
insert into cust_visit select 1000,'2006-11-3',2,1
insert into cust_visit select 1001,'2006-11-1',2,2
insert into cust_visit select 1001,'2006-11-2',2,2
insert into cust_visit select 1001,'2006-11-3',5,2
insert into cust_visit select 1001,'2006-11-4',1,2

create table RS_YGB(emp_id int,emp_name varchar(10))
insert into RS_YGB select 1,'销售人员A'
insert into RS_YGB select 2,'销售人员B'
go


declare @sql varchar(8000)
set @sql=''

select @sql=@sql+',['+rtrim(cust_trade)+'级降级客户数]=sum(case when st='+rtrim(cust_trade)+' and st>et then 1 else 0 end)'
+',['+rtrim(cust_trade)+'级升级客户数]=sum(case when st='+rtrim(cust_trade)+' and st<et then 1 else 0 end)'
from customer group by cust_trade order by cust_trade

set @sql='select emp_name'+@sql+' from RS_YGB a,'
+'(select '
+' cust_id,sales_emp,'
+' (select m.EXPECT_BUY from cust_visit m where m.cust_id=t.cust_id and m.INSERT_TIME=(select min(INSERT_TIME) from cust_visit where cust_id=m.cust_id and INSERT_TIME between ''2006-11-01'' and ''2006-11-06'')) as st, '
+' (select m.EXPECT_BUY from cust_visit m where m.cust_id=t.cust_id and m.INSERT_TIME=(select max(INSERT_TIME) from cust_visit where cust_id=m.cust_id and INSERT_TIME between ''2006-11-01'' and ''2006-11-06'')) as et '
+' from '
+' customer t) b where a.emp_id=b.sales_emp group by a.emp_name'

exec(@sql)
go


/*
emp_name 1级降级客户数 1级升级客户数 2级降级客户数 2级升级客户数
---------- ------------- ------------- ------------- -------------
销售人员A 0 1 0 0
销售人员B 0 0 1 0
*/

drop table customer,cust_visit,RS_YGB
go
never2006 2006-12-11
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/5221/5221317.xml?temp=.8847162
这个帖子 100 分 2个帖子+起来200分 解决就立即给分
never2006 2006-12-11
  • 打赏
  • 举报
回复
customer
cust_id cust_trade sales_emp
1000 1 1
1001 2 2

cust_visit
cust_id INSERT_TIME EXPECT_BUY sales_emp
1000 2006-11-1 1 1
1000 2006-11-2 1 1
1000 2006-11-3 2 1
1001 2006-11-1 2 2
1001 2006-11-2 2 2
1001 2006-11-3 5 2
1001 2006-11-4 1 2


RS_YGB
emp_id emp_name
1 销售人员A
2 销售人员B




生成
查询 2006-11-1 到 2006-11-6这个时间段

1级降级客户数 1级升级客户数 2级降级客户数 2级升级客户数
销售人员A 1 0 0 0
销售人员B 0 0 0 1
neustrong 2006-12-11
  • 打赏
  • 举报
回复
mark
dawugui 2006-12-11
  • 打赏
  • 举报
回复
建议:给出表结构\数据和想要的结果.
never2006 2006-12-11
  • 打赏
  • 举报
回复
就是在 cust_visit这个表里面 会有对客户的多次拜访记录
每次拜访客户的等级会发生变化 EXPECT_BUY 这个列 保存客户的等级

就是在一个时间段内 INSERT_TIME 保存拜访时间
开始时间客户的等级 同结束时间客户的等级相比较 是升级还是降级

升级 就是拜访这个客户的销售人员 的升级客户数+1 降级 就是这个销售人员的降级客户数+1

最后是要做一个 统计表
1级降级客户数 1级升级客户数 2级降级客户数 2级升级客户数(一共有5个等级)
销售人员A 100 100 100 100
销售人员B 100 100 100 100
never2006 2006-12-11
  • 打赏
  • 举报
回复
customer(cust_id int, cust_trade int, sales_emp int)
cust_id 为主键无重复数据
cust_visit(cust_id int, INSERT_TIME datetime, EXPECT_BUY int, sales_emp int)
INSERT_TIME 为主键 同customer的关系是多对一
RS_YGB(emp_id int)
emp_id为主键 同customer以及cust_visit表 的sales_emp 关联


3个表就这样
dawugui 2006-12-11
  • 打赏
  • 举报
回复
某个销售人员的 1级客户的升级,降级数量 2级客户的升级,降级数量

升级,降级?
chuifengde 2006-12-11
  • 打赏
  • 举报
回复
列出数据说明
never2006 2006-12-11
  • 打赏
  • 举报
回复
几个要求 整和在一起 我就确实不知道怎么写语句了...

请大大们帮帮忙 想下 急
never2006 2006-12-11
  • 打赏
  • 举报
回复
判断升级降级
就是用 这一个时间段


最后一个时间的 客户等级 同开始时的等级比较
来判断升级 降级

never2006 2006-12-11
  • 打赏
  • 举报
回复
我考虑的是
select emp_id
(select count(cust_id) from cust_visit where EXPECT_BUY=1 and cust_id in(判断升级降级))
from RS_YGB

客户等级只有5级


大概这样 但是 始终不对...

或者也想过
在程式里面抓2个 记录集 2层循环来判断
caixia615 2006-12-11
  • 打赏
  • 举报
回复
有 点 迷糊 ,帮顶

34,594

社区成员

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

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