1、有一个尺码颜色表结构如下:颜色尺码   小码   中码   大码
红          10件    10件   10件                     
黄          20件    15件   10件                     
蓝          30件    10件   18件2、以上表中的尺码不固定,有些是3个码,有些是4个码或更多尺码。以上表中的颜色也是不固定,以下举例说明:颜色尺码   小码   中码   大码      加大码
红          10件    10件   10件     20件                
黄          20件    15件   10件     25件                
蓝          30件    10件   18件     30件
粉色        20件    15件   10件     25件                 3、以上表目前是在Excel中处理,所以对于例和行不固定的情况下很容易就能处理,现在想用SQL来管理这些数据,请问这个表应该怎么设计?说明:这个表其实是反应每一款产品每一种颜色每一种尺码有多少数量,也会经常更新,有时会增加颜色或者增加尺码等。

解决方案 »

  1.   

    --建立测试环境
    set nocount on
    create table test(颜色 varchar(20),尺码 varchar(20),数量 int)
    insert into test select '红','小','10'
    insert into test select '红','大','10'
    insert into test select '黄','中','10'
    insert into test select '粉','加大','25'
    insert into test select '黄','大','28'
    go
    --测试
    declare @sql varchar(8000)
    set @sql='select 颜色 '
    select @sql=@sql+',sum(case when 尺码='''+尺码+''' then 数量 else 0 end)['+尺码+']' 
    from (select distinct 尺码 from test)aset @sql=@sql+' from test group by 颜色'
    exec(@sql)
    --删除测试环境
    drop table test
     set nocount off/*--
    颜色                   大           加大          小           中
    -------------------- ----------- ----------- ----------- -----------
    粉                    0           25          0           0
    红                    10          0           10          0
    黄                    28          0           0           10--*/
      

  2.   

    Product
    ID    Name 
    1     Prod-A
    2     Prod-BColor
    ID    Name
    1     红
    2     黄
    3     蓝Size
    ID   Name
    1    大码
    2    中码ProductColor
    ID  ProductID   ColorID
    1   1(Prod-A)   1(红) 
    2   1(Prod-A)   2(蓝)  ProductSize
    ID  ProductID   SizeID
    1   1(Prod-A)   1(大码) 
    2   1(Prod-A)   2(中码)ProductRequisitionDetail
    ID   ProductColorID    ProductSizeID   Qty
    1    1(Prod-A-红)      1(Prod-A-大码)  30
    2    1(Prod-A-红)      2(Prod-A-中码)  60 
      

  3.   


    我已经试过动态SQL的方法,和你的结果一样,列的顺序无法控制,比如我要按小中大来排列,在具体应用中可能是S M L 或8 10 12 14等。如果顺序不对,看起来不直观。
      

  4.   


    你这种方法我也试过了,这样的数据与我上面的格式完全不同,所以用户不能接受,然后我在这种设计的基础上还采用1楼朋友的那种动态SQL的处理方法,因为无法控制顺序,所以要另外想办法。
      

  5.   

    --建立测试环境
    set nocount on
    create table test(颜色 varchar(20),尺码 varchar(20),数量 int)
    insert into test select '红','小','10'
    insert into test select '红','大','10'
    insert into test select '黄','中','10'
    insert into test select '粉','加大','25'
    insert into test select '黄','大','28'
    go
    create table chima(尺码 varchar(20),rowindex int)
    insert into chima select '小',1
    insert into chima select '中',2
    insert into chima select '大',3
    insert into chima select '加大',4
    go
    --测试
    declare @sql varchar(8000)
    set @sql='select 颜色 '
    select @sql=@sql+',sum(case when 尺码='''+尺码+''' then 数量 else 0 end)['+尺码+']' 
    from (select  distinct a.尺码,rowindex from test a inner join chima b on a.尺码=b.尺码
     )a order by rowindexset @sql=@sql+' from test group by 颜色'
    exec(@sql)
    --删除测试环境
    drop table test
    drop table chima
     set nocount off/*--
    颜色                   小           中           大           加大
    -------------------- ----------- ----------- ----------- -----------
    粉                    0           0           0           25
    红                    10          0           10          0
    黄                    0           10          28          0--*/