表1:
itemid  --物品ID
itemprice  --物品价格
buytime    --购买时间表2:
itemid   --物品ID
itemtype --物品种类现在要得到每个种类在一段时间内的销售比重比如对表2进行搜索select itmeid,itemtype from 表2 order by itemtype
得到结果如下:
itemid   itemtype
4758 1
1157 1
1255 1
1256 1
3458 1
3460 1
3711 1
3737 1
3738 1
3949 1
3988 1
4310 1
4819 1
4230 1
4308 1
4480 1
4481 1
1154 1
1250 1
1047 1
1043 1
1003 1
1051 1
1153 1
1150 1
6416 2
3705 2
404 2
344 2
6417 2
4108 2
406 2
405 2
403 2
4106 2
402 2
383 2
401 2
5957 2
382 2
5524 2
6726 3
6625 3
6270 3
6825 3
6268 3
6269 3
6307 3
7026 3
7125 3
7825 3
7826 3
7925 3
7926 3
7025 3
6926 3
7126 3
6626 3
6725 3
6826 3
6925 3
10308 4
10309 4
4535 4
10306 4
4929 4
10307 4
4107 4
10310 4
4171 4
4172 4
4173 4
4174 4
4183 4
4185 4
4186 4
4187 4
4184 4
然后根据ID到表1中差找各种类销售比重下面的Sql不会写了,谁能指点我一下啊?

解决方案 »

  1.   

    然后根据ID到表1中差找各种类销售比重:贴建表及插入记录的SQL,及要求结果出来看看
      

  2.   

    select 表2.itemtype,count(*) /c.cnt as 销售比重
    from 表1 ,表2, (select Count(*) as CNT from 表1 where 表1.buytime between '2011-09-01' and '2011-10-01') c
    where 表1.itemid=表2.itemid
    And 表1.buytime between '2011-09-01' and '2011-10-01'
    group by 表2.itemtype
      

  3.   

    要求结果:
    type    gold    bizhong
    1       3333    10%
    2       2222    5%
    ....
      

  4.   

    select 表2.itemid,表2.itemtype,concat(count(*) *100/c.cnt,'%') as 销售比重
    from 表1 ,表2, (select Count(*) as CNT from 表1 where 表1.buytime between '2011-09-01' and '2011-10-01') c
    where 表1.itemid=表2.itemid
    And 表1.buytime between '2011-09-01' and '2011-10-01'
    group by 表2.itemid,表2.itemtype
      

  5.   

    建议
    贴建表及插入记录的SQL,及要求结果出来看看
    你的数据与要求结果不匹配?