Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Invalid column name problem

Author  Topic 

scottstown
Starting Member

10 Posts

Posted - 2011-07-11 : 10:57:36
Hello all,

I am pretty new to sql select scripts. I am currently having a problem of "Invalid column name."

SELECT StudyID,
DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT)
AS ReadingTimeInHours,
MAX(ReadingTimeInHours) AS MaxReadingTime,
MIN(ReadingTimeInHours) AS MinReadingTime,
AVG(ReadingTimeInHours) AS AvgReadingTime
FROM LocaldbView
GROUP BY LocaldbView.StudyID, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT

I read in another post that this is "isn't available as an identifier until the resultset is produced i.e. can't be used to build the resultset."

What does this mean, and how should the select statment be writtten?

Thank you,
Scott P

X002548
Not Just a Number

15586 Posts

Posted - 2011-07-11 : 11:01:25
Post the DDL for LocaldbView

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

scottstown
Starting Member

10 Posts

Posted - 2011-07-11 : 11:05:50
Ahh.. MSSQL? I am using it inside Eclipse BIRT
Go to Top of Page

scottstown
Starting Member

10 Posts

Posted - 2011-07-11 : 11:29:19
Would it be something like this?


SELECT StudyID,
DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT)
AS ReadingTimeInHours

FROM LocaldbView, (
SELECT
MAX(ReadingTimeInHours) AS MaxReadingTime,
MIN(ReadingTimeInHours) AS MinReadingTime,
AVG(ReadingTimeInHours) AS AvgReadingTime
FROM LocaldbView )



I am getting Msg 102, Level 15, State 1, Line 10
Incorrect syntax near ')'.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-07-11 : 11:55:51
Maybe one of these will help?
SELECT 
StudyID,
DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT) AS ReadingTimeInHours,
MAX(DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT)) AS MaxReadingTime,
MIN(DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT)) AS MinReadingTime,
AVG(DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT)) AS AvgReadingTime
FROM
LocaldbView
GROUP BY
LocaldbView.StudyID,
LocaldbView.ReceiveDateTime,
LocaldbView.ReportFinalizeDT

-- OR
SELECT
StudyID,
ReadingTimeInHours,
MAX(ReadingTimeInHours) AS MaxReadingTime,
MIN(ReadingTimeInHours) AS MinReadingTime,
AVG(ReadingTimeInHours) AS AvgReadingTime
FROM
(
SELECT
StudyID,
DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT) AS ReadingTimeInHours,
FROM
LocaldbView
GROUP BY
LocaldbView.StudyID,
LocaldbView.ReceiveDateTime,
LocaldbView.ReportFinalizeDT
) AS T
Go to Top of Page

scottstown
Starting Member

10 Posts

Posted - 2011-07-11 : 12:10:52
Thank you Lamprey. I appreciate your help. However neither of the above work.

The first one executes fine but the max, min, and avg are equal to the "ReadingTimeInHours"

The second example seems like more what I would need but gives
"Msg 156, Level 15, State 1, Line 12
Incorrect syntax near the keyword 'FROM'."

Any suggestions?

Thanks once again.
Scott
Go to Top of Page

yogi86
Starting Member

13 Posts

Posted - 2011-07-11 : 12:39:15
Delete the comma after
'DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT) AS ReadingTimeInHours,'

Go to Top of Page

scottstown
Starting Member

10 Posts

Posted - 2011-07-11 : 13:53:46
Thanks everyone for the help. But it is still not working correctly. I am still getting the same value for all the fields (max, min, avg)

This is the code I had to use to get it working at all.

SELECT
StudyID,
ReadingTimeInHours,
MAX(ReadingTimeInHours) AS MaxReadingTime,
MIN(ReadingTimeInHours) AS MinReadingTime,
AVG(ReadingTimeInHours) AS AvgReadingTime
FROM
(
SELECT
StudyID,
DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT) AS ReadingTimeInHours
FROM
LocaldbView
GROUP BY
LocaldbView.StudyID,
LocaldbView.ReceiveDateTime,
LocaldbView.ReportFinalizeDT
) AS T
GROUP BY
T.StudyID, T.ReadingTimeInHours
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-07-11 : 17:44:47
quote:
Originally posted by scottstown

Thanks everyone for the help. But it is still not working correctly. I am still getting the same value for all the fields (max, min, avg)

This is the code I had to use to get it working at all.

Yep, I left forgot to add the GROUP BY. Good catach.
Go to Top of Page
   

- Advertisement -