January 25, 2013 at 2:28 am
Hi, i'm having problems with writing a pretty basic query:
I have a table called #mydates, i want to select following:
IF MAX(mydate)<GETDATE() THEN MAX(mydate) ELSE ( MIN(mydate) WHERE mydate>=GETDATE ) END IF
To put it in words, if greatest mydate is less then current date, select most recent mydate. Otherwise if mydate is greater or equal to the current date, select the most recent mydate that is closest to the current date.
Basic table structure:
CREATE TABLE [#mydates](
[id] [int] NOT NULL,
[mydate] [datetime] NULL
) ON [PRIMARY]
GO
INSERT INTO [#mydates] (id, [mydate])
SELECT 1,'2012-01-03' UNION ALL
SELECT 2,'2012-12-05' UNION ALL
SELECT 3,'2013-02-15'
SELECT * FROM #mydates
Thanks for your time
January 25, 2013 at 2:38 am
select
case when MAX(mydate) < GETDATE() THEN MAX(mydate) ELSE MIN(Mydate) end as mydate
from #mydates where mydate >= GETDATE()
January 25, 2013 at 2:45 am
Yes, but you've missed that IF MAX(mydate)<GETDATE() THEN MAX(mydate)
Your WHERE clause truncates all dates that are less then current date "where mydate >= GETDATE()"
This can be seen with this values
INSERT INTO [#mydates] (id, [mydate])
SELECT 1,'2012-01-03' UNION ALL
SELECT 2,'2012-12-05' UNION ALL
SELECT 3,'2012-02-15'
The desired result would be
'2012-12-05'
Btw, thanks for a fast reply.
January 25, 2013 at 2:54 am
That's not your original data set, the last date was 2013-02-15.
CREATE TABLE [#mydates](
[id] [int] NOT NULL,
[mydate] [datetime] NULL
) ON [PRIMARY]
GO
INSERT INTO [#mydates] (id, [mydate])
SELECT 1,'2012-01-03' UNION ALL
SELECT 2,'2012-12-05' UNION ALL
SELECT 3,'2013-02-15'
SELECT * FROM #mydates
January 25, 2013 at 3:00 am
That is true, i had to change the data to demonstrate the original IF specifications that i specified above.
Hmm, to clarify it lets look at two sets:
With the table below:
INSERT INTO [#mydates] (id, [mydate])
SELECT 1,'2012-01-03' UNION ALL
SELECT 2,'2012-12-05' UNION ALL
SELECT 3,'2013-02-15' UNION ALL
SELECT 4,'2013-03-15'
Desired result would be '2013-02-15'
With the table below:
INSERT INTO [#mydates] (id, [mydate])
SELECT 1,'2012-01-03' UNION ALL
SELECT 2,'2012-12-05' UNION ALL
SELECT 3,'2012-02-15'
Desired result would be '2012-12-05'
I think my IF specs are correct...
January 25, 2013 at 3:20 am
Does it make any sense at all?
January 25, 2013 at 3:22 am
yes it makes sense, I am busy with work now, when I get a spare moment I will re-look at this unless someone else beats me to it
January 25, 2013 at 3:23 am
naturally, thanks 🙂
January 25, 2013 at 4:02 am
January 25, 2013 at 4:06 am
Just to add, I simplified your requirements.
To put it in words, if greatest mydate is less then current date, select most recent mydate. Otherwise if mydate is greater or equal to the current date, select the most recent mydate that is closest to the current date.
1. If greatest mydate is less then current date then the most recent mydate will be the closest in time to current date.
2. If mydate is greater or equal to the current date then return the mydate closest in time to current date.
Therefore: Return the mydate closest in time to current date.
Did I understand the requirements correctly?
Edit to add:
What is your desired output from this data set?
INSERT INTO [#mydates] (id, [mydate])
SELECT 1,'2012-01-03' UNION ALL
SELECT 2,'2013-01-24' UNION ALL
SELECT 3,'2013-02-15'
January 25, 2013 at 4:16 am
If your desired output above is 2013-01-24 then my first solution is correct.
If your desired output above is 2013-02-15 then the following will work.
SELECT
TOP 1 *
FROM
#mydates
ORDER BY
CASE
WHEN mydate > GETDATE() THEN 0
ELSE 1
END,
ABS(DATEDIFF(SECOND, GETDATE(), mydate));
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply