Hey, I am trying to learning SQL on my own and I am stuck in an error.

  • I am trying to find a average from a column but all I am getting is an Unrecognised name error msg. This is the query.

    Select End_time - Start_time as time_duration, Avg(time_duration) as avg_time From table

    I am getting an error " Unrecognized name : time_duration"

    Where should i establish the column "time_duration" to perform an operation with it.

  • Select Avg(datediff(ss, Start_time , End_time )) as avg_time_duration_in_Seconds From table


    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • That doesn't look like SQL Server error message or syntax. What database system are you using?

    (Always best to identify the system when you're posting a question that might require that knowledge to provide an answer)

  • An important principle in SQL is "All at once". One consequence of this is that all expressions in the SELECT list are defined at once, and not one by one. Therefore time_duration is undefined at this point.

    I believe that there are engines that violates this rule. SQL Server complies to the SQL standard in this regard.

    But there is a second problem with your query. You have Start_Time, End_time and then an aggregate function. This means that you need to have Start_time and End_time in your GROUP BY clause. But since the aggregate function operates on exactly these two columns, the AVG becomes redundant, since there is only one value of input for each group.

    What the right query would be I don't know, since I don't know what your original problem is.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • This was removed by the editor as SPAM

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply