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.
| 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 AvgReadingTimeFROM LocaldbViewGROUP BY LocaldbView.StudyID, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDTI 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 |
|
|
scottstown
Starting Member
10 Posts |
Posted - 2011-07-11 : 11:05:50
|
| Ahh.. MSSQL? I am using it inside Eclipse BIRT |
 |
|
|
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 ReadingTimeInHoursFROM LocaldbView, (SELECT MAX(ReadingTimeInHours) AS MaxReadingTime,MIN(ReadingTimeInHours) AS MinReadingTime,AVG(ReadingTimeInHours) AS AvgReadingTimeFROM LocaldbView )I am getting Msg 102, Level 15, State 1, Line 10Incorrect syntax near ')'. |
 |
|
|
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 AvgReadingTimeFROM LocaldbViewGROUP BY LocaldbView.StudyID, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT-- ORSELECT StudyID, ReadingTimeInHours, MAX(ReadingTimeInHours) AS MaxReadingTime, MIN(ReadingTimeInHours) AS MinReadingTime, AVG(ReadingTimeInHours) AS AvgReadingTimeFROM ( SELECT StudyID, DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT) AS ReadingTimeInHours, FROM LocaldbView GROUP BY LocaldbView.StudyID, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT ) AS T |
 |
|
|
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 12Incorrect syntax near the keyword 'FROM'."Any suggestions?Thanks once again.Scott |
 |
|
|
yogi86
Starting Member
13 Posts |
Posted - 2011-07-11 : 12:39:15
|
| Delete the comma after 'DATEDIFF(hh, LocaldbView.ReceiveDateTime, LocaldbView.ReportFinalizeDT) AS ReadingTimeInHours,' |
 |
|
|
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 AvgReadingTimeFROM ( 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 |
 |
|
|
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. |
 |
|
|
|
|
|
|
|