-2

on the stackoverflow site in fount Thad select CONVERT(CHAR(10), DATEADD(second, MAX(60), 0), 108) will result in 00:01:00 (that's true btw :-) )

now I want to integrate this in the following query (for Ssms12):

SELECT Run.TaskName,history.runtime
,CONVERT(CHAR(10), DATEADD(second, MAX(history.runtime), 0), 108)

FROM dbo.history
INNER JOIN Run
ON dbo.history.TaskID=Run.TaskID

whatever I try, I can't get this to work in the above example, does anyone have an idea how to fix this?

error: Column 'Run.TaskName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Tedo G.
  • 1,556
  • 3
  • 17
  • 29
Thomas
  • 39
  • 1
  • 7

3 Answers3

1

You have to group by other columns where you are not using aggregate function:

SELECT Run.TaskName,history.runtime,
   CONVERT(CHAR(10), DATEADD(second, history.runtime, 0), 108)
FROM dbo.history
INNER JOIN Run ON dbo.history.TaskID=Run.TaskID

You are using MAX for runtime so you can do like :

SELECT Run.TaskName,MAX(history.runtime) runtime,
    CONVERT(CHAR(10), DATEADD(second, MAX(history.runtime), 0), 108)
FROM dbo.history
INNER JOIN Run ON dbo.history.TaskID=Run.TaskID
GROUP BY Run.TaskName

It means either you have to remove MAX from runtime or you have to use group by for rest of columns.

Shushil Bohara
  • 5,556
  • 2
  • 15
  • 32
1

Most probably you needn't MAX. select CONVERT(CHAR(10), DATEADD(second, 60, 0), 108) is OK, so

SELECT Run.TaskName,history.runtime
      ,CONVERT(CHAR(10), DATEADD(second, history.runtime, 0), 108)
FROM dbo.history 
INNER JOIN dbo.Run
    ON dbo.history.TaskID=dbo.Run.TaskID
Serg
  • 22,285
  • 5
  • 21
  • 48
0

Make sure you have necessary permissions to Run table and use the full qualified table as below if the table exists and you have necessary permission to read the tables

SELECT Run.TaskName,history.runtime
      ,CONVERT(CHAR(10), DATEADD(second, MAX(history.runtime), 0), 108)
FROM dbo.history 
INNER JOIN dbo.Run
    ON dbo.history.TaskID=dbo.Run.TaskID
Tharsan Sivakumar
  • 6,351
  • 3
  • 19
  • 28