我想从两列中检索最大值-SQL表

时间:2019-04-26 16:32:56

标签: sql sql-server

请找到表结构的附件图片

enter image description here

3 个答案:

答案 0 :(得分:0)

您可以尝试使用UNION ALL构建唯一的列结果,然后从中选择最大值

select max(col) 
from (
select col1 col
from  trans_punch
union all
select col2
from  trans_punch) t

答案 1 :(得分:0)

您可以使用公共表格表达式来合并列,然后选择最大值。

;with cteUnionPunch(Emp_id, both_punch) AS
(
SELECT Emp_id, In_Punch FROM trans_punch 
UNION ALL
SELECT Emp_id, Out_Punch FROM trans_punch
)
SELECT Emp_id, max(both_punch) FROM cteUnionPunch GROUP BY Emp_id

答案 2 :(得分:0)

您可以使用apply

select tp.Emp_id, max(tpp.Punchs)
from trans_punch as tp cross apply
     ( values (In_Punch), (Out_Punch) ) tpp(Punchs)
group by tp.Emp_id;
相关问题