【问题标题】:How to get maximum count of a field in a table in Tsql and groupby如何在Sql中获取表中字段的最大计数并分组
【发布时间】:2013-02-09 16:19:58
【问题描述】:

我有一张表Customer_Complex_LoginLogs来记录客户入口。

我想获取一天内发生的最大进入次数(并且我想知道发生这种情况的那一天)。

我知道我应该执行group by TFEnteranceDate

如何在 TSQL 中实现这一点?

表名:

Customer_Complex_LoginLogs

表格字段:

Id guid  PK
Id_Customer guid   FK
TFEnteranceDate datetime
TFEnteranceDatep nvarchar(10)

【问题讨论】:

  • @Sepster 我想要任何一天的最大入场人数它已经发生了
  • @MartinSmith 我添加了表格详细信息
  • @Karamafrooz - 您还应该包含数据类型,因为如果datetimedate 相比,答案会有所不同。作为一般规则,包括有关表上任何键和索引的信息也很有用。
  • @MartinSmith 我添加了信息
  • 所以史蒂夫的回答可能对你有用。不过,您不应该将日期存储为 nvarchar(10)。如果 2008+ 或 datetime 如果更早,则使用 date 数据类型

标签: sql tsql group-by aggregate-functions


【解决方案1】:

如果没有更多信息,这可能是一个简单的 GROUP BY

SELECT TOP 1 TFEnteranceDate, Count(TFEnteranceDate) as Enterance
FROM Customer_Complex_LoginLogs 
GROUP BY TFEnteranceDate
ORDER BY Count(TFEnteranceDate) DESC

编辑:记录最大数量的 TFEnteranceDate 的那一天

【讨论】:

  • +1 啊,我最近一直在使用(反对?)MS Access(blugrh!)并且完全忘记了 TOP 1/ORDER BY“模式”,以避免必须重新加入才能获得与最大值关联的行。不错。
猜你喜欢
  • 2015-10-15
  • 1970-01-01
  • 2013-12-22
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
相关资源
最近更新 更多