sql-如何在一个查询中组合选择列,计数和比较?

时间:2016-10-24 18:42:20

标签: sql

通过使用SQL命令,列出idworker,来自worker table的nameworker和total_late,用于“迟到”记录超过1次的工人。

1 个答案:

答案 0 :(得分:0)

试试这个(假设有一个Late表,当工人迟到时每次出现一条记录):

SELECT w.idworker, w.nameworker, l.total_late
FROM Worker w LEFT JOIN 
(SELECT idworker, SUM(late) as total_late, COUNT(*) AS number_late 
 FROM Late GROUP BY idworker) l ON w.idworker = l.idworker
WHERE number_late > 1