CASE statement [message #597021] |
Mon, 30 September 2013 03:57 |
pstanand
Messages: 133 Registered: February 2005 Location: Chennai,India
|
Senior Member |
|
|
Hi,
I have a table test_date with the following structure.
test_date(id number,tdate date)
And it contains values as follows
1 5-Sep-13
2 6-Sep-13
3 4-Sep-13
4 null
5 1-Oct-13
6 5-Oct-13
I would like to get the maximum date value if the tdate is null. I used the following query and it doesn't fetches the correct result. Could you please correct me where i'm wrong.
select case when tdate is null then (select max(tdate)from test_date)end tdate from test_date.
Thanks.
Anand
|
|
|
|
|
|
Re: CASE statement [message #597025 is a reply to message #597021] |
Mon, 30 September 2013 04:40 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
If this the max in the whole table it could be:
select id,
nvl(tdate,
max(tdate) over
(order by id
rows between unbounded preceding and unbounded following)
tdate
from test_date
or
select id,
nvl(tdate, (select max(tdate) from test_date)) tdate
from test_date
or
with maxdate as (select max(tdate) maxdt from test_date)
select id, nvl(tdate, maxdt) tdate
from test_date, maxdate
...
[Updated on: Mon, 30 September 2013 13:35] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: CASE statement [message #597077 is a reply to message #597072] |
Mon, 30 September 2013 14:24 |
|
Littlefoot
Messages: 21808 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Let me guess: it is because he managed to compose such a query at the moment.
Does it work? Yes, it seems so.
Could it be improved? Yes, certainly.
Although number of posts doesn't show someone's knowledge and experience, it seems that @peterf isn't very experienced. I'm quite sure that, after he spends years and decades in writing Oracle queries, he'll be capable of solving this very problem having his eyes closed. As of now, well, query looks as it does.
So, perhaps you could tell him what you found wrong or suboptimal so that all of us could learn something from you. How does that sound?
|
|
|
|
|
Re: CASE statement [message #597088 is a reply to message #597077] |
Mon, 30 September 2013 16:12 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
Littlefoot wrote on Mon, 30 September 2013 20:24Let me guess: it is because he managed to compose such a query at the moment.
I posted Quote:
All valid questions that I was going to lead up to Michel So I'm not sure how our comment relates to what I wrote LF. However, yes, quite clearly that that is what he composed at that point in time. Do you have an issue with me asking him why he used the to_char? If so, why. I fully intended to discuss with him why it was not necessary, so I'm not sure what the problem is.
Quote:Does it work? Yes, it seems so. And if what he had written didn't work, I would have posted something different, rather than ask him about an initially unnecessary piece of code.
Quote:Could it be improved? Yes, certainly. OK, in fact, the code that you posted earlier was an improvement, however, the OP seemed to think that Peter's was the one to go for. Do you think that possibly, discussing the problems with that code might have been beneficial?
Quote:Although number of posts doesn't show someone's knowledge and experience, it seems that @peterf isn't very experienced. I'm quite sure that, after he spends years and decades in writing Oracle queries, he'll be capable of solving this very problem having his eyes closed. As of now, well, query looks as it does. I'm not really sure what point you're making here LittleFoot. The query looks as it does, yes, and it is need need of improvement as has already been said. In fact, better solutions already exist in the Thread, unfortunately, as I already said, the OP seems to want to take the 'least good' option. Again, the reason for me starting a discussion by asking, in the first instance, about the unnecessary to_char.
Quote:So, perhaps you could tell him what you found wrong or suboptimal I'm pretty sure that I did that, when I said:
Quote: Why the unnecessary to_char on the date Peter?
Quote:so that all of us could learn something from you. How does that sound? Kind of condescending, to be honest, very unlike you LF. I don't pretend to be a font of all knowledge. I simply tried to initiate a discussion on the problems with the solution provided.
[Updated on: Mon, 30 September 2013 16:15] Report message to a moderator
|
|
|
Re: CASE statement [message #597103 is a reply to message #597088] |
Tue, 01 October 2013 00:20 |
|
Littlefoot
Messages: 21808 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Huh, right, this might turn into another useless discussion. Worst of all, I started it. What an idiot.
The fact is: I was astonished with all these why this or why not that questions. It was a loooong day yesterday and my tolerance level was pretty low, so I wrote what I wrote so I'll just have to live with it.
As of the OP picking the least good solution and thanking its creator, that's nothing to be upset about (especially not stating that he doesn't deserve answers in the future). Having ~40 posts on a forum in 8 years suggests that he is not an experienced forum user. People like him sometimes don't read everything that was posted, but jump to the last message and fail to observe everything else.
Finally, as I was pretty much wrong and probably unnecessarily overreacted, I apologize if I hurt anyone's feelings.
|
|
|
|
|
Re: CASE statement [message #597109 is a reply to message #597106] |
Tue, 01 October 2013 02:41 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
Quote:
I am doing reporting now and I am converting every date datatype to char for formatting and hence maybe I went with the flow. LOL, yes, that makes a lot of sense. Something I'm sure that we're all guilty of.
Quote:And why did Anand use mine I think he was just looking for a readymade case statement and when he saw the code he checked it and then used it without breaking much head on other valuable posts. Agreed, that is a definite possibilty. Hopefully the OP will now realise that there were slightly better options available. What say you Annand?
Quote:So we should hopefully close this discussion now. thanks everyone for their suggestion.
[Updated on: Tue, 01 October 2013 02:41] Report message to a moderator
|
|
|