我在数据库表中的记录存放结构如下:
type Description COLOR SIZE Qty
G3020C Bg Comp Glove  Red L 288
G3020A Bg Comp Glove   Blk L 288
G3022C Bg Spt Glove  BLU M 144
G3022C Bg Spt Glove   BLU L 144
G3022D Bg Sport Glove  RED M 144
G3022D Bg Sport Glove  RED L 144
G3022E Bg Spt Glove  BLK S 72
G3022E Bg Spt Glove  BLK M 144
G3022E Bg Spt Glove   BLK L 144现在用户要求我将数据安以下格式显示在DBGRID中:
type Description COLOR S M L
G3020C Bg Comp Glove  Red             288
G3020A Bg Comp Glove   Blk 288
G3022C Bg Spt Glove  BLU 144 144
G3022D Bg Sport Glove  RED 144 144
G3022E Bg Spt Glove  BLK    72 144 144
这里只是部分数据,数据库表中SIZE的值不止这3个.
用户有意思就是将数据库中的isze的唯一值作为列,将QTY的值显示在对应SIZE值对就有列下.
不知道大家是否能看明白.肯请各位大侠帮忙看如何通过QUERY来实现.

解决方案 »

  1.   

    今天刚刚收藏了 马可的一段代码 你看看那对你有没有帮助呢?
    Create table test (name char(10),km char(10),cj int)
    go
    insert test values('张三','语文',80)
    insert test values('张三','数学',86)
    insert test values('张三','英语',75)
    insert test values('李四','语文',78)
    insert test values('李四','数学',85)
    insert test values('李四','英语',78)SELECT * FROM Testdeclare @sql varchar(8000)
    set @sql = 'select name'
    select @sql = @sql + ',sum(case km when '''+km+''' then cj end) ['+km+']' from (select distinct km from test) as a
    select @sql = @sql+' from test group by name'
    exec(@sql)
      

  2.   

    假定原表叫oldtable;新表叫newtable;
    第一步:
      insert into newtable(type ,Description,COLOR)
            select (type,Description,COLOR) from oldtable group by type, Description,COLOR第二步:
      update newtable set s = 
                  (select qty from oldtable a,newtable b 
                       where (a.type = b.type and 
                              a.description = b.description and
                              a.color = b.color and
                              a.size = 's'))第三步:
      update newtable set M = 
                  (select qty from oldtable a,newtable b 
                       where (a.type = b.type and 
                              a.description = b.description and
                              a.color = b.color and
                              a.size = 'M'))第四步:
      update newtable set L = 
                  (select qty from oldtable a,newtable b 
                       where (a.type = b.type and 
                              a.description = b.description and
                              a.color = b.color and
                              a.size = 'L'))
      

  3.   

    to  47522341(睡到8:30) 
    现在有一个问题就是我的原表oldtable中的SIZE的值有多少个是不确定的.
      

  4.   

    to angle097113(抵制日货,人人有责!) 
    我的数据库是Access好象不能用存贮过程吧!
      

  5.   

    不太会用ACCESS,如果用SQL Server我就生成临时表来对付。