现有近10年的商品历史销量表,每个表的结构一样,如下所示:2000年销量表
商品编码    销量   
033001       52000
033002       48000
2001年销量表
商品编码    销量   
033001       72000
033002       68000
2002年销量表
商品编码    销量   
033001       92000
033002       118000
.
.
.
2009年销量表
商品编码    销量   
033001       152000
033002       178000现想查出这10年中年年都处于年度单品销量贡献比排名中倒数100名的商品编码。年度单品销量贡献比=年度单品销量/年度所有商品销量*100%。查询语句越简洁越好,最好是一条语句就解决问题 :) 先谢各位啦!

解决方案 »

  1.   

    select t1.商品编码 from
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from 2000年销量表) bfb from 2000年销量表 t order by bfb) t1,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from 2001年销量表) bfb from 2001年销量表 t order by bfb) t2,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from 2002年销量表) bfb from 2002年销量表 t order by bfb) t3,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from 2003年销量表) bfb from 2003年销量表 t order by bfb) t4,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from 2004年销量表) bfb from 2004年销量表 t order by bfb) t5,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from 2005年销量表) bfb from 2005年销量表 t order by bfb) t6,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from 2006年销量表) bfb from 2006年销量表 t order by bfb) t7,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from 2007年销量表) bfb from 2007年销量表 t order by bfb) t8,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from 2008年销量表) bfb from 2008年销量表 t order by bfb) t9,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from 2009年销量表) bfb from 2009年销量表 t order by bfb) t10
    where t1.商品编码 = t2.商品编码 and
          t1.商品编码 = t3.商品编码 and
          t1.商品编码 = t4.商品编码 and
          t1.商品编码 = t5.商品编码 and
          t1.商品编码 = t6.商品编码 and
          t1.商品编码 = t7.商品编码 and
          t1.商品编码 = t8.商品编码 and
          t1.商品编码 = t9.商品编码 and
          t1.商品编码 = t10.商品编码
      

  2.   

    select top 100 商品编码,max(年度单品销量贡献比) 年度单品销量贡献比 from 
    (
    select top 100 商品编码,销量*100.0/(select sum(销量) from 2000年销量) as 年度单品销量贡献比 from 2000年销量表
    union all
    select top 100 商品编码,销量*100.0/(select sum(销量) from 2001年销量) as 年度单品销量贡献比 from 2001年销量表
    union all
    select top 100 商品编码,销量*100.0/(select sum(销量) from 2002年销量) as 年度单品销量贡献比 from 2002年销量表
    union all
    select top 100 商品编码,销量*100.0/(select sum(销量) from 2003年销量) as 年度单品销量贡献比 from 2003年销量表
    union all
    select top 100 商品编码,销量*100.0/(select sum(销量) from 2004年销量) as 年度单品销量贡献比 from 2004年销量表
    union all
    select top 100 商品编码,销量*100.0/(select sum(销量) from 2005年销量) as 年度单品销量贡献比 from 2005年销量表
    union all
    select top 100 商品编码,销量*100.0/(select sum(销量) from 2006年销量) as 年度单品销量贡献比 from 2006年销量表
    union all
    select top 100 商品编码,销量*100.0/(select sum(销量) from 2007年销量) as 年度单品销量贡献比 from 2007年销量表
    union all
    select top 100 商品编码,销量*100.0/(select sum(销量) from 2008年销量) as 年度单品销量贡献比 from 2008年销量表
    union all
    select top 100 商品编码,销量*100.0/(select sum(销量) from 2009年销量) as 年度单品销量贡献比 from 2009年销量表
    ) TT
    group by 商品编码
    order by 年度单品销量贡献比 asc
      

  3.   

    select * from (select top 100 商品编码 from [2000年销量表] order by 销量/(select sum(销量) from [2000年销量表]) desc) a
    union
    select * from (select top 100 商品编码 from [2001年销量表] order by 销量/(select sum(销量) from [2001年销量表]) desc) b
    union
    select * from (select top 100 商品编码 from [2002年销量表] order by 销量/(select sum(销量) from [2002年销量表]) desc) c
    union
    select * from (select top 100 商品编码 from [2003年销量表] order by 销量/(select sum(销量) from [2003年销量表]) desc) d
    union
    select * from (select top 100 商品编码 from [2004年销量表] order by 销量/(select sum(销量) from [2004年销量表]) desc) e
    union
    select * from (select top 100 商品编码 from [2005年销量表] order by 销量/(select sum(销量) from [2005年销量表]) desc) f
    union
    select * from (select top 100 商品编码 from [2006年销量表] order by 销量/(select sum(销量) from [2006年销量表]) desc) g
    union
    select * from (select top 100 商品编码 from [2007年销量表] order by 销量/(select sum(销量) from [2007年销量表]) desc) h
    union
    select * from (select top 100 商品编码 from [2008年销量表] order by 销量/(select sum(销量) from [2008年销量表]) desc) i
    union
    select * from (select top 100 商品编码 from [2009年销量表] order by 销量/(select sum(销量) from [2009年销量表]) desc) j
      

  4.   


    select [year], 商品编码,销量,rate,count(*) as [cnt]
    from 
    (
    (select top 100 2000 as [year],商品编码,销量,1.0*销量/total as rate
    from T1 ,(select sum(销量) as total from T1) A
    order by rate) X1
    union all
    (select top 100 2001 as [year],商品编码,销量,1.0*销量/total as rate
    from T1 ,(select sum(销量) as total from T2) A
    order by rate) X2
    union all
    (select top 100 2002 as [year],商品编码,销量,1.0*销量/total as rate
    from T1 ,(select sum(销量) as total from T3) A
    order by rate) X3
    --一直union all 下去
    ) XX
    group by [year], 商品编码 ,销量,rate
    having count(*)= 10 
      

  5.   

    要年年处于倒数100,所以要having 刷选一下的
      

  6.   

    --理解错,应该是这样
    select 商品编码
    from
    (
    select * from (select top 100 商品编码 from [2000年销量表] order by 销量/(select sum(销量) from [2000年销量表]) desc) a
    union all
    select * from (select top 100 商品编码 from [2001年销量表] order by 销量/(select sum(销量) from [2001年销量表]) desc) b
    union all
    select * from (select top 100 商品编码 from [2002年销量表] order by 销量/(select sum(销量) from [2002年销量表]) desc) c
    union all
    select * from (select top 100 商品编码 from [2003年销量表] order by 销量/(select sum(销量) from [2003年销量表]) desc) d
    union all
    select * from (select top 100 商品编码 from [2004年销量表] order by 销量/(select sum(销量) from [2004年销量表]) desc) e
    union all
    select * from (select top 100 商品编码 from [2005年销量表] order by 销量/(select sum(销量) from [2005年销量表]) desc) f
    union all
    select * from (select top 100 商品编码 from [2006年销量表] order by 销量/(select sum(销量) from [2006年销量表]) desc) g
    union all
    select * from (select top 100 商品编码 from [2007年销量表] order by 销量/(select sum(销量) from [2007年销量表]) desc) h
    union all
    select * from (select top 100 商品编码 from [2008年销量表] order by 销量/(select sum(销量) from [2008年销量表]) desc) i
    union all
    select * from (select top 100 商品编码 from [2009年销量表] order by 销量/(select sum(销量) from [2009年销量表]) desc) j
    ) t
    group by 商品编码
    having count(1) = 10
      

  7.   

    由于你的表名是数字开头,需要加上中括号.select t1.商品编码 from
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2000年销量表]) bfb from [2000年销量表] t order by bfb) t1,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2001年销量表]) bfb from [2001年销量表] t order by bfb) t2,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2002年销量表]) bfb from [2002年销量表] t order by bfb) t3,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2003年销量表]) bfb from [2003年销量表] t order by bfb) t4,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2004年销量表]) bfb from [2004年销量表] t order by bfb) t5,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2005年销量表]) bfb from [2005年销量表] t order by bfb) t6,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2006年销量表]) bfb from [2006年销量表] t order by bfb) t7,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2007年销量表]) bfb from [2007年销量表] t order by bfb) t8,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2008年销量表]) bfb from [2008年销量表] t order by bfb) t9,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2009年销量表]) bfb from [2009年销量表] t order by bfb) t10
    where t1.商品编码 = t2.商品编码 and
          t1.商品编码 = t3.商品编码 and
          t1.商品编码 = t4.商品编码 and
          t1.商品编码 = t5.商品编码 and
          t1.商品编码 = t6.商品编码 and
          t1.商品编码 = t7.商品编码 and
          t1.商品编码 = t8.商品编码 and
          t1.商品编码 = t9.商品编码 and
          t1.商品编码 = t10.商品编码
    他们用的union all也行.
      

  8.   


    nianran520 ,你的语句里,having count(1)=10 ,是什么意思?
      

  9.   

    select t1.商品编码 from
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2000年销量表]) bfb from [2000年销量表] t order by bfb) t1,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2001年销量表]) bfb from [2001年销量表] t order by bfb) t2,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2002年销量表]) bfb from [2002年销量表] t order by bfb) t3,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2003年销量表]) bfb from [2003年销量表] t order by bfb) t4,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2004年销量表]) bfb from [2004年销量表] t order by bfb) t5,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2005年销量表]) bfb from [2005年销量表] t order by bfb) t6,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2006年销量表]) bfb from [2006年销量表] t order by bfb) t7,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2007年销量表]) bfb from [2007年销量表] t order by bfb) t8,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2008年销量表]) bfb from [2008年销量表] t order by bfb) t9,
    (select top 100 * 商品编码 , 销量 , 销量 * 100.0 / (select sum(销量) from [2009年销量表]) bfb from [2009年销量表] t order by bfb) t10
    where t1.商品编码 = t2.商品编码 and
          t1.商品编码 = t3.商品编码 and
          t1.商品编码 = t4.商品编码 and
          t1.商品编码 = t5.商品编码 and
          t1.商品编码 = t6.商品编码 and
          t1.商品编码 = t7.商品编码 and
          t1.商品编码 = t8.商品编码 and
          t1.商品编码 = t9.商品编码 and
          t1.商品编码 = t10.商品编码