现有表(主叫号码、被叫号码、拨打时间),存储数据如下   
     A         2001       11:00
     A         2002       11:21   
     A         2001       11:50
     B         2001       11:00
     B         2002       11:21
     B         2003       11:30
     B         2001       11:50
需要按主叫分组,统计被叫的个数,以及拨打的总次数,并且按总拨打次数降序排列得到如下结果
    主叫号码   被叫号码  次数 
      B          2001      2
                 2002      1
                 2003      1
                  合计     4
      A          2001      2
                 2003      1
                  合计     3 
  请问如何写sql?我用的C#开发asp.net程序,sql   server数据库。谢谢大家

解决方案 »

  1.   

    select 主叫号码,被叫号码 ,  count(*) as cs   from  表 group by 主叫号码,被叫号码
    having  sum(cs)
      

  2.   

    try
    select 
      主叫号码=(case when not exists(select 1 from tb where 主叫号码=t.主叫号码 and 被叫号码<t.被叫号码) then  被叫号码 end),
      被叫号码,
      次数=count(1)
    from
      tb t
    group by
      主叫号码,
      被叫号码
      

  3.   

    if OBJECT_ID('tb') is not null drop table tb
      go
    create table tb(name nvarchar(10),year nvarchar(20),time nvarchar(20))
      go
    insert tb
    select
        'A',        '2001',      '11:00' union all select
        'A',        '2002',      '11:21'  union all select
        'A',        '2001',      '11:50' union all select
        'B',        '2001',      '11:00' union all select
        'B',        '2002',      '11:21' union all select
        'B',        '2003',      '11:30' union all select
        'B',        '2001',      '11:50' 
    select isnull(a.name,'总计') as 名称,isnull(a.year,'小计') as 年度,COUNT(1) as 次数
      from tb a
      group by a.name,a.year with rollup
    /*
    名称 年度 次数
    A 2001 2
    A 2002 1
    A 小计 3
    B 2001 2
    B 2002 1
    B 2003 1
    B 小计 4
    总计 小计 7
    */
      
      

  4.   


    declare @t table(主叫号码 varchar(1),被叫号码 varchar(4),拨打时间 datetime)
    insert @t select
        'A',        '2001'      ,'11:00' union all select 
        'A' ,       '2002'     , '11:21' union all select  
        'A'  ,      '2001'    ,  '11:50' union all select 
        'B'   ,     '2001'   ,   '11:00' union all select 
        'B'    ,    '2002'      ,'11:21' union all select 
        'B'     ,   '2003'  ,    '11:30' union all select 
        'B'      ,  '2001' ,     '11:50'select  主叫号码,isnull(被叫号码,'合计'),次数=count(*) from @t
    group by 主叫号码,被叫号码 
    with rollup
    having(grouping(主叫号码)<>1 or grouping(被叫号码)<>1)主叫号码      次数
    ---- ---- -----------
    A    2001 2
    A    2002 1
    A    合计   3
    B    2001 2
    B    2002 1
    B    2003 1
    B    合计   4(7 行受影响)
      

  5.   

    if OBJECT_ID('tb') is not null drop table tb
      go
    create table tb(name nvarchar(10),year nvarchar(20),time nvarchar(20))
      go
    insert tb
    select
        'A',        '2001',      '11:00' union all select
        'A',        '2002',      '11:21'  union all select
        'A',        '2001',      '11:50' union all select
        'B',        '2001',      '11:00' union all select
        'B',        '2002',      '11:21' union all select
        'B',        '2003',      '11:30' union all select
        'B',        '2001',      '11:50' 
    select case when year='2001' then a.name 
                when grouping(name)=1 then '总计' 
                else '' end name,isnull(a.year,'小计') as 年度,COUNT(1) as 次数
      from tb a
      group by a.name,a.year with rollup/*
    name 年度 次数
    A 2001 2
    2002 1
    小计 3
    B 2001 2
    2002 1
    2003 1
    小计 4
    总计 小计 7
    */
      
      

  6.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([主叫号码] varchar(1),[被叫号码] int,[拨打时间] varchar(6))
    insert [tb]
    select 'A',2001,'11:00' union all
    select 'A',2002,'11:21' union all
    select 'A',2001,'11:50' union all
    select 'B',2001,'11:00' union all
    select 'B',2002,'11:21' union all
    select 'B',2003,'11:30' union all
    select 'B',2001,'11:50'-->查询
    select 
      主叫号码,被叫号码,次数
    from
    (
    select 
      主叫号码 as px,
      主叫号码=(case when not exists(select 1 from tb where 主叫号码=t.主叫号码 and 被叫号码<t.被叫号码) then  主叫号码 else '' end),
      ltrim(被叫号码) as 被叫号码,
      次数=count(1)
    from tb t
    group by 主叫号码,被叫号码
    union all
    select 主叫号码,'','合计',count(1) from tb group by 主叫号码
    ) t
    order by 
      px desc,
      被叫号码
    --测试结果:
    /*
    主叫号码 被叫号码         次数
    ---- ------------ -----------
    B    2001         2
         2002         1
         2003         1
         合计           4
    A    2001         2
         2002         1
         合计           3(7 行受影响)*/