我需要显示某人签入的总天数和小时数。
下面的代码在单独的列中创建变量,除了列标题外没有标识符。我还可以创建一列来显示总小时数。
select id, fname, lname, othertablevariable,
extract(day from diff) Days,
extract(hour from diff) Hours
from(
select id, fname, lname, othertablevariable,
(CAST(checkout as timestamp)-CAST(checkin as timestampe)) diff
from table t1, table t2
where t1.id=t2.id);
输出结果为:
id fname lname days hours
但是,我希望“天”列和“小时”列成为单个列。
理想情况下,它可能显示为4 days 3 hours
。这可能吗?
答案 0 :(得分:2)
您可以使用内部查询来实现它。检查以下答案:
select id, ..., Days || Hours ConcatenatedColumn
from
(
select id, fname, lname, othertablevariable,
extract(day from diff) Days,
extract(hour from diff) Hours
from(
select id, fname, lname, othertablevariable,
(CAST(checkout as timestamp)-CAST(checkin as timestampe)) diff
from table t1, table t2
where t1.id=t2.id)
)
答案 1 :(得分:1)
您可以使用CONCAT()
函数来实现它:
select id, fname, lname, othertablevariable,
CONCAT(extract(day from diff), ' days ', extract(hour from diff), ' hours') AS dayshours
from (
....
结果将为X days Y hours