CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

求句sql语句,,,挺难的。。。急活

楼主koenemy(小妮她爹--给孩子起个名太费劲)2005-11-04 08:34:06 在 MS-SQL Server / 基础类 提问

视图  
   
  员工id             预约时间     上司id           谈话内容     谈话记录时间     CustomerID  
  1                       2005-2-1       0                   aaaa             2005-2-17               8  
  1                       null               0                   bbbb             2005-2-16               9  
  2                       null               1                   null             2005-2-12                 10  
  2                       2005-1-9       1                   cccc             2005-2-13               11  
  3                       2005-8-8       1                   dddd             2005-2-12                 12  
  3                       null               1                   eeee             2005-2-11                 13  
  想要得到以下统计  
  员工id=1的或是上司id=1   的   预约的人数(预约时间不是null的)   还有已联系的人数(谈话内空不是null的)这两个数还得能按时间段来统计  
   
  员工id             预约的人数       已联系的人数       客户总数  
  1                       1                           2                               2  
  2                       1                           1                                 2  
  3                       1                           2                                   2 问题点数:50、回复次数:2Top

1 楼rivery(river)回复于 2005-11-04 08:42:50 得分 40

select   员工id,预约人数=sum(case   when   (员工id=1   or   上司id=1)   and   预约时间   is   not   null   then   1   else   0   end),  
              已联系人数=sum(case   when   谈话内容   is   not   null   then   1   else   0   end),  
              客户总数=count(*)  
  from   视图  
  group   by   员工idTop

2 楼dutguoyi(新鲜鱼排)回复于 2005-11-04 08:56:19 得分 10

 
  create   table   #table1(id1   int,order_time   datetime,id2   int,chat_info   varchar(8000),chat_time   datetime,   customerID   int)  
  insert   #table1   select   1,'2005-2-1',0,'aaaa','2005-2-17',8  
  union   all   select   1,null,0,'bbbb','2005-2-16',9  
  union   all   select   2,null,1,null,'2005-2-12',10  
  union   all   select   2,'2005-1-9',1,'cccc','2005-2-13',11  
  union   all   select   3,'2005-8-8',1,'dddd','2005-2-12',12  
  union   all   select   3,null,1,'eeee','2005-2-11',13  
   
  select   *   from   #table1  
  select   t2.id1,t2.count2,t3.count3,   t1.count1  
  from   (select   count(1)   as   count1,id1   from   #table1  
  group   by   id1)   as   t1  
  join   (  
  select   id1,count(1)   as   count2   from     #table1  
  where   (id1=1   or   id2=1)     and   order_time   is   not   null    
  group   by   id1  
  )   t2   on   t1.id1=t2.id1  
  join   (  
  select   id1,count(1)   as   count3   from     #table1  
  where   chat_info   is   not   null    
  group   by   id1  
  )   t3   on   t1.id1=t3.id1  
  drop   table   #table1  
   
   
  id1                   count2             count3             count1              
  -----------   -----------   -----------   -----------    
  1                       1                       2                       2  
  2                       1                       1                       2  
  3                       1                       2                       2  
   
  (3   row(s)   affected)  
  Top

相关问题

  • sql语句,急
  • sql语句 急急
  • 急急,sql语句!
  • sql语句急救
  • 急用!!sql语句
  • 急求SQL语句!!!!!!!!!!!!!
  • 求SQL语句急
  • 急问!!!!!!!!!!!!!!!!!!!!!SQL语句
  • 急求SQL语句?
  • 急求SQL语句

关键词

  • 员工
  • null
  • 预约
  • 人数
  • 上司
  • 联系
  • 时间
  • table
  • select

得分解答快速导航

  • 帖主:koenemy
  • rivery
  • dutguoyi

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo