gpt4 book ai didi

sql - 在查询中选择前 20 个不同的行

转载 作者:行者123 更新时间:2023-12-02 07:02:13 24 4
gpt4 key购买 nike

我在带有日期戳的表格中保存用户操作日志。

每次用户进入一个项目时,它都会记录他们查看该项目并标记日期时间。

我想获取用户最近进入的前 20 个项目。

这是我目前所拥有的:

SELECT DISTINCT TOP (20) Users_Tran_ID
FROM App_UserLog
WHERE (Users_ID = 'ADMIN') AND (Users_Transaction LIKE 'Permit Viewed%')
ORDER BY App_UserLog.Users_LogDate DESC

问题是,我不希望输出中出现 Users_LogDate,因为我需要 UNIQUE Users_Tran_ID。

当我从 SELECT 中删除 Users_LogDate 时,出现以下错误:

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

这是我得到的基本选择和结果集。我只想要不同的 Users_Tran_ID 和前 20 个。

ADMIN   Permit Viewed   B2011-0012  2013-08-29 17:35:04.000
ADMIN Permit Viewed B2011-0012 2013-08-29 17:29:09.000
ADMIN Permit Viewed B2006-0043 2013-08-29 17:29:05.000
ADMIN Permit Viewed B2011-0012 2013-08-29 17:28:49.000
ADMIN Permit Viewed B2008-0167 2013-08-29 17:17:13.000
ADMIN Permit Viewed B2006-0043 2013-08-29 17:17:08.000
ADMIN Permit Viewed B2011-0012 2013-08-29 17:16:55.000
ADMIN Permit Viewed B2011-0012 2013-08-29 17:16:45.000
ADMIN Permit Viewed B2011-0012 2013-08-29 17:16:38.000
ADMIN Permit Viewed B2007-0478 2013-08-29 17:16:32.000
ADMIN Permit Viewed B2011-0012 2013-08-29 11:39:59.000
ADMIN Permit Viewed B2007-0478 2013-08-29 11:22:05.000
ADMIN Permit Viewed B2007-0478 2013-08-29 11:22:03.000
ADMIN Permit Viewed B2011-0012 2013-08-29 10:23:17.000
ADMIN Permit Viewed B2011-0012 2013-08-29 10:21:58.000
ADMIN Permit Viewed B2011-0012 2013-08-29 10:20:41.000
ADMIN Permit Viewed B2011-0012 2013-08-29 10:20:37.000
ADMIN Permit Viewed B2007-0478 2013-08-29 10:18:01.000
ADMIN Permit Viewed B2007-0478 2013-08-29 10:17:58.000
ADMIN Permit Viewed B2011-0012 2013-08-29 10:17:54.000
ADMIN Permit Viewed B2011-0012 2013-08-29 10:01:50.000
ADMIN Permit Viewed B2011-0012 2013-08-29 10:01:48.000
ADMIN Permit Viewed B2007-0478 2013-08-29 09:56:32.000
ADMIN Permit Viewed B2007-0478 2013-08-29 09:53:31.000
ADMIN Permit Viewed B2007-0478 2013-08-29 09:53:27.000
ADMIN Permit Viewed ENG2012-0141 2013-08-15 12:51:09.000
ADMIN Permit Viewed ENG2012-0141 2013-08-15 12:51:07.000
ADMIN Permit Viewed BLD2012-0094 2013-08-13 12:01:39.000
ADMIN Permit Viewed ENG2013-0047 2013-08-05 10:40:34.000
ADMIN Permit Viewed ENG2013-0047 2013-08-05 10:40:28.000
ADMIN Permit Viewed ENG2013-0047 2013-08-05 10:40:24.000
ADMIN Permit Viewed ENG2013-0047 2013-08-05 10:40:21.000
ADMIN Permit Viewed ENG2013-0047 2013-08-05 10:40:13.000
ADMIN Permit Viewed ENG2013-0047 2013-08-05 10:40:09.000
ADMIN Permit Viewed ENG2013-0047 2013-08-05 10:40:07.000
ADMIN Permit Viewed ENG2012-0141 2013-08-05 10:37:55.000
ADMIN Permit Viewed ENG2012-0141 2013-08-05 10:37:50.000
ADMIN Permit Viewed ENG2012-0141 2013-08-05 10:37:46.000
ADMIN Permit Viewed ENG2012-0141 2013-08-05 10:37:40.000
ADMIN Permit Viewed ENG2012-0141 2013-08-05 10:37:33.000
ADMIN Permit Viewed ENG2012-0141 2013-08-05 10:37:31.000
ADMIN Permit Viewed BLD2012-0919 2013-08-05 10:27:21.000
ADMIN Permit Viewed BLD2012-0919 2013-08-05 10:27:17.000
ADMIN Permit Viewed ENG2012-0119 2013-08-05 10:26:51.000
ADMIN Permit Viewed ENG2012-0119 2013-08-05 10:26:48.000

我该怎么做?

最佳答案

您想使用 group by 而不是 distinct:

SELECT TOP (20) Users_Tran_ID
FROM App_UserLog
WHERE (Users_ID = 'ADMIN') AND (Users_Transaction LIKE 'Permit Viewed%')
GROUP BY Users_Tran_ID
ORDER BY max(App_UserLog.Users_LogDate) DESC;

通过聚合,您可以指定要按最近的日志日期排序。瞧,那应该做你想做的。

关于sql - 在查询中选择前 20 个不同的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18522798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com