如表MSN中,字段FromUsername是发送方的账号,字段ToUsername是接收方的账号。
我想得到是该表中FromUsername和ToUsername的交互次数,即聊天次数,该如何?
(注:FromUsername和ToUsername可能为空,一般情况下FromUsername和ToUsername是一一对应的,
如果有为空的这里暂时不考虑)。
如:FromUsername为[email protected],ToUsername为[email protected]
[email protected][email protected]发送了10条,[email protected]回复
了8条,则他们之间的交互记录是18。或者谁能针对一张这样的表提出一些比较有意义的统计分析结果也行,给出自已的查询语句。另:下面的这条语句谁能帮忙分析一下?我查出的结果和我想像中的不一样。
SELECT     A1.FromUsername, A1.ToUsername, COUNT(*) AS counts
FROM         MSN AS A1 INNER JOIN
                      MSN AS A2 ON A1.FromUsername = A2.ToUsername AND A1.ToUsername = A2.FromUsername
GROUP BY A1.FromUsername, A1.ToUsername

解决方案 »

  1.   

    SELECT FromUserName,ToUserName,COUNT(*) AS counts
    FROM (
        SELECT
           FromUserName,
           ToUserName
        FROM MSN
        UNION ALL
        SELECT
           ToUserName,
           FromUserName
        FROM MSN
    ) AS A
    GROUP BY FromUserName,ToUserName
      

  2.   

    为何不这么写?
    SELECT     A1.FromUsername, A1.ToUsername, COUNT(*) AS counts
    FROM         MSN A1
    GROUP BY A1.FromUsername, A1.ToUsername
      

  3.   

    无非就是接收方和发送方颠倒一下次序而已。不影响GROUP BY A1.FromUsername, A1.ToUsername的。
      

  4.   

    create proc proc_msg
    (
      @FromUsername varchar(50),
      @ToUsername varchar(50)
    )
    as
    SELECT COUNT(*) AS counts
    FROM MSN where FromUsername = @FromUsername AND ToUsername = @ToUsername
      

  5.   

    SELECT     FromUsername, ToUsername, COUNT(*) AS counts
    FROM         MSN 
    GROUP BY FromUsername, ToUsername
      

  6.   

    create table #MSN
    (
      FromUsername varchar(50),
      ToUsername varchar(50)
    )
    insert into #MSN select 'aa','bb'
    union all select 'aa','cc'
    union all select 'tt','cc'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'gg','ee'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'bb','dd'
    union all select 'aa','cc'
    union all select 'tt','bb'SELECT COUNT(*) AS counts
    FROM #MSN where FromUsername = N'aa' AND ToUsername = N'cc'counts
    -----------
    7(1 行受影响)
      

  7.   

    --楼主的查询结果。
    create table msn(FromUsername varchar(30),ToUsername varchar(30))
    go
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    insert msn select '[email protected]','[email protected]'
    go
    SELECT     A1.FromUsername, A1.ToUsername, COUNT(*) AS counts
    FROM         MSN AS A1 inner JOIN
                          MSN AS A2 ON A1.FromUsername = A2.ToUsername AND A1.ToUsername = A2.FromUsername
    GROUP BY A1.FromUsername, A1.ToUsername
    /*
    FromUsername                   ToUsername                     counts      
    ------------------------------ ------------------------------ ----------- 
    [email protected]             [email protected]             80
    [email protected]             [email protected]             80(所影响的行数为 2 行)*/
      

  8.   

    create table #MSN
    ( id int identity(1,1),
      FromUsername varchar(50),
      ToUsername varchar(50)
    )
    insert into #MSN select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
     
    ;with t
    as
    (
    select id, Min_abcd=min([value]) ,Max_abcd=max([value]) 
    from #msn 
    unpivot([value] for [abcd] in ([FromUsername], [ToUsername])) as U
    group by ID

    select Min_abcd+'---'+Max_abcd,count(1)as cnt
    from t
    group by Min_abcd+'---'+Max_abcddrop table #msn/*
                                                                      cnt
    ------------------------------------------------------       -----------
    [email protected]@hotmail.com                              8
    [email protected]@hotmail.com                              1
    [email protected]@hotmail.com                              1
    */
      

  9.   

    人家要的,,是
    [email protected]             [email protected]             160这种数据吧,呵,,,
      

  10.   

    create table #msn
    ( id int identity(1,1),
      FromUsername varchar(50),
      ToUsername varchar(50)
    )
     
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]'
    insert #msn select '[email protected]','[email protected]';with t
    as
    (
    select id, Min_abcd=min([value]) ,Max_abcd=max([value]) 
    from #msn 
    unpivot([value] for [abcd] in ([FromUsername], [ToUsername])) as U
    group by ID

    select Min_abcd+'---'+Max_abcd,count(1)as cnt
    from t
    group by Min_abcd+'---'+Max_abcddrop table #msn/*
                                                         cnt
    ---------                                            -----------
    [email protected]@hotmail.com              18
    */
      

  11.   

    也不一定就是要得到某种特别的数据,我的想法是通过分析表中的记录来了解谁和谁之间有过交谈的次数统计(这可以显示在form上)。然后点击统计结果再看到详细的交谈记录。而数据量巨大,而且有可能发送方或者接收方是空的,或者聊天的内容是空的(发送方或者接收方为空的暂时不考虑)。大家也可以自己造一些数据看看,至少在100条以上吧。谢谢楼上的几位建议,可以考虑一下。
      

  12.   


    [email protected]@hotmail.com 这只是显示的格式,表示两个人通讯过呀,上面的不是你想要的结果  ?
      

  13.   

    create table #MSN
    ( id int identity(1,1),
      FromUsername varchar(50),
      ToUsername varchar(50)
    )
    insert into #MSN select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
     union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
      

  14.   

    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]'
    union all select '[email protected]','[email protected]
    ;with t
    as
    (
        select id, Min_abcd=min([value]) ,Max_abcd=max([value]) 
        from #msn 
        unpivot([value] for [abcd] in ([FromUsername], [ToUsername])) as U
        group by ID

    select Min_abcd+'---'+Max_abcd,count(1)as cnt
    from t
    group by Min_abcd+'---'+Max_abcddrop table #msn/*
    ------------------------------------------------------ -----------
    [email protected]@hotmail.com                      7
    [email protected]@hotmail.com                      1
    [email protected]@hotmail.com                      2
    [email protected]@hotmail.com                      1
    [email protected]@hotmail.com                      1
    [email protected]@hotmail.com                      1
    [email protected]@hotmail.com                       83
    [email protected]@hotmail.com                       11
    [email protected]@hotmail.com                       11
    */
      

  15.   

    create table #MSN
    (
      FromUsername varchar(50),
      ToUsername varchar(50)
    )
    insert into #MSN select 'aa','bb'
    union all select 'aa','cc'
    union all select 'tt','cc'
    union all select 'tt','cc'
    union all select 'tt','cc'
    union all select 'tt','cc'
    union all select 'tt','cc'
    union all select 'tt','cc'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'gg','ee'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'bb','dd'
    union all select 'aa','cc'
    union all select 'tt','bb'
    union all select 'cc','bb'
    union all select 'cc','bb'
    union all select 'cc','aa'
    union all select 'cc','aa'
    union all select 'cc','aa'
    union all select 'cc','aa'
    union all select 'cc','aa'
    union all select 'cc','bb';with temp as 
     (
      SELECT     FromUsername, ToUsername, COUNT(*) AS counts
      FROM       #MSN AS A1 group by FromUsername,ToUsername
     )select distinct a.FromUsername Username1,a.ToUsername Username2,isnull(a.counts,0)+isnull(b.counts,0) as counts 
       from temp a  left join temp b on a.FromUsername=b.ToUsername and a.ToUsername=b.FromUsername
    drop table #MSN
      

  16.   

    谢谢happy兄弟的回复,但是这样有一个问题就是如果我有一方为NULL的话你这样统计出来的结果就不准确了。有可能最后变成了A和A聊天或者B和B聊天,这是不可能的。那要怎么去除有为NULL的记录呢?
      

  17.   

    create table #MSN
    (
      FromUsername varchar(50),
      ToUsername varchar(50)
    )
    insert into #MSN select 'aa','bb'
    union all select 'aa','cc'
    union all select 'tt','cc'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'gg','ee'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'bb','dd'
    union all select 'aa','cc'
    union all select 'tt','bb'
    SELECT     A1.FromUsername, A1.ToUsername, COUNT(*) AS counts
    FROM         #MSN AS A1 full JOIN
                          #MSN AS A2 ON A1.FromUsername = A2.ToUsername AND A1.ToUsername = A2.FromUsername
    GROUP BY A1.FromUsername, A1.ToUsername
      

  18.   

    SELECT     A1.FromUsername, A1.ToUsername, COUNT(*) AS counts
    FROM         MSN AS A1 full JOIN
                          MSN AS A2 ON A1.FromUsername = A2.ToUsername AND A1.ToUsername = A2.FromUsername
    GROUP BY A1.FromUsername, A1.ToUsername
      

  19.   

    create table #MSN
    (
      FromUsername varchar(50),
      ToUsername varchar(50)
    )
    insert into #MSN select 'aa','bb'
    union all select 'aa','cc'
    union all select 'tt','cc'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'gg','ee'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'aa','cc'
    union all select 'bb','dd'
    union all select 'aa','cc'
    union all select 'tt','bb'
    SELECT     A1.FromUsername, A1.ToUsername, COUNT(*) AS counts
    FROM         #MSN AS A1 full JOIN
                          #MSN AS A2 ON A1.FromUsername = A2.ToUsername AND A1.ToUsername = A2.FromUsername  where   A1.FromUsername is not null and  A1.ToUsername is not null
    GROUP BY A1.FromUsername, A1.ToUsername