|
-
February 18th, 2008, 04:19 AM
#1
Query for calculating greatest.....
Hai all,
i hav an table vth four columns
Code:
name sub1 sub2 sub3
raj 40 50 87
sam 34 67 14
my query is tat i hav to calculate the large value for raj and sam...
can any one suggest vth query...?
thanks,
-
February 18th, 2008, 05:06 AM
#2
Re: Query for calculating greatest.....
Hi all.
What do you mean with "the large value for raj and sam"?
The records of column name that have more characters? Or what?
It's quite hard to suppose what you're looking for...
-
February 18th, 2008, 05:12 AM
#3
Re: Query for calculating greatest.....
i hav to show ouput as...
name subname
raj 87
sam 67
-
February 18th, 2008, 05:26 AM
#4
Re: Query for calculating greatest.....
OK, now it's clear 
You want to extract the records in alphabetical order, so
Code:
SELECT name, subname
FROM YOUR_TABLE
ORDER BY name
is all you need.
-
June 5th, 2008, 11:25 PM
#5
Re: Query for calculating greatest.....
I think what he meant was that he wanted to show each persons highest marks...That is to show whoch subject each person is strongest in...Not meerly show it in alphabetical order..Right??
-
June 5th, 2008, 11:32 PM
#6
Re: Query for calculating greatest.....
The "problem" is that the three grades are all IN THE SAME ROW.
This is a database DESIGN problem.
So the first questions are:
1) Can you change the Database? (If not Why not?)
2) WHY was such a bad design chosen in the first place???
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
June 5th, 2008, 11:52 PM
#7
Re: Query for calculating greatest.....
Why not create 3 queries..
The first displays only sub1 and that too only if its value is higher than sub 2 and 3 else is blank...
The second displays only sub2 and that too only if its value is higher than sub1 and 3 else is blank...
The third displays only sub3 and that too only if its value is higher than sub 1 and 2 else is blank...
Finally
Create a third query which combines the data from all three queries and thus displays only the highest value,....
It's pain staking and may create other problems but atleast u don't have to change the table structure...
-
June 6th, 2008, 12:03 AM
#8
Re: Query for calculating greatest.....
I agree that such design is somewhat strange to begin with, and it does sound "school-workish", anyways....
If you the number of columns are fixed, you could for example use CASE to solve the problem, but it isn't a pretty solution:
Code:
SELECT CASE
WHEN sub1 < sub2 THEN
CASE
WHEN sub2 < sub3 THEN sub3 --sub3 is highest
ELSE sub2 --sub2 is highest
END
ELSE
CASE
WHEN sub1 < sub3 THEN sub3 --sub3 is highest
ELSE sub1 --sub1 is higest
END
END
FROM <YOUR FROM STATEMENT HERE>
If you use SQL Server 2005+ then you could likely unpivot the table as well, and select it out based on that for a much more elegant solution:
Code:
WITH CTE AS (
SELECT [name], ColumName, UnPivotScore
FROM
(
SELECT [name], sub1, sub2, sub3
FROM <YOUR TABLE HERE>) P
UNPIVOT
(
UnPivotScore FOR ColumName IN (sub1, sub2, sub3)
) AS UP
)
SELECT [name], MAX(UnPivotScore)
FROM CTE
GROUP BY [name]
Both should return
raj 87
sam 67
edit: I'm sure there are many other solutions as well, which is why I personally like SQL. Many paths to the same result
Last edited by Alsvha; June 6th, 2008 at 12:06 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|