April 9, 2010 at 12:01 pm
Ok, here is what I'm running into
I have 2 tables that I have to use.
table 1 has 2 columns that I need
refDate, closedDate (taskName needs to be joined on table 2)
I need to calculate the difference between these 2 dates for each record, So I did this.
select datediff("d", refDate, closedDate) as DayDifference, Task from OpenRecords where [quarter] = 3
which works,
then once I get those totals I need to calculate them against table 2 data which has a field named
daysToComplete (taskName needs to match the task in table 1)
so it would be something like
DayDifference - daysToComplete = total
and then I need to count all of the records that have a total less then 0 (-1, -2, etc)
I tried a subquery, but it gave me an error message:
select
x.RefDate.
x.CloseDate
from (Select
RefDate = ISNULL((Select refDate from OpenRecords where quarter = 3 ),0),
CloseDate = ISNULL((Select closedDate from OpenRecords where quarter = 3),0)
from OpenRecords ) as x
and it gives me this error:
Msg 258, Level 15, State 1, Line 3
Cannot call methods on datetime.
how can I do what I'm trying to accomplish? Why can't I do the dates like I'm doing?
April 9, 2010 at 12:44 pm
Please provide table definition, sample data and expected result in a ready to use format as described in the first link in my signature.
That would make it easier for us to test our solution.
April 9, 2010 at 10:21 pm
SQL_NuB (4/9/2010)
Ok, here is what I'm running intoI have 2 tables that I have to use.
table 1 has 2 columns that I need
refDate, closedDate (taskName needs to be joined on table 2)
I need to calculate the difference between these 2 dates for each record, So I did this.
select datediff("d", refDate, closedDate) as DayDifference, Task from OpenRecords where [quarter] = 3
which works,
then once I get those totals I need to calculate them against table 2 data which has a field named
daysToComplete (taskName needs to match the task in table 1)
so it would be something like
DayDifference - daysToComplete = total
and then I need to count all of the records that have a total less then 0 (-1, -2, etc)
I tried a subquery, but it gave me an error message:
select
x.RefDate.
x.CloseDate
from (Select
RefDate = ISNULL((Select refDate from OpenRecords where quarter = 3 ),0),
CloseDate = ISNULL((Select closedDate from OpenRecords where quarter = 3),0)
from OpenRecords ) as x
and it gives me this error:
Msg 258, Level 15, State 1, Line 3
Cannot call methods on datetime.
how can I do what I'm trying to accomplish? Why can't I do the dates like I'm doing?
Subqueries in a SELECT list must only return 1 value. You're code doesn't do that. Also, you don't need any subqueries according to the code your posted. Try this, instead...
SELECT ISNULL(refDate,0) AS RefDate,
ISNULL(closedDate,0) AS CloseDate
FROM dbo.OpenRecords
WHERE quarter = 3
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply