表如下,请问如何一次查询出每个员工 初步联系,预约看场, 已看场有意向, 已看场不考虑, 签约客户 的数量
  可以实现吗?谢谢啦 name(姓名)                 cus_name(客户名)                 cus_state(客户状态)
   张三                        肯德基                            初步联系
   张三                        按时的                            预约看场
   张三                        翻跟斗                            预约看场
   张三                        粗色的                            初步联系
   张三                        粗豆腐干的                        签约客户
   小刘                        挥洒的                            已看场有意向
   小刘                        士大夫                            已看场不考虑
   小刘                        gf的                              已看场不考虑
   小刘                        搞活风格                          已看场不考虑
   王五                        反对感发                          签约客户
   王五                        环境燃放法                        签约客户
   王五                        我日的感发                        签约客户
   王五                        地方搞活                          初步联系

解决方案 »

  1.   

    SELECT CUS_NAME,CUS_STATE,COUNT(1) '數量'
    FROM TB
    GROUP BY CUS_NAME,CUS_STATE
      

  2.   

    select [name],[cus_state],count(*)数量 from tb 
    gorup by [name],[cus_state]
      

  3.   

    select 
       name,count(cus_state) 
    from 
       tb 
    where 
       cus_state='初步联系'
    group by 
       name
      

  4.   

    If not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([name(姓名)] nvarchar(2),[cus_name(客户名)] nvarchar(5),[cus_state(客户状态)] nvarchar(6))
    Insert tb
    Select '张三','肯德基','初步联系' union all
    Select '张三','按时的','预约看场' union all
    Select '张三','翻跟斗','预约看场' union all
    Select '张三','粗色的','初步联系' union all
    Select '张三','粗豆腐干的','签约客户' union all
    Select '小刘','挥洒的','已看场有意向' union all
    Select '小刘','士大夫','已看场不考虑' union all
    Select '小刘','gf的','已看场不考虑' union all
    Select '小刘','搞活风格','已看场不考虑' union all
    Select '王五','反对感发','签约客户' union all
    Select '王五','环境燃放法','签约客户' union all
    Select '王五','我日的感发','签约客户' union all
    Select '王五','地方搞活','初步联系'
    Go
    --Select * from tb-->SQL查询如下:
    declare @s varchar(8000)
    set @s='select [name(姓名)]'
    select @s=@s+',sum(case [cus_state(客户状态)] when '''+[cus_state(客户状态)]+''' then 1 else 0 end)['+[cus_state(客户状态)]+']'
    from tb group by [cus_state(客户状态)]
    exec(@s+' from tb group by [name(姓名)]')
    /*
    name(姓名) 初步联系        签约客户        已看场不考虑      已看场有意向      预约看场
    -------- ----------- ----------- ----------- ----------- -----------
    王五       1           3           0           0           0
    小刘       0           0           3           1           0
    张三       2           1           0           0           2(3 行受影响)
    */
      

  5.   


    SELECT CUS_NAME,CUS_STATE,COUNT(1) '數量'
    FROM TB
    GROUP BY CUS_NAME,CUS_STATE鸭子
      

  6.   

    SELECT CNAME,CUS_STATE,COUNT(1) '數量'
    FROM TB
    GROUP BY NAME,CUS_STATE
      

  7.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-27 11:12:08
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([name] varchar(4),[cus_name] varchar(10),[cus_state] varchar(12))
    insert [tb]
    select '张三','肯德基','初步联系' union all
    select '张三','按时的','预约看场' union all
    select '张三','翻跟斗','预约看场' union all
    select '张三','粗色的','初步联系' union all
    select '张三','粗豆腐干的','签约客户' union all
    select '小刘','挥洒的','已看场有意向' union all
    select '小刘','士大夫','已看场不考虑' union all
    select '小刘','gf的','已看场不考虑' union all
    select '小刘','搞活风格','已看场不考虑' union all
    select '王五','反对感发','签约客户' union all
    select '王五','环境燃放法','签约客户' union all
    select '王五','我日的感发','签约客户' union all
    select '王五','地方搞活','初步联系'
    --------------开始查询--------------------------
    select 
      [name],[cus_state],count(*)数量 
    from 
      tb 
    group by 
      [name],[cus_state]
    order by 
      [name]
    ----------------结果----------------------------
    /*name cus_state    数量
    ---- ------------ -----------
    王五   初步联系         1
    王五   签约客户         3
    小刘   已看场不考虑       3
    小刘   已看场有意向       1
    张三   初步联系         2
    张三   签约客户         1
    张三   预约看场         2(7 行受影响)
    */
      

  8.   

    -->Title:生成測試數據
    -->Author:wufeng4552【水族杰纶】
    -->Date :2009-08-27 11:13:32
     
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([name] nvarchar(2),[cus_name] nvarchar(5),[cus_state] nvarchar(6))
    Insert tb
    select N'张三',N'肯德基',N'初步联系' union all
    select N'张三',N'按时的',N'预约看场' union all
    select N'张三',N'翻跟斗',N'预约看场' union all
    select N'张三',N'粗色的',N'初步联系' union all
    select N'张三',N'粗豆腐干的',N'签约客户' union all
    select N'小刘',N'挥洒的',N'已看场有意向' union all
    select N'小刘',N'士大夫',N'已看场不考虑' union all
    select N'小刘',N'gf的',N'已看场不考虑' union all
    select N'小刘',N'搞活风格',N'已看场不考虑' union all
    select N'王五',N'反对感发',N'签约客户' union all
    select N'王五',N'环境燃放法',N'签约客户' union all
    select N'王五',N'我日的感发',N'签约客户' union all
    select N'王五',N'地方搞活',N'初步联系'
    Go
    select [name],[cus_state],count(*)数量 from tb 
    group by [name],[cus_state]
    /*
    name cus_state 数量
    ---- --------- -----------
    小刘   已看场不考虑    3
    小刘   已看场有意向    1
    王五   初步联系      1
    张三   初步联系      2
    王五   签约客户      3
    张三   签约客户      1
    张三   预约看场      2(7 個資料列受到影響)
    */
      

  9.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-27 11:12:08
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([name] varchar(4),[cus_name] varchar(10),[cus_state] varchar(12))
    insert [tb]
    select '张三','肯德基','初步联系' union all
    select '张三','按时的','预约看场' union all
    select '张三','翻跟斗','预约看场' union all
    select '张三','粗色的','初步联系' union all
    select '张三','粗豆腐干的','签约客户' union all
    select '小刘','挥洒的','已看场有意向' union all
    select '小刘','士大夫','已看场不考虑' union all
    select '小刘','gf的','已看场不考虑' union all
    select '小刘','搞活风格','已看场不考虑' union all
    select '王五','反对感发','签约客户' union all
    select '王五','环境燃放法','签约客户' union all
    select '王五','我日的感发','签约客户' union all
    select '王五','地方搞活','初步联系'
    --------------开始查询--------------------------
    declare @s varchar(8000)
    set @s='select [name]'
    select @s=@s+',sum(case [cus_state] when '''+[cus_state]+''' then 1 else 0 end)['+[cus_state]+']'
    from tb group by [cus_state]
    exec(@s+' from tb group by [name]')
    ----------------结果----------------------------
    /*name 初步联系        签约客户        已看场不考虑      已看场有意向      预约看场
    ---- ----------- ----------- ----------- ----------- -----------
    王五   1           3           0           0           0
    小刘   0           0           3           1           0
    张三   2           1           0           0           2(3 行受影响)*/
      

  10.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([name] varchar(4),[cus_name] varchar(10),[cus_state] varchar(12))
    insert [tb]
    select '张三','肯德基','初步联系' union all
    select '张三','按时的','预约看场' union all
    select '张三','翻跟斗','预约看场' union all
    select '张三','粗色的','初步联系' union all
    select '张三','粗豆腐干的','签约客户' union all
    select '小刘','挥洒的','已看场有意向' union all
    select '小刘','士大夫','已看场不考虑' union all
    select '小刘','gf的','已看场不考虑' union all
    select '小刘','搞活风格','已看场不考虑' union all
    select '王五','反对感发','签约客户' union all
    select '王五','环境燃放法','签约客户' union all
    select '王五','我日的感发','签约客户' union all
    select '王五','地方搞活','初步联系'--SQL SERVER 2000
    declare @sql varchar(8000)
    set @sql = 'select [name] '
    select @sql = @sql + ' , sum(case [cus_state] when ''' + [cus_state] + ''' then 1 else 0 end) [' + [cus_state] + ']'
    from (select distinct [cus_state] from tb) as a
    set @sql = @sql + ' from tb group by [name]'
    exec(@sql) 
    /*
    name 初步联系        签约客户        已看场不考虑      已看场有意向      预约看场
    ---- ----------- ----------- ----------- ----------- -----------
    王五   1           3           0           0           0
    小刘   0           0           3           1           0
    张三   2           1           0           0           2(3 行受影响)
    */
      

  11.   

    --2000静态
    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-27 11:12:08
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([name] varchar(4),[cus_name] varchar(10),[cus_state] varchar(12))
    insert [tb]
    select '张三','肯德基','初步联系' union all
    select '张三','按时的','预约看场' union all
    select '张三','翻跟斗','预约看场' union all
    select '张三','粗色的','初步联系' union all
    select '张三','粗豆腐干的','签约客户' union all
    select '小刘','挥洒的','已看场有意向' union all
    select '小刘','士大夫','已看场不考虑' union all
    select '小刘','gf的','已看场不考虑' union all
    select '小刘','搞活风格','已看场不考虑' union all
    select '王五','反对感发','签约客户' union all
    select '王五','环境燃放法','签约客户' union all
    select '王五','我日的感发','签约客户' union all
    select '王五','地方搞活','初步联系'
    --------------开始查询--------------------------
    select [name] ,
      sum(case [cus_state] when '初步联系' then 1 else 0 end) 初步联系,
      sum(case [cus_state] when '预约看场' then 1 else 0 end) 预约看场,
      sum(case [cus_state] when '签约客户' then 1 else 0 end) 签约客户,
      sum(case [cus_state] when '已看场有意向' then 1 else 0 end) 已看场有意向,
      sum(case [cus_state] when '已看场不考虑' then 1 else 0 end) 已看场不考虑
    from 
      tb
    group by
      [name]
    ----------------结果----------------------------
    /*name 初步联系        预约看场        签约客户        已看场有意向      已看场不考虑
    ---- ----------- ----------- ----------- ----------- -----------
    王五   1           0           3           0           0
    小刘   0           0           0           1           3
    张三   2           2           1           0           0(3 行受影响)*/