• asranantha (12/2/2012)


    hi friend i have a small doubt in sql server plz tell me how to solve

    table data contins like id,name,sal

    1,abc,1200

    2,jaid,5300

    3,union,1000

    4,ravod.200

    i want output like

    id , name, sal , max(sal), min(sal)

    1 ,abc ,1200 , null , null

    2 ,jaid ,5300 , yes , null

    3,union , 1000 , null ,null

    4,ravod ,200 ,null , yes

    when ever max value find that time display yes other wise display null values and when ever min valu find that time disply yes other wise display null values .plz tell me how to write query in sql server

    This should do what you want:

    /*

    table data contins like id,name,sal

    1,abc,1200

    2,jaid,5300

    3,union,1000

    4,ravod.200

    i want output like

    id , name, sal , max(sal), min(sal)

    1 ,abc ,1200 , null , null

    2 ,jaid ,5300 , yes , null

    3,union , 1000 , null ,null

    4,ravod ,200 ,null , yes

    */

    create table #TestTable(

    id int,

    name varchar(10),

    salary int

    );

    go

    insert into #TestTable

    values

    (1,'abc',1200),

    (2,'jaid',5300),

    (3,'union',1000),

    (4,'ravod',200);

    go

    with MaxSalary as (

    select

    max(salary) MaxSalary

    from

    #TestTable

    ), MinSalary as (

    select

    min(salary) MinSalary

    from

    #TestTable

    )

    select

    id,

    name,

    salary,

    case when MaxSalary is not null then 'Yes' end MaxSalary,

    case when MinSalary is not null then 'Yes' end MinSalary

    from

    #TestTable tt

    left outer join MaxSalary ms1

    on (tt.salary = ms1.MaxSalary)

    left outer join MinSalary ms2

    on (tt.salary = ms2.MinSalary)

    go

    drop table #TestTable;

    go