繁体   English   中英

如何连接两个提取列?

[英]How to Concatenate Two Extracted Columns?

我需要显示有人签到的总时间和小时数。

下面的代码在单独的列中创建变量,除了列标题之外没有标识符。 我还可以创建一个显示总小时数的列。

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

但是,我希望days列和小时列为单列。

理想情况下,它可以显示为4 days 3 hours 这可能吗?

您可以使用内部查询来实现它。 检查以下答案:

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)
)

您可以使用CONCAT()函数来实现它:

select id, fname, lname, othertablevariable,
       CONCAT(extract(day from diff), ' days ', extract(hour from diff), ' hours') AS dayshours
from (
....

结果将是X days Y hours

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM