有表 info_job(id,station,min_salary,max_salary)我想知道按职位(station)分组,分组数量大于2的min_salary,max_salary(要求min_salary和max_salary:分组统计时按值升序,为奇数取中间数,为偶数取中间两数平均数)
结果是:
1 程序员 2000   5000
2 销售   2500  4000
3 会计   1500  6000非常感谢。

解决方案 »

  1.   

    这个用一条sql吗,不太现实额!
    用T-SQL慢慢编程吧,^_^!
      

  2.   

    是啊  就是用T-SQL  不怎么熟悉数据库  所以希望大家能帮助
      

  3.   

    create table info_job(id int,station varchar(8),min_salary int,max_salary int)
    insert info_job
    select 1 ,'程序员', 2000 ,5000  union all
    select 2 ,'销售', 2800 ,4000  union all
    select 3 ,'会计', 2300 ,6000  union all
    select 4 ,'销售', 2000 ,5000    union all
    select 5 ,'销售', 2500 ,4000  union all
    select 6 ,'会计', 1500 ,6000  union all
    select 7 ,'程序员', 3000 ,5000    union all
    select 8 ,'财务', 2500 ,4000  union all
    select 9 ,'会计', 5500 ,6000
    ;with cte as 
    (
    select *,
    min1=row_number() over (partition by station order by min_salary), 
    max1=row_number() over (partition by station order by max_salary) 
    from
    info_job
    )
    select station,
    (case when count(*)%2=0 
          then (select sum(min_salary)/2 from cte b 
                where a.station=b.station 
                and b.min1 between max(a.min1)/2 and max(a.min1)/2+1)
      else
    (select min_salary from cte b 
                where a.station=b.station 
                and b.min1= max(a.min1)/2+1)
    end) as min_salary,
    (case when count(*)%2=0 
          then (select sum(max_salary)/2 from cte b 
                where a.station=b.station 
                and b.max1 between max(a.max1)/2 and max(a.max1)/2+1)
      else
    (select max_salary from cte b 
                where a.station=b.station 
                and b.max1= max(a.max1)/2+1)
    end) as max_salary
    from 
    cte a
    group by station
    having count(*)>1
    /*
    station  min_salary  max_salary
    -------- ----------- -----------
    程序员      2500        5000
    会计       2300        6000
    销售       2500        4000(3 行受影响)
    */
      

  4.   


    create table info_job(id int,station varchar(8),min_salary int,max_salary int)
    insert info_job
    select 1 ,'程序员', 2000 ,5000  union all
    select 2 ,'销售', 2800 ,4000  union all
    select 3 ,'会计', 2300 ,6000  union all
    select 4 ,'销售', 2000 ,5000    union all
    select 5 ,'销售', 2500 ,4000  union all
    select 6 ,'会计', 1500 ,6000  union all
    select 7 ,'程序员', 3000 ,5000    union all
    select 8 ,'财务', 2500 ,4000  union all
    select 9 ,'会计', 5500 ,6000goselect aa.station
    ,case when bb._count%2=0 then bb.min_salary else aa.min_salary end
    ,case when bb._count%2=0 then bb.max_salary else aa.max_salary end
    from
    (
    select station,row_number() over(partition by station order by min_salary) ids,min_salary,max_salary
    from info_job bb group by station,min_salary,max_salary
    ) aa
    inner join
    (
    select station,count(*) _count,count(*)/2+1 ids,avg(min_salary) min_salary,avg(max_salary) max_salary from info_job group by station having count(*) > 1 
    )bb on aa.station = bb.station and aa.ids = bb.ids
      

  5.   

    谢谢大家 嗯  我想把min_salary+max_salary的和求出来  可她总是报不能使用别名  该怎么解决呢 
      

  6.   

    select aa.station
        ,case when bb._count%2=0 then bb.min_salary else aa.min_salary end
        ,case when bb._count%2=0 then bb.max_salary else aa.max_salary end
        from
        (
            select station,row_number() over(partition by station order by min_salary) ids,min_salary,max_salary
            from info_job bb group by station,min_salary,max_salary
        ) aa
        inner join
        (
            select station,count(*) _count,count(*)/2+1 ids,avg(min_salary) min_salary,avg(max_salary) max_salary from info_job group by station having count(*) > 1 
        )bb on aa.station = bb.station and aa.ids = bb.ids这个是你回的啊   嗯  我想把min_salary+max_salary查出来  嗯 可不能使用别名  怎么解决呢
      

  7.   


    select aa.station
      ,case when bb._count%2=0 then bb.min_salary else aa.min_salary end a
      ,case when bb._count%2=0 then bb.max_salary else aa.max_salary end b,
    case when bb._count%2=0 then bb.min_salary+bb.max_salary else aa.min_salary+aa.max_salary end
      from
      (
      select station,row_number() over(partition by station order by min_salary) ids,min_salary,max_salary
      from info_job bb group by station,min_salary,max_salary
      ) aa
      inner join
      (
      select station,count(*) _count,count(*)/2+1 ids,avg(min_salary) min_salary,avg(max_salary) max_salary from info_job group by station having count(*) > 1  
      )bb on aa.station = bb.station and aa.ids = bb.ids
      

  8.   

    呵呵  第一次在csdn上面求助 谢谢你们  解决了