关于一个报表(样式如下),我的表设计如下:产品表:
T_Product {pid,code,name,…};
1, zs001, 产品A
2,zs002, 产品B
3,zs003, 产品C产品使用表:
T_ProudctByUse {id,pid,csutList,date};
1, 1, '1,3', 2011-05-06
2, 3, '2'  , 2011-03-06
3, 2, '2,3', 2011-02-12客户表:
T_Customers{cid,name,city…};
1, 张三, 湖北
2, 李四, 广西
3,王五, 湖南现在要实现如下报表样式:
      产品A  产品B  产品C
张三   √    
李四      √     √
王五   √     √思考了半天不知道这个SQL语句怎么写,同时感觉表的设计有一些问题,请SQL高手指导一下,谢谢了。

解决方案 »

  1.   


    --按产品表固定--产品表:
    create table T_Product (pid int,code varchar(10),[name] varchar(15))
    insert into T_Product
    select 1, 'zs001', '产品A' union all
    select 2, 'zs002', '产品B' union all
    select 3, 'zs003', '产品C'--产品使用表:
    create table T_ProudctByUse (id int,pid int,csutList varchar(10),date datetime)
    insert into T_ProudctByUse
    select 1, 1, '1,3', '2011-05-06' union all
    select 2, 3, '2' , '2011-03-06' union all
    select 3, 2, '2,3', '2011-02-12'--客户表:
    create table T_Customers (cid int,[name] varchar(10),city varchar(10))
    insert into T_Customers
    select 1,'张三','湖北' union all
    select 2,'李四','广西' union all
    select 3,'王五','湖南'
    goselect c.[name] as cname,max(case a.[name] when '产品A' then '√' else '' end) as [产品A],
    max(case a.[name] when '产品B' then '√' else '' end) as [产品B],
    max(case a.[name] when '产品C' then '√' else '' end) as [产品C]
    from T_Customers c left join T_ProudctByUse b on charindex(','+ltrim(c.cid)+',',','+b.csutList+',') > 0
    left join T_Product a on b.pid = a.pid
    group by c.[name]drop table T_Product,T_ProudctByUse,T_Customers
      

  2.   


    /*
    cname      产品A  产品B  产品C
    ---------- ---- ---- ----
    李四              √    √
    王五         √    √    
    张三         √         (3 行受影响)
      

  3.   


    在产品使用表里面有一个csutList 存放着客户的id用','分开。
      

  4.   


    --按动态的,不过不知道报表怎么生成动态列!--产品表:
    create table T_Product (pid int,code varchar(10),[name] varchar(15))
    insert into T_Product
    select 1, 'zs001', '产品A' union all
    select 2, 'zs002', '产品B' union all
    select 3, 'zs003', '产品C'--产品使用表:
    create table T_ProudctByUse (id int,pid int,csutList varchar(10),date datetime)
    insert into T_ProudctByUse
    select 1, 1, '1,3', '2011-05-06' union all
    select 2, 3, '2' , '2011-03-06' union all
    select 3, 2, '2,3', '2011-02-12'--客户表:
    create table T_Customers (cid int,[name] varchar(10),city varchar(10))
    insert into T_Customers
    select 1,'张三','湖北' union all
    select 2,'李四','广西' union all
    select 3,'王五','湖南'
    godeclare @sql varchar(8000)
    set @sql = 'select c.[name]'
    select @sql = @sql + ',max(case a.[name] when ''' + [name] + ''' then ''√'' else '''' end) as [' + [name] + ']'
    from (select distinct [name] from T_Product)t
    select @sql = @sql + ' from T_Customers c left join T_ProudctByUse b on charindex('',''+ltrim(c.cid)+'','','',''+b.csutList+'','') > 0
    left join T_Product a on b.pid = a.pid group by c.[name]'
    exec(@sql)drop table T_Product,T_ProudctByUse,T_Customers
    /*name       产品A  产品B  产品C
    ---------- ---- ---- ----
    李四              √    √
    王五         √    √    
    张三         √         (3 行受影响)