首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

交叉表的有关问题!鱼鱼师傅不知道在不在? 帮忙解决一下

2012-03-05 
交叉表的问题!鱼鱼师傅不知道在不在???? 帮忙解决一下createtabletable_goods(idvarchar(10),visitint,goo

交叉表的问题!鱼鱼师傅不知道在不在???? 帮忙解决一下
create       table       table_goods   (id       varchar(10),visit   int,goods_id   varchar(10),goods   int   ,name   varchar(30))      
    insert       table_goods            
              select         '196 '   ,   1   ,   'a01 ',   1   , '衣服 '    
    union           all               select         '196 '   ,1   ,   'a02 ',   2   ,   '裤子 '
    union           all               select         '196 '   ,1   ,   'a03 ',   3   ,   '帽子 '
    union           all               select         '196 '   ,2   ,   'a04 ',   1   ,   '鞋 '  
    union           all               select         '196 '   ,2   ,   'a05 ',   2   ,   '袜子 '      
    union           all               select         '196 '   ,2   ,   'a06 ',   3   ,   '皮带 '      
    union           all               select         '196 '   ,2   ,   'a07 ',   4   ,   '衣服 '  
    union           all               select         '566 '   ,1   ,   'b08 ',   1   ,   '钢笔 '      
    union           all               select         '566 '   ,2   ,   's09 ',   1   ,   '啤酒 '      
    union           all               select         '566 '   ,3   ,   'g01 ',   1   ,   '螺丝刀 '  

    id                 表示用户
    visit           表示反问次数  
    goods_id     表示发生物品ID
    goods           表示发生顺序
    mame             表示发生   物品名称

id           visit         goods_id       goods       cname
1961                 a01             1               衣服
1961a022               裤子
1961a033               帽子
1962a041               鞋
1962a052               袜子


1962a063               皮带
1962a074               衣服
5661b081               钢笔
5662s091               啤酒
5663g011               螺丝刀

  变成下表

id           visit     goods_id1       goods1       goods_id2         goods2       goods_id3         goods3       goods_id4       goods4
1961a01                 衣服             a02                   裤子             a03                   帽子               NULL             NULL  
1962a04                 鞋           a05                   袜子       a06                   皮带     a07               衣服
5661b08                 钢笔           NULL                 NULL             NULL                 NULL               NULL             NULL  
5662s09                 啤酒           NULL                 NULL             NULL                 NULL               NULL             NULL                                                  
5663g01                 螺丝刀         NULL                 NULL             NULL                 NULL               NULL             NULL                

  declare   @str   varchar(1000)
  set   @str= 'select   id,visit   '
  select   @str=@str+ ',min(case   when   goods= ' ' '+   Cast(goods   As   Varchar)   + ' ' '   then   name   else   Null   end)   as   goods ' ' '+   Cast(goods   As   Varchar)   + ' ' ' '
  from   t   group   by   goods
  select   @str=@str+ '   from   t   group   by   id,visit '
  exec(@str)
--   这里的goods   是   INT   但是   name   是VARCHAR   这里的   cast(goods   as   varchar)   类型转换会出错   现在如果在加一列   goods_id   该怎么写啊?
      求动态语句   和   静态语句   的写法。。。。我在这里先   谢谢了!!



[解决办法]
如下修改即可

declare @str varchar(1000)
set @str= 'select id,visit '
select @str=@str+ ',min(case when goods= '+ Cast(goods As Varchar) + ' then goods_id else Null end) as goods_id '+ Cast(goods As Varchar)
+ ',min(case when goods= '+ Cast(goods As Varchar) + ' then name else Null end) as goods '+ Cast(goods As Varchar)
from table_goods group by goods
select @str=@str+ ' from table_goods group by id,visit order by id,visit '
exec(@str)

热点排行