george sibbald (2/6/2011)
did the interviewer give a solution?Beats me where the case statement would come into it but this would do it:
select top 1 yourcolumn from yourtable order by yourcolumn desc
Ouch. :blush:
I did read more into the question then what was actually asked (I thought of the max BidPrice per item...)
Re-reading the question, your solution would be much better than mine.
Both could be wrapped with a SELECT CASE WHEN 1=1 statement...
nzngh (2/6/2011)
It was an Interview question, surly I couldn't answer it. I have tried it in any possible way that I know but I got only Null though with two column of min and max it works and give me min and max but having only one column nop.
My reply would be, "Why would I use an oddball solution like that when Min and Max are built for that kind of thing? Building code that follows common, standard practices makes it easier to document and maintain. Building code with 'tricks' that nobody knows makes it more difficult on everyone, including me. So why would I do something like that?"
I see three possibilities here:
1. A prior dev/DBA used oddball tricks like that, and everyone currently working there hates it, and it's a "trick question" to see if you'd continue practices that everyone hates.
2. The person doing the interview uses oddball tricks like that, either out of miseducation, ego gratification, or "job security".
3. The company or department has policies that force them to use oddball coding practices, probably due to Pointy-Haired-Boss Syndrome.
If it's either 2 or 3, you don't want to work there anyway. If it's 1, then my reply would get a good reaction.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Well he didn't look so much like an odd fish nor uneducated how ever I don't know about their considerations for they didn't explain and I didn't ask.:hehe: You may right but honestly what I am looking for is the answer, if there would be an answer.
I would think of using order by then, using top 1 to get the max value.
select top 1 given_field from your_table order by given_field desc
using case statement only makes sense if you want to find which one is larger between two columns for a given row
GSquared (2/7/2011)
nzngh (2/6/2011)
It was an Interview question, surly I couldn't answer it. I have tried it in any possible way that I know but I got only Null though with two column of min and max it works and give me min and max but having only one column nop.My reply would be, "Why would I use an oddball solution like that when Min and Max are built for that kind of thing? Building code that follows common, standard practices makes it easier to document and maintain. Building code with 'tricks' that nobody knows makes it more difficult on everyone, including me. So why would I do something like that?"
I see three possibilities here:
1. A prior dev/DBA used oddball tricks like that, and everyone currently working there hates it, and it's a "trick question" to see if you'd continue practices that everyone hates.
2. The person doing the interview uses oddball tricks like that, either out of miseducation, ego gratification, or "job security".
3. The company or department has policies that force them to use oddball coding practices, probably due to Pointy-Haired-Boss Syndrome.
If it's either 2 or 3, you don't want to work there anyway. If it's 1, then my reply would get a good reaction.
Or how about if you want to see how much a person REALLY knows about T-SQL or want to see their thought process. I doubt these people actually use the CASE statement to find the MAX value. But forcing someone to write a solution using CASE shows whether that person has some experience in T-SQL or just some superficial knowledge of T-SQL gleaned of a book the night before the interview.
Viewing 15 posts - 1 through 15 (of 76 total)
You must be logged in to reply to this topic. Login to reply