联通笔试题,写给大家参考一下     表中内容                   查询结果          
   a    9     10              a    9    45
   b    9     15              b    9    40
   c    9     20              c    9    35
   d    9     10              d    9    45
   a    9.5   30              a    9.5  45
   b    9.5   20              b    9.5  55
   c    9.5   10              c    9.5  65
   d    9.5   15              d    9.5  60
   a    9.8   15              a    9.8  75
   b    9.8   20              b    9.8  70
   c    9.8   30              c    9.8  60
   d    9.8   25              d    9.8  65

解决方案 »

  1.   

    ???有规律吗?select column1,column2,case column3 when 9 then 55 - column3 when 9.5 then 75 - column3 when 9.8 then 90 - column3 end as column3
    from tablename
      

  2.   

    只看出這樣的規律Create Table TEST
    (a Char(1),
     b Numeric(10,1),
     c Int)
    Insert TEST Select    'a',    9,     10
    Union All Select   'b',    9,     15
    Union All Select   'c',    9,     20
    Union All Select   'd',    9,     10
    Union All Select   'a',    9.5,   30
    Union All Select   'b',    9.5,   20
    Union All Select   'c',    9.5,   10
    Union All Select   'd',    9.5,   15
    Union All Select   'a',    9.8,   15
    Union All Select   'b',    9.8,   20
    Union All Select   'c',    9.8,   30
    Union All Select   'd',    9.8,   25
    GO
    Select 
    a,
    b,
    (Case b When 9.0 Then 55-c When 9.5 Then 75-c When 9.8 Then 90-c End) As c
    From TEST
    GO
    Drop Table TEST
    --Result
    /*
    a b c
    a 9.0 45
    b 9.0 40
    c 9.0 35
    d 9.0 45
    a 9.5 45
    b 9.5 55
    c 9.5 65
    d 9.5 60
    a 9.8 75
    b 9.8 70
    c 9.8 60
    d 9.8 65
    */
      

  3.   

    看懂了...
    查询结果第3列是一个和,是表中第1列不同而第2列相同的行的第3列的和SELECT a.c1, a.c2, b.c3 - a.c3 AS c3
    FROM TEST a INNER JOIN
              (SELECT c2, SUM(c3) AS c3
             FROM test
             GROUP BY c2) b ON b.c2 = a.c2
      

  4.   

    declare @a Table(a Char(1),b Numeric(10,1),c Int)
    Insert @a Select    'a',    9,     10
    Union All Select   'b',    9,     15
    Union All Select   'c',    9,     20
    Union All Select   'd',    9,     10
    Union All Select   'a',    9.5,   30
    Union All Select   'b',    9.5,   20
    Union All Select   'c',    9.5,   10
    Union All Select   'd',    9.5,   15
    Union All Select   'a',    9.8,   15
    Union All Select   'b',    9.8,   20
    Union All Select   'c',    9.8,   30
    Union All Select   'd',    9.8,   25
    select a,b,c=(select sum(c) from @a where a<>b.a and b=b.b) from @a b
      

  5.   

    phommy(顽石宫主) 和 itblog(^ω^)  都很牛呀
      

  6.   

    drop table yyy
    create table yyy(co1 char(1),co2 numeric(10,1) ,co3 int)
    insert into  yyy select 'a',    9 ,    10             
     union select  'b',     9  ,   15             
     union select  'c' ,   9  ,  20             
     union select  'd'  ,  9 ,    10              
     union select  'a'  ,  9.5,   30              
     union select  'b'  ,  9.5,   20             
     union select  'c'  ,  9.5 ,  10              
     union select  'd'   , 9.5,   15             
     union select  'a'  ,  9.8 ,  15              
     union select  'b'   , 9.8 ,  20             
     union select  'c'   , 9.8,   30              
     union select  'd'   , 9.8 ,  25      select co1,co2,co3=(select sum(co3) from yyy where co2=a.co2  and co1<>a.co1) from yyy a order by co2
      

  7.   

    规律就是:
    abcd和系数作为一组,然后检索出除自己本身以外的数值的和。
    a 9 (b+c+d的第三列)
      

  8.   

    drop table tt
    create table tt
    (
      id1 varchar(1) ,
      id2 numeric(10,2),
      id3 int
    )insert into tt
    select 'a', 9,   10 union    
    select 'b', 9,   15 union
    select 'c', 9,   20 union    
    select 'd', 9,   10 union    
    select 'a', 9.5, 30 union
    select 'b', 9.5, 20 union  
    select 'c', 9.5, 10 union 
    select 'd', 9.5, 15 union 
    select 'a', 9.8, 15 union 
    select 'b', 9.8, 20 union 
    select 'c', 9.8, 30 union
    select 'd', 9.8, 25  
    select b.id1 , b.id2 ,
    (select sum(isnull(a.id3,0)) from tt a where  a.id2 = b.id2 and a.id3 <> b.id3) as id3
    from tt b
    order by 2drop table tt