表(不设主键)结构如下所示:
商店名      商品名    销售额
第一百货    彩电      1600 
第一百货    冰箱      1700
第一百货    照相机    1800
第二百货    彩电      1600 
第二百货    冰箱      1700
第二百货    照相机    1800我现在使用sql语句进行查询得到如下结果:
商店名     销售额第1位商品名  销售额第2位商品名   1,2位销售额合计  合计值占总销售额的比例(四舍五入)
第一百货   照相机             冰箱                3500             69
第二百货   照相机             冰箱                3500             69问题是sql语句该怎么写,有请各位大大指教。谢谢。

解决方案 »

  1.   

    --> 测试数据: [tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb] (商店名 varchar(8),商品名 varchar(6),销售额 int)
    insert into [tb]
    select '第一百货','彩电',1600 union all
    select '第一百货','冰箱',1700 union all
    select '第一百货','照相机',1800 union all
    select '第二百货','彩电',1600 union all
    select '第二百货','冰箱',1700 union all
    select '第二百货','照相机',1800
    go
    select 商店名,销售额第1位商品名=max(case mc when 1 then 商品名 else null end),
    销售额第2位商品名=max(case mc when 2 then 商品名 else null end),
    [1,2位销售额合计]=sum(case when mc in(1,2) then 销售额 end),
    合计值占总销售额的比例=cast(round(sum(case when mc in(1,2) then 销售额 end)*100.0/sum(销售额),0) as int)
     from
    (select *,mc=(select count(1) from tb where 商店名=a.商店名 and 销售额>=a.销售额) from [tb] a)a
    group by 商店名
    --结果:
    商店名      销售额第1位商品名 销售额第2位商品名 1,2位销售额合计   合计值占总销售额的比例
    -------- --------- --------- ----------- -----------
    第二百货     照相机       冰箱        3500        69
    第一百货     照相机       冰箱        3500        69
      

  2.   

    --> 测试数据:[ta]
    if object_id('[ta]') is not null drop table [ta]
    go
    create table [ta]([商店名] varchar(8),[商品名] varchar(6),[销售额] int)
    insert [ta]
    select '第一百货','彩电',1600 union all
    select '第一百货','冰箱',1700 union all
    select '第一百货','照相机',1800 union all
    select '第二百货','彩电',1600 union all
    select '第二百货','冰箱',1700 union all
    select '第二百货','照相机',1800--------------------------------查询开始------------------------------
    ;with a as
    (
    select id=row_number() over(partition by[商店名] order by [销售额] desc),* from  [ta] 
    )
    ,b as
    (
    select [商店名] , 
    (select[商品名] from a where id=1 and [商店名]=t.[商店名])as  [销售额第1位商品名] ,
    (select[商品名] from a where id=2 and [商店名]=t.[商店名]) as[销售额第2位商品名],
    sum([销售额]) as [1,2位销售额合计],
    ceiling((select sum(t.[销售额])*100.00/ sum([销售额])from [ta] where [商店名]=t.[商店名] group by [商店名])) as[合计值占总销售额的比例]
    from a as t where id<=2
    group by [商店名]
    )
    select * from b
    /*
    商店名      销售额第1位商品名 销售额第2位商品名 1,2位销售额合计   合计值占总销售额的比例
    -------- --------- --------- ----------- ---------------------------------------
    第二百货     照相机       冰箱        3500        69
    第一百货     照相机       冰箱        3500        69(2 行受影响)
    */
      

  3.   

    上个是05的 这个是2000的--> 测试数据:[ta]
    if object_id('[ta]') is not null drop table [ta]
    go
    create table [ta]([商店名] varchar(8),[商品名] varchar(6),[销售额] int)
    insert [ta]
    select '第一百货','彩电',1600 union all
    select '第一百货','冰箱',1700 union all
    select '第一百货','照相机',1800 union all
    select '第二百货','彩电',1600 union all
    select '第二百货','冰箱',1700 union all
    select '第二百货','照相机',1800--------------------------------查询开始------------------------------
    --2000
    select [商店名] , 
    max(case id when 1 then [商店名] else null end) as [销售额第1位商品名] ,
    max(case id when 2 then [商店名] else null end) as [销售额第2位商品名],
    sum([销售额]) as [1,2位销售额合计],
    ceiling((select sum(t.[销售额])*100.00/ sum([销售额])from [ta] where [商店名]=t.[商店名] group by [商店名])) as[合计值占总销售额的比例]
    from 
    (
    select *,id=(select count(1)+1 from ta where [商店名]=a.[商店名] and [销售额]>a.[销售额] )from ta a
    ) t where id<=2
    group by [商店名]
    /*
    商店名      销售额第1位商品名 销售额第2位商品名 1,2位销售额合计   合计值占总销售额的比例
    -------- --------- --------- ----------- ---------------------------------------
    第二百货     照相机       冰箱        3500        69
    第一百货     照相机       冰箱        3500        69(2 行受影响)
    */