将 TOP 5 与 COUNT 和 LEFT JOIN 一起使用

发布于 2024-09-14 12:17:23 字数 588 浏览 5 评论 0原文

我一直在使用 HansUp 给我的以下代码(干杯!),它运行得很好:

SELECT g.ID, Count(t.Grade) AS Total
FROM grade AS g 
LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#) AS t ON g.ID=t.Grade 
GROUP BY g.ID 
ORDER BY 2 DESC; 

我现在正在寻找返回的前 5 个结果。我认为这很简单:

SELECT **TOP 5** g.ID, Count(t.Grade) AS Total
FROM grade AS g 
LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#) AS t ON g.ID=t.Grade 
GROUP BY g.ID 
ORDER BY 2 DESC; 

不幸的是,这不起作用。

有没有人有什么想法。

谢谢

I've been using the following code, given to me by HansUp (cheers!), and it's been working great:

SELECT g.ID, Count(t.Grade) AS Total
FROM grade AS g 
LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#) AS t ON g.ID=t.Grade 
GROUP BY g.ID 
ORDER BY 2 DESC; 

I'm now looking to find the TOP 5 results returned. I thought it would be as simple as:

SELECT **TOP 5** g.ID, Count(t.Grade) AS Total
FROM grade AS g 
LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#) AS t ON g.ID=t.Grade 
GROUP BY g.ID 
ORDER BY 2 DESC; 

Unfortunately that's not working.

Does anyone have any ideas.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

童话 2024-09-21 12:17:23

TOP 子句将根据您的第一个排序字段为您提供顶部。由于您的第一个排序字段对于所有记录都是常量 (2),因此您将获得所有记录。将 ID 字段添加到 ORDER BY 子句中,您将只获得 5 条记录。

SELECT TOP 5 g.ID, Count(t.Grade) AS Total
FROM grade AS g LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#)  AS t ON g.ID = t.Grade
GROUP BY g.ID
ORDER BY g.ID, 2 DESC;

如果您实际上按 Total 降序排列在前 5 个记录之后,请将 SQL 更改为以下内容:

SELECT TOP 5 g.ID, Count(t.Grade) AS Total
FROM grade AS g LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#)  AS t ON g.ID = t.Grade
GROUP BY g.ID
ORDER BY Count(t.Grade) DESC , 2 DESC;

This is top by value, so if multiple records have a Total that is the same and it恰好位于前 5 个值中总而言之,你会把它们全部拿回来。如果您确实只想要返回 5 条记录,则必须对唯一的字段进行排序。

The TOP clause will get you the top based on your first sort field. Since your first sort field is a constant (2) for all records, you get all records. Add the ID field to your ORDER BY clause and you'll only get five records.

SELECT TOP 5 g.ID, Count(t.Grade) AS Total
FROM grade AS g LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#)  AS t ON g.ID = t.Grade
GROUP BY g.ID
ORDER BY g.ID, 2 DESC;

If you're actually after the top 5 by Total in descending order, change the SQL to the following:

SELECT TOP 5 g.ID, Count(t.Grade) AS Total
FROM grade AS g LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#)  AS t ON g.ID = t.Grade
GROUP BY g.ID
ORDER BY Count(t.Grade) DESC , 2 DESC;

This is top by value, so if multiple records have a total that is the same and it happens to be in the top 5 value of Total, you'll get them all back. If you truly only ever want five records back, you have to sort on a field that is unique.

圈圈圆圆圈圈 2024-09-21 12:17:23

这应该有效。

SELECT TOP 5 g.ID, Count(t.Grade) AS Total
FROM grade AS g 
LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#) AS t ON g.ID=t.Grade 
GROUP BY g.ID 
ORDER BY 2 DESC

This should work.

SELECT TOP 5 g.ID, Count(t.Grade) AS Total
FROM grade AS g 
LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#) AS t ON g.ID=t.Grade 
GROUP BY g.ID 
ORDER BY 2 DESC
送舟行 2024-09-21 12:17:23

据我所知,这应该可行:

  SELECT TOP 5 g.ID, Count(t.Grade) AS Total
  FROM grade AS g 
  LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#) AS t ON g.ID=t.Grade 
  GROUP BY g.ID 
  ORDER BY Count(t.Grade) DESC;

这里的关键点是,当您想在 WHERE 或 ORDER BY 子句中使用 SELECT 语句中的完整表达式时,请使用它。

如果您只是使用 Access 查询网格来编写 SQL,您将立即获得正确的结果(尽管您必须深入 SQL 视图来编写子查询)。

So far as I can see, this should work:

  SELECT TOP 5 g.ID, Count(t.Grade) AS Total
  FROM grade AS g 
  LEFT JOIN (SELECT Grade FROM telephony WHERE [Date] BETWEEN #08/16/2010# AND #08/20/2010#) AS t ON g.ID=t.Grade 
  GROUP BY g.ID 
  ORDER BY Count(t.Grade) DESC;

The key point here is that you use the full expression from the SELECT statement when you want to use it in a WHERE or ORDER BY clause.

If you'd just use the Access query grid to write your SQL, you would have gotten the correct results right off the bat (though you'd have to dip into SQL view to write your subquery).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文