SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sql query implementaion


sql query implementaion

Author
Message
asranantha
asranantha
SSC Eights!
SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)

Group: General Forum Members
Points: 974 Visits: 4717
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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90986 Visits: 38945
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




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
asranantha
asranantha
SSC Eights!
SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)

Group: General Forum Members
Points: 974 Visits: 4717
thanks its working fine. in this query i want modify when max salary then display yes and remaing sal column display no and when min salary then display yes and remaing sal columns no.
pls tell me query
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90986 Visits: 38945
asranantha (12/2/2012)
thanks its working fine. in this query i want modify when max salary then display yes and remaing sal column display no and when min salary then display yes and remaing sal columns no.
pls tell me query


Don't think so. You should be able to figure this out based on what I have already provided.
It is a minor change to what I provided.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
asranantha
asranantha
SSC Eights!
SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)

Group: General Forum Members
Points: 974 Visits: 4717
k i got it
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90986 Visits: 38945
asranantha (12/2/2012)
k i got it


Are you sure? Post the code with your changes.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
asranantha
asranantha
SSC Eights!
SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)SSC Eights! (974 reputation)

Group: General Forum Members
Points: 974 Visits: 4717
just we add else statement here
case when MaxSalary is not null then 'Yes' else 'no' end MaxSalary,
case when MinSalary is not null then 'Yes' else 'no' end MinSalary
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search