2
select 
convert(varchar(10),  TotalSeconds / 3600) +':'+
convert(varchar(10),   TotalSeconds % 3600 / 60) +':'+ 
 convert(varchar(10),  TotalSeconds % 60) as Seconds
from
(select
  DateDiff
(second, date,outtime )
 as TotalSeconds 
    from attendance.attn_card_register) x -- this one
Paul White
  • 83,961
  • 28
  • 402
  • 634
Isha
  • 23
  • 1
  • 1
  • 3

2 Answers2

12

All tables used in a FROM clause must have a "correlation name" in standard SQL terms a.k.a "alias" in common vernacular SQL

When you use a table directly, the name is obvious. When you use an inline derived table it must be aliased whether it is actually referenced or not

You can use a CTE too. Here "MyCTE" is the name and doesn't required an alias

;WITH MyCTE AS
(
    select
      DateDiff (second, date,outtime)  as TotalSeconds 
    from
        attendance.attn_card_register)
)
Select 
   convert(varchar(10),  TotalSeconds / 3600) +':'+
   convert(varchar(10),   TotalSeconds % 3600 / 60) +':'+ 
   convert(varchar(10),  TotalSeconds % 60) as Seconds
from
   MyCTE;

Edit: not all RDBMS require it. SQL Server, MySQL, and Teradata do, Oracle doesn't.

gbn
  • 69,809
  • 8
  • 163
  • 243
11

It is the alias used to reference the derived table. In the outer SELECT if you fully qualified the column references they would read x.TotalSeconds

select convert(varchar(10)
     , x.TotalSeconds / 3600) +':'+ convert(varchar(10)
     , x.TotalSeconds % 3600 / 60) +':'+ convert(varchar(10)
     , x.TotalSeconds % 60) as Seconds 
from (select DateDiff (second, date,outtime ) as TotalSeconds 
      from attendance.attn_card_register) x
RobPaller
  • 1,041
  • 7
  • 12