Unix Technical Forum

query help(2)

This is a discussion on query help(2) within the SQL Server forums, part of the Microsoft SQL Server category; --> got slightly similar table. #Temp Table: name,name1,name2,exam,score A,A1,A21, A,A1,A21,math100,88 A,A1,A21,math101,56 A,A1,A21, A,A1,A21,math102,67 A,A1,A21, A,A1,A21,math104,45 A,A1,A21,Chm100,55 A,A1,A21, A,A1,A21,chm101,86 A,A1,A21,chm102,44 A,A1,A21, ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:30 PM
techquest@fastmail.fm
 
Posts: n/a
Default query help(2)

got slightly similar table.
#Temp Table:
name,name1,name2,exam,score
A,A1,A21,
A,A1,A21,math100,88
A,A1,A21,math101,56
A,A1,A21,
A,A1,A21,math102,67
A,A1,A21,
A,A1,A21,math104,45
A,A1,A21,Chm100,55
A,A1,A21,
A,A1,A21,chm101,86
A,A1,A21,chm102,44
A,A1,A21,
(skipping)
A,A1,A22,math100,86
A,A1,A22,
A,A1,A22,math101,99
(skipping)
A,A2,A21,
A,A2,A21,math100,68
A,A2,A21,
A,A2,A21,math101,92
(skipping)

How to select * in case of highest
score and lowest score grouped
by name,name1,name2 where exam
and score are not null
(empty fields) and by exam subject
'math', 'chm',etc?.

drop Table #Temp

result like:
A,A1,A21,math100,88
A,A1,A21,math104,45
A,A1,A21,Chm100,55
A,A1,A21,chm102,44
A,A1,A22,math101,99
etc.






--
Sent by techquest from fastmail within field fm
This is a spam protected message. Please answer with reference header.
Posted via http://www.usenet-replayer.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:30 PM
I_AM_DON_AND_YOU?
 
Posts: n/a
Default Re: query help(2)

create table pp
(
name char(10),
name1 char(10),
name2 char(10),
exam char(10),
score int
)

insert into pp values ('A', 'A1', 'A21', null, null)
insert into pp values ('A', 'A1', 'A21', 'Math 100', 88)
insert into pp values ('A', 'A1', 'A21', 'Math 101', 56)
insert into pp values ('A', 'A1', 'A21', null, null)
insert into pp values ('A', 'A1', 'A21', 'Math 102', 67)
insert into pp values ('A', 'A1', 'A21', null, null)
insert into pp values ('A', 'A1', 'A21', 'Math 104', 45)
insert into pp values ('A', 'A1', 'A21', 'Chm 100', 55)
insert into pp values ('A', 'A1', 'A21', null, null)
insert into pp values ('A', 'A1', 'A21', 'Chm 101', 86)
insert into pp values ('A', 'A1', 'A21', 'Chm 102', 44)
insert into pp values ('A', 'A1', 'A21', null, null)
insert into pp values ('A', 'A1', 'A22', 'Math 100', 86)
insert into pp values ('A', 'A1', 'A22', null, null)
insert into pp values ('A', 'A1', 'A22', 'Math 101', 99)
insert into pp values ('A', 'A1', 'A21', null, null)
insert into pp values ('A', 'A1', 'A21', 'Math 100', 68)
insert into pp values ('A', 'A1', 'A21', null, null)
insert into pp values ('A', 'A1', 'A21', 'Math 101', 92)



select name, name1, name2, exam, max(score) MaxScore, Min(score) MinScore
from pp
where exam is not null and score is not null
group by name, name1, name2, exam


"techquest@fastmail.fm" <u753143310@spawnkill.ip-mobilphone.net> wrote in
message news:l.1066880501.1423919677@x130-65-109-102.sjsu.edu...
> got slightly similar table.
> #Temp Table:
> name,name1,name2,exam,score
> A,A1,A21,
> A,A1,A21,math100,88
> A,A1,A21,math101,56
> A,A1,A21,
> A,A1,A21,math102,67
> A,A1,A21,
> A,A1,A21,math104,45
> A,A1,A21,Chm100,55
> A,A1,A21,
> A,A1,A21,chm101,86
> A,A1,A21,chm102,44
> A,A1,A21,
> (skipping)
> A,A1,A22,math100,86
> A,A1,A22,
> A,A1,A22,math101,99
> (skipping)
> A,A2,A21,
> A,A2,A21,math100,68
> A,A2,A21,
> A,A2,A21,math101,92
> (skipping)
>
> How to select * in case of highest
> score and lowest score grouped
> by name,name1,name2 where exam
> and score are not null
> (empty fields) and by exam subject
> 'math', 'chm',etc?.
>
> drop Table #Temp
>
> result like:
> A,A1,A21,math100,88
> A,A1,A21,math104,45
> A,A1,A21,Chm100,55
> A,A1,A21,chm102,44
> A,A1,A22,math101,99
> etc.
>
>
>
>
>
>
> --
> Sent by techquest from fastmail within field fm
> This is a spam protected message. Please answer with reference header.
> Posted via http://www.usenet-replayer.com



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 07:30 PM
Vishal Parkar
 
Posts: n/a
Default Re: query help(2)

Techquest,

From the expected result you 've posted i assume that anything starting from exam name as
math represents math examination. anything starting from chm represents chm examination.Im
not sure what is the number that is appended to each exam represents. Is my assumption
correct? or those number has some significance and represents different exams??

--sample table and records
create table #T
(name varchar(50),name1 varchar(50),name2 varchar(50),exam varchar(50),score int)
insert into #t
select 'A','A1','A21',null,null union all
select 'A','A1','A21','math100',88 union all
select 'A','A1','A21','math101',56 union all
select 'A','A1','A21',null,null union all
select 'A','A1','A21','math102',67 union all
select 'A','A1','A21',null,null union all
select 'A','A1','A21','math104',45 union all
select 'A','A1','A21','Chm100',55 union all
select 'A','A1','A21',null,null union all
select 'A','A1','A21','chm101',86 union all
select 'A','A1','A21','chm102',44 union all
select 'A','A1','A21',null,null union all
select 'A','A1','A22','math100',86 union all
select 'A','A1','A22',null,null union all
select 'A','A1','A22','math101',99 union all
select 'A','A2','A21',null,null union all
select 'A','A2','A21','math100',68 union all
select 'A','A2','A21',null,null union all
select 'A','A2','A21','math101',92

--Try following query.

select a.* from #t a join
(select name,name1,name2, substring (exam, 1, patindex ('%[0-9]%', exam)-1) exam,
max(score) 'score'
from #t
where (exam is not null and score is not null)
group by name,name1,name2,
substring (exam, 1, patindex ('%[0-9]%', exam)-1)
union all
select name,name1,name2, substring (exam, 1, patindex ('%[0-9]%', exam)-1) exam,
min(score) 'score'
from #t
where (exam is not null and score is not null)
group by name,name1,name2,
substring (exam, 1, patindex ('%[0-9]%', exam)-1)) b
on a.name = b.name and a.name1=b.name1
and a.name2=b.name2 and b.exam = substring (a.exam, 1, patindex ('%[0-9]%', a.exam)-1)
and a.score = b.score
order by 1 ,2 ,3,substring (a.exam, 1, patindex ('%[0-9]%', a.exam)-1),5 desc

/*
If there is no significance of the numbers that are appended to each exam just remove them
it unncessary makes query logic complicated. If it is required and is a part of atrributes
of
the table then seperate it into another column to normalize the table design.
*/

--Use following query if you want to group on exams as it is (inclusive of numbers).

select name,name1,name2, exam,
max(score) 'score'
from #t
where (exam is not null and score is not null)
group by name,name1,name2, exam
union all
select name,name1,name2, exam,
min(score) 'score'
from #t
where (exam is not null and score is not null)
group by name,name1,name2, exam
order by 1,2,3,4,5 desc

--
-Vishal


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 07:30 PM
I_AM_DON_AND_YOU?
 
Posts: n/a
Default Re: query help(2)



"Vishal Parkar" <_vgparkar@yahoo.co.in> wrote in message
news:OdWPf3SmDHA.988@TK2MSFTNGP10.phx.gbl...
> Techquest,
>
> From the expected result you 've posted i assume that anything starting

from exam name as
> math represents math examination. anything starting from chm represents

chm examination.Im
> not sure what is the number that is appended to each exam represents. Is

my assumption
> correct? or those number has some significance and represents different

exams??
>



Vishal:

The number after the exam is part of the exam name (course name), e.g. "Math
100", "Math 102", "Math 103" are three different exams (courses). This is
equivalent what is called (in some classes in India ) Math Part I, Math
Part II etc.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 07:31 PM
Vishal Parkar
 
Posts: n/a
Default Re: query help(2)

hmm, i never know that.

--
-Vishal


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:11 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com