--表
tb_1
用户编号  发放类型 发放时间
00001      a        2010-01-01 
00001      b        2010-01-05 
00001      f        2010-01-07 00002      a        2010-02-01 
00002      c        2010-01-07 
00002      e        2010-01-11
00002      a        2010-01-01 
.........最后想得到的结果是
月份      发放a      发放b       发放c      发放d      发放e      发放f      用户数
2010-1-1  是           否        否          否          否        否          10
2010-1-1  是           是        否          是          否        否         。--
这样算出某一个指定的月内满足各种发放组合的用户数比如说:
只发放了a类型的用户有多少,
发放了a,b两种类型的用户有多少,
发放了b,c,f  三种类型的用户有多少等等计算出这个月内所有的发放组合类型,并统计出满足于各组合的用户数量--
郁闷死了,还要结合数学知识的组合知识,好难啊
 

解决方案 »

  1.   

    sql server中有PIVOT关系运算符可以用
      

  2.   

    用户数是:满足某种组合的用户的数量
    用户编号  发放类型 发放时间

    00001      a        2010-01-01 
    00001      b        2010-01-05 
    00001      f        2010-01-07
     00002      a        2010-02-01 
    00002      c        2010-01-07 
    00002      e        2010-01-11
    00002      a        2010-01-01 
    .........比如说,用户 00001,在2010-01月份,发放了a,b,f三种补助
    那么, 发放a,b,f三种的组合就包含00001用户,这一种组合的用户数里就应该加上1
      

  3.   

    好的,可能我子啊帖子里是说明白问题的内容一下举一个发放补助的例子,
    补助假设有两种国家补助100,地方补助60  分别以a,b代替。--详细的发放记录表
    用户编号  发放补助  发放时间
    00001      a        2010-01-01 
    00001      b        2010-01-05 
     
    00002      a        2010-01-01
     
    00003      a        2010-01-07 
    00003      b        2010-01-1100004      a        2010-01-01 00005      b       2010-01-01 如上:
    发放了国家补助和地方补助的有00001和00003两个人
    只发放了国家补助的有00002,00004两个人
    只发放了地方补助的有00005一个人

    :最后希望得到的查询结果如下:

    月份      发放a      发放b            用户数
    2010-1-1  是           否               2
    2010-1-1  否           是               1
    2010-1-1  是           是               2
      

  4.   


    create table #a
    (
    customerid varchar(10)
    ,ftype varchar(1)
    ,ftime datetime
    )insert #a
    select '00001','a','2010-01-01'
    union
    select '00001','b','2010-01-05' 
    union
    select '00001','f','2010-01-07'
    union
    select '00002','a','2010-02-01' 
    union
    select '00002','c','2010-01-07' 
    union
    select '00002','e','2010-01-11'
    union
    select '00002','a','2010-01-01' 
    select customerid,ftime
    ,a=case when ftype= 'a' then 1 else 0 end
    ,b=case when ftype= 'b' then 1 else 0 end
    ,c=case when ftype= 'c' then 1 else 0 end
    ,d=case when ftype= 'd' then 1 else 0 end
    ,e=case when ftype= 'e' then 1 else 0 end
    ,f=case when ftype= 'f' then 1 else 0 end
    from #a
    select ftime
    ,a=case when sum(a) > 0 then 'Y' else 'N' end
    ,b=case when sum(b) > 0 then 'Y' else 'N' end
    ,c=case when sum(c) > 0 then 'Y' else 'N' end
    ,d=case when sum(d) > 0 then 'Y' else 'N' end
    ,e=case when sum(e) > 0 then 'Y' else 'N' end
    ,f=case when sum(f) > 0 then 'Y' else 'N' end
    ,ccustomer = count(customerid)
    from 
    (
    select customerid,ftime
    ,a=sum(a)
    ,b=sum(b)
    ,c=sum(c)
    ,d=sum(d)
    ,e=sum(e)
    ,f=sum(f)
    from
    (
    select customerid,ftime=convert(varchar(4),year(ftime))+'-'+convert(varchar(2),month(ftime))
    ,a=case when ftype= 'a' then 1 else 0 end
    ,b=case when ftype= 'b' then 1 else 0 end
    ,c=case when ftype= 'c' then 1 else 0 end
    ,d=case when ftype= 'd' then 1 else 0 end
    ,e=case when ftype= 'e' then 1 else 0 end
    ,f=case when ftype= 'f' then 1 else 0 end
    from #a
    ) as aa
    group by customerid,ftime) as tab
    group by ftimedrop table #a
      

  5.   


    CREATE TABLE [dbo].[Table_1](
    [用户编号] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    [发放类型] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
    [发放时间] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL
    ) ON [PRIMARY]insert into Table_1
     select '00001','a','2010-01-01'
    union
     select '00001','b','2010-01-05' 
    union
     select '00001','f','2010-01-07' 
    union
     select '00002','a','2010-02-01' 
    union
     select '00002','c','2010-01-07' 
    union
     select '00002','e','2010-01-11'
    union
     select '00002','a','2010-01-01'declare TCURSOR CURSOR  FOR select distinct 发放类型 from Table_1 ;
    declare @str varchar(max),@type varchar(max),@temp varchar(max),@sql varchar(max)
    begin
     set @type='';
     OPEN TCURSOR 
    FETCH NEXT FROM TCURSOR INTO @temp;
                while @@FETCH_STATUS=0
                begin
                     set @type=@type+'(case (select count(*) from dbo.Table_1 where 发放时间=t0.发放时间 and 发放类型='+''''+@temp+''''+') when 0 then ''否'' else ''是'' end )'+ ' as 发放'+@temp+',';
                     FETCH NEXT FROM TCURSOR INTO @temp;
                end
    CLOSE TCURSOR; 
    DEALLOCATE TCURSOR;
    set @type=@type+'(select count(distinct(用户编号)) from  dbo.Table_1  where 发放时间=t0.发放时间) as 用户数';
    set @sql='select 发放时间,'+ @type +' from table_1 t0 group by 发放时间';
    exec (@sql);                                
    end2010-01-01 是 否 否 否 否 2
    2010-01-05 否 是 否 否 否 1
    2010-01-07 否 否 是 否 是 2
    2010-01-11 否 否 否 是 否 1
    2010-02-01 是 否 否 否 否 1