有表A与表B,用SQL语句得出表C:
如下:
表A
incount   content
   4        ABC
   2        CDF
   3        DDA
表B
 phone    ismember    content
  138        1          ABC
  133        1          ABC
  131        1          CDF
  132        0          DDA
  139        1          DDA
  130        0          DDA
  135        0          CDF
  136        0          ABC
  137        1          ABC表C
incount    content   会员数   非会员数
  4           ABC       3         1
  2           CDF       1         1
  3           DDA       1         2
注:B表中的字段当ismember=1 时代表会员
     ismember=0时代表非会员

解决方案 »

  1.   

    select a.incount , ab.* from a inner join (select content,sum(case ismember when 1 then 1 else 0 end) as 会员数,sum(case ismember when 0 then 1 else 0 end) as 非会员 from b group by b.content)ab on a.content= ab.content;INCOUNT CONTENT     会员数     非会员
    ------- ------- ---------- ----------
    4       ABC              3          1
    2       CDF              1          1
    3       DDA              1          2刚刚学习了一下,解决了这个问题.
    继续努力中......
      

  2.   

    用判断语句把行数据分成几列 函数有 case decode
    case 原始列 when 特征 then 值 else 0 as 目标列
    decode(原始列,特征值,值 ,0) as 目标列
      

  3.   

    create table mtb6 (incount number(10),content varchar2(10))
     CREATE TABLE MTB7(PHONE NUMBER(10),ISMEMBER NUMBER(2),CONTENT VARCHAR2(10))
     
    select a.incount,a.content
           ,sum(decode(flag,1,pCount,0)) as 会员数
           ,sum(decode(flag,0,pCount,0)) as 非会员数
    from mtb6 a
         ,(
    select content,ismember as flag
           ,sum(rn) pCount
    from (
           select content,ismember
                ,rank()over(partition by content,ismember order by ismember) rn
           from mtb7
          ) group by content,ismember
     ) b
    where a.content=b.content
    group by a.incount,a.content
      

  4.   

    select a.incount,
           a.content,
           sum(t.member) member,
           sum(t.unmember) unmember (select a.incount,
                                            a.content,
                                            1 member,
                                            0 unmember
                                       from a, b
                                      where a.content = b.content
                                        and b.ismember = 1
                                     union all
                                     select a.incount, a.content, 0, 1
                                       from a, b
                                      where a.content = b.content
                                        and b.ismember = 0) t
     group by a.incount, a.content
      

  5.   

    纠正一下,补了个from:select a.incount, a.content, sum(t.member) member, sum(t.unmember) unmember
      from (select a.incount, a.content, 1 member, 0 unmember
              from a, b
             where a.content = b.content
               and b.ismember = 1
            union all
            select a.incount, a.content, 0, 1
              from a, b
             where a.content = b.content
               and b.ismember = 0) t
     group by a.incount, a.content
      

  6.   

    Select a.Incount, b.*
      From Mtb6 a,
           (Select Content,
                   Sum(Decode(Ismember, 1, 1)),
                   Sum(Decode(Ismember, 0, 1))
              From Mtb7
             Group By Content) b
     Where a.Content = b.Content
      

  7.   

    个人认为下面的解答比较完整,要考虑外联接的问题,因为可能表B中没有表A的外键值。
    这样统计出来就可能会缺记录。
    下面是SQL2000和sybase数据库的写法,外联接和isnull函数不一致。自己可转化一下。select a.incount as incount,a.content as content,isnull(b.会员数,0),isnull(c.非会员数,0)
    from tablea a,
    (select content,count(*) as 会员数 from tableb where ismember=1 group by content) b,
    (select content,count(*) as 非会员数 from tableb where ismember=0  group by content) c
    where a.content*=b.content and a.content*=c.content
      

  8.   

    select a.incount,
           a.content,
           sum(decode(b.ismember,1,1,0) "会员数",
           sum(decode(b.ismember,0,1,0) "非会员数"
    from a,b
    where a.content=b.content
    group by a.incount,a.content;
      

  9.   

    select A.incount,B.content,(select Count(*) from B where B.content=A.content 
    and ismember=1) as 会员数,(select Count(*) from B where B.content=A.content and ismember=0) as 非会员数 from A
      

  10.   

    前面的 B.content 应为 A.content,写得匆忙,不好意思
      

  11.   

    select aa.incount,aa.content,bb.会员数,bb.非会员数 from
    (
    select * from 
    (
    select 4 as incount,'ABC' as content
    union all 
    select 2 as incount,'CDF' as content
    union all 
    select 3 as incount,'DDA' as content
    )a
    )aa
    inner join
    (
    select a.content,a.会员数,b.非会员数 from
    (
    select content,ismember,num_count as '非会员数' from
    (
    select content,ismember,
    case when ismember=1 then '会员数' else '非会员数' end as leixing,
    count(*) as num_count
    from
    (
    select 138 as phone, 1 as ismember,'ABC' as content
    union all
    select 133 as phone, 1 as ismember,'ABC' as content
    union all
    select 131 as phone, 1 as ismember,'CDF' as content
    union all
    select 132 as phone, 0 as ismember,'DDA' as content
    union all
    select 139 as phone, 1 as ismember,'DDA' as content
    union all
    select 130 as phone, 0 as ismember,'DDA' as content
    union all
    select 135 as phone, 0 as ismember,'CDF' as content
    union all
    select 136 as phone, 0 as ismember,'ABC' as content
    union all
    select 137 as phone, 1 as ismember,'ABC' as content
    ) b
    group by content,ismember
    )a
    where leixing='非会员数'
    )binner join
    (
    select content,ismember,num_count as '会员数' from
    (
    select content,ismember,
    case when ismember=1 then '会员数' else '非会员数' end as leixing,
    count(*) as num_count
    from
    (
    select 138 as phone, 1 as ismember,'ABC' as content
    union all
    select 133 as phone, 1 as ismember,'ABC' as content
    union all
    select 131 as phone, 1 as ismember,'CDF' as content
    union all
    select 132 as phone, 0 as ismember,'DDA' as content
    union all
    select 139 as phone, 1 as ismember,'DDA' as content
    union all
    select 130 as phone, 0 as ismember,'DDA' as content
    union all
    select 135 as phone, 0 as ismember,'CDF' as content
    union all
    select 136 as phone, 0 as ismember,'ABC' as content
    union all
    select 137 as phone, 1 as ismember,'ABC' as content
    ) b
    group by content,ismember
    )a
    where leixing='会员数'
    )a
    on a.content=b.content)bb
    on aa.content=bb.content
      

  12.   

    有没人同意我的做法:
    select t.incount,t.content,v.membercount,t.incount-v.membercount
    A t,
    (
    select content,sum(ismember) membercount
    from B 
    group by content
    ) v
    where t.content=v.content
      

  13.   

    tblUser = A,tblUserInfo = B
    select *,
    会员数=(select count(phone) from tblUserInfo where tblUserInfo.content = tblUser.content and ismember = '1'),
    非会员数=(select count(phone) from tblUserInfo where tblUserInfo.content = tblUser.content and ismember = '0')  
    from tblUser
      

  14.   

    Select a.Incount, a.content,isnull(b.hy,0),isnull(b.fhy,0)
      From tablea a,
           (Select Content,Sum(case Ismember when 1 then 1 end) as hy,Sum(case Ismember when 0 then 1 end) as fhy
    From tableb Group By Content) b
     Where a.Content *= b.Content