我需要在b_id
中找到重复最长时间的值。例如,下表的查询应该返回40(n.b。,查询应返回单个值)
| b_id | s_id | doi | dos | charge | +------+------+------------+------------+--------+ | 10 | 3 | 0000-00-00 | 0000-00-00 | 200 | | 10 | 2 | 0000-00-00 | 0000-00-00 | 200 | | 20 | 1 | 0000-00-00 | 0000-00-00 | 200 | | 30 | 2 | 0000-00-00 | 0000-00-00 | 200 | | 40 | 4 | 0000-00-00 | 0000-00-00 | 200 | | 40 | 5 | 0000-00-00 | 0000-00-00 | 200 | | 70 | 5 | 0000-00-00 | 0000-00-00 | 200 | | 40 | 4 | 0000-00-00 | 0000-00-00 | 200 |
答案 0 :(得分:2)
SELECT TOP 1 COUNT(*) as total FROM dbo.Table GROUP BY b_id ORDER BY total DESC
编辑: ups,mysql等价物:
SELECT COUNT(*) as total FROM dbo.Table GROUP BY b_id ORDER BY total DESC LIMIT 1
EDIT2: 评论之后:
SELECT b_id FROM dbo.Table GROUP BY b_id ORDER BY COUNT(b_id) DESC LIMIT 1