| Author |
Topic |
|
anuradhay
Starting Member
41 Posts |
Posted - 2005-04-01 : 00:14:36
|
| I have a table like the one given belowStudentID Mark1 Mark2 Mark3 Mark4001 52 65 45 46002 30 20 25 30003 75 69 80 95Now i want to retrieve only those students who has got more than 50 in two subjects.. |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2005-04-01 : 00:35:02
|
Since Mark1, Mark2 etc are columns and not in another table, I can't see any easy way to do it other thanselect * from Marks where (Mark1 > 50 and Mark2 > 50)OR (Mark1 > 50 and MArk3 > 50)OR (Mark1 > 50 and Mark4 > 50)OR (Mark2 >50 and Mark3 >50)OR (Mark2 > 50 and Mark4 > 50)OR (Mark3 > 50 and Mark4 > 50) --I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-04-01 : 00:47:41
|
| [code]select StudentID, Mark1, Mark2, Mark3, Mark4from Marks where (case when Mark1 > 50 then 1 else 0 end + case when Mark2 > 50 then 1 else 0 end + case when Mark3 > 50 then 1 else 0 end + case when Mark4 > 50 then 1 else 0 end ) = 2[/code]CODO ERGO SUM |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2005-04-01 : 00:48:54
|
| yep, as I said, I can't see any easy way to do it...and just to be picky - Mike, you'd need to make it >=2 rather than just =2--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
SqlStar
Posting Yak Master
121 Posts |
Posted - 2005-04-01 : 02:23:17
|
| Jones,Your solution is nice.Rafiq~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"You have to dream before your dreams can come true" -- President Of India |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2005-04-01 : 02:26:42
|
quote: Jones,Your solution is nice.
Mr Jones' solutions are always nice....--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-04-01 : 08:31:05
|
Well, I gave it to him the way he asked, "only those students who has got more than 50 in two subjects". Now if he had said "two or more"...quote: Originally posted by rrb yep, as I said, I can't see any easy way to do it...and just to be picky - Mike, you'd need to make it >=2 rather than just =2--I hope that when I die someone will say of me "That guy sure owed me a lot of money"
CODO ERGO SUM |
 |
|
|
ts_abbott@hotmail.com
Starting Member
36 Posts |
Posted - 2005-04-01 : 10:21:16
|
here's an alternative solution which may be more extensible...SELECT a.StudentId, COUNT(*) AS NumMarksAboveFifty FROM (SELECT StudentId, Mark1 AS Mark FROM Marks UNION ALL SELECT StudentId, Mark2 AS Mark FROM Test UNION ALL SELECT StudentId, Mark3 AS Mark FROM Test UNION ALL SELECT StudentId, Mark4 AS Mark FROM Test) aWHERE a.Mark > 50GROUP BY StudentIdHAVING COUNT(*) > 1 |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-04-01 : 11:35:58
|
I doubt that scanning the table four times, prerforming a union all, and then grouping the output would be more extensible that a single scan of the table.If you do want to do it that way though, at least move the filter into the union all selects to reduce the size of the result set to group.SELECT a.StudentId, COUNT(*) AS NumMarksAboveFiftyFROM ( SELECT StudentId FROM Marks where Mark1 > 50 UNION ALL SELECT StudentId FROM Marks where Mark2 > 50 UNION ALL SELECT StudentId FROM Marks where Mark3 > 50 UNION ALL SELECT StudentId FROM Marks where Mark4 > 50 ) aGROUP BY StudentIdHAVING COUNT(*) > 1 quote: Originally posted by ts_abbott@hotmail.com here's an alternative solution which may be more extensible...SELECT a.StudentId, COUNT(*) AS NumMarksAboveFifty FROM (SELECT StudentId, Mark1 AS Mark FROM Marks UNION ALL SELECT StudentId, Mark2 AS Mark FROM Test UNION ALL SELECT StudentId, Mark3 AS Mark FROM Test UNION ALL SELECT StudentId, Mark4 AS Mark FROM Test) aWHERE a.Mark > 50GROUP BY StudentIdHAVING COUNT(*) > 1
CODO ERGO SUM |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-04-01 : 11:42:19
|
[code]SELECT StudentID ,Mark1 ,Mark2 ,Mark3 ,Mark4FROM MarksWHERE 1 > SIGN(Mark1/51) + SIGN(Mark2/51) + SIGN(Mark3/51) + SIGN(Mark4/51)[/code] rockmoose |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-04-01 : 11:52:52
|
Shouldn't that be?WHERE 1 < SIGN(Mark1/51) + SIGN(Mark2/51) + SIGN(Mark3/51) + SIGN(Mark4/51) quote: Originally posted by rockmoose
SELECT StudentID ,Mark1 ,Mark2 ,Mark3 ,Mark4FROM MarksWHERE 1 > SIGN(Mark1/51) + SIGN(Mark2/51) + SIGN(Mark3/51) + SIGN(Mark4/51) rockmoose
CODO ERGO SUM |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-04-01 : 11:59:51
|
| Big difference in logic.Thanks Michael.It's funny how the smallest of things can cause so much aggravation!grrrrrrrrrrrrrrrrrr...rockmoose |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-04-01 : 12:06:46
|
I guess you are also assuming they can't have something like -73 for a test score where the SIGN function would return -1.Of couse, that could never happen, because "It's handled by the front-end".quote: Originally posted by rockmoose Big difference in logic.Thanks Michael.It's funny how the smallest of things can cause so much aggravation!grrrrrrrrrrrrrrrrrr...rockmoose
CODO ERGO SUM |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-04-01 : 12:17:24
|
Are You bugging me Michael ? rockmoose |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-04-01 : 12:31:01
|
Hey, I'm just goofing around.I didn't even bring up what happens when they're NULL...quote: Originally posted by rockmoose Are You bugging me Michael ? rockmoose
CODO ERGO SUM |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-04-01 : 12:49:21
|
| Yeah, given the current table design, we should probably not assume too much.Maybe a ISNUMERIC(Mark1) would be in place as well!rockmoose |
 |
|
|
|