如何返回第一名和领带?

时间:2014-03-18 09:11:43

标签: sql-server-2008 tsql

目标:哪个'爬行动物'物种考试最多?显示animal_id和an_name。第1次可能的关系 - 在这种情况下应该归还所有被绑定的动物。

爬行动物:海龟,鳄鱼,蜥蜴,蛇

如何过滤掉考试最多并且包含联系的物种?

我的代码到目前为止......

SELECT AN.an_id, AN.an_name, COUNT(EH.ex_date)
FROM vt_animals AS AN
INNER JOIN vt_exam_headers AS EH ON EH.an_id = AN.an_id
WHERE AN.an_type IN ('snake', 'chelonian', 'crocodilian', 'lizard')
GROUP BY AN.an_id, AN.an_name

enter image description here

1 个答案:

答案 0 :(得分:0)

SELECT TOP 1 WITH TIES AN.an_id, AN.an_name, COUNT(EH.ex_date) as cnt
FROM vt_animals AS AN
INNER JOIN vt_exam_headers AS EH ON EH.an_id = AN.an_id
WHERE AN.an_type IN ('snake', 'chelonian', 'crocodilian', 'lizard')
GROUP BY AN.an_id, AN.an_name
ORDER BY COUNT(EH.ex_date) DESC