下午全部, 我是一个stackoverflow处女,所以请放轻松我。我很难从数据库中检索我需要的信息,并认为相关的子查询可能是答案,但我不确定。
我需要的所有信息都来自一张桌子:
╔══════╦══════════════╦══════════════╦════════════╦══════╦═════════════════════╦═════════════╗
║ COL1 ║ COL2 ║ FILENAME ║ COL4 ║ COL5 ║ DATETIME ║ COL7 ║
╠══════╬══════════════╬══════════════╬════════════╬══════╬═════════════════════╬═════════════╣
║ 2 ║ generaluser1 ║ shared2.txt ║ Shared ║ XP1 ║ 2013-04-04 19:23:09 ║ Checked In ║
║ 3 ║ generaluser1 ║ shared2.txt ║ Shared ║ XP1 ║ 2013-04-04 19:24:06 ║ Checked Out ║
║ 4 ║ generaluser1 ║ shared2.txt ║ Shared ║ XP1 ║ 2013-04-04 19:24:20 ║ Checked In ║
║ 5 ║ generaluser1 ║ restdoc3.txt ║ Restricted ║ XP1 ║ 2013-04-04 19:25:04 ║ Checked In ║
║ 6 ║ generaluser1 ║ restdoc3.txt ║ Restricted ║ XP1 ║ 2013-04-04 19:26:08 ║ Checked Out ║
║ 7 ║ generaluser1 ║ restdoc3.txt ║ Restricted ║ XP1 ║ 2013-04-04 19:26:21 ║ Checked In ║
║ 8 ║ generaluser1 ║ shared2.txt ║ Shared ║ XP1 ║ 2013-04-04 19:26:51 ║ Checked Out ║
║ 9 ║ generaluser1 ║ restdoc3.txt ║ Restricted ║ XP1 ║ 2013-04-04 19:28:41 ║ Checked Out ║
║ 10 ║ generaluser1 ║ restdoc3.txt ║ Restricted ║ XP1 ║ 2013-04-04 19:31:01 ║ Checked In ║
║ 11 ║ generaluser2 ║ restdoc3.txt ║ Restricted ║ XP2 ║ 2013-04-04 19:31:13 ║ Checked Out ║
║ 12 ║ generaluser1 ║ shared2.txt ║ Shared ║ XP1 ║ 2013-04-04 19:33:03 ║ Checked In ║
║ 13 ║ generaluser2 ║ restdoc3.txt ║ Restricted ║ XP2 ║ 2013-04-04 19:33:28 ║ Checked In ║
║ 14 ║ generaluser2 ║ restdoc4.txt ║ Restricted ║ XP2 ║ 2013-04-04 19:33:36 ║ Checked In ║
╚══════╩══════════════╩══════════════╩════════════╩══════╩═════════════════════╩═════════════╝
我要做的是为每个文档名称(col2)检索一行,其中datetime是最新的。
因此,我想要为上述行提取/输出的结果只是:
╔══════╦══════════════╦══════════════╦════════════╦══════╦═════════════════════╦════════════╗
║ COL1 ║ COL2 ║ FILENAME ║ COL4 ║ COL5 ║ DATETIME ║ COL7 ║
╠══════╬══════════════╬══════════════╬════════════╬══════╬═════════════════════╬════════════╣
║ 12 ║ generaluser1 ║ shared2.txt ║ Shared ║ XP1 ║ 2013-04-04 19:33:03 ║ Checked In ║
║ 13 ║ generaluser2 ║ restdoc3.txt ║ Restricted ║ XP2 ║ 2013-04-04 19:33:28 ║ Checked In ║
║ 14 ║ generaluser2 ║ restdoc4.txt ║ Restricted ║ XP2 ║ 2013-04-04 19:33:36 ║ Checked In ║
╚══════╩══════════════╩══════════════╩════════════╩══════╩═════════════════════╩════════════╝
我可以设法获取我选择的文档或所有文档的最新行,但不是每个唯一文档的最新行。
提前感谢任何帮助我的人。
答案 0 :(得分:0)
假设包含文件名的列名为FileName
,包含日期和时间的列为DateTime
。
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT FileName, MAX(DateTime) max_date
FROM TableName
GROUP BY FileName
) b ON a.FileName = b.FileName AND
a.DateTime = b.max_Date
输出
╔══════╦══════════════╦══════════════╦════════════╦══════╦═════════════════════╦════════════╗
║ COL1 ║ COL2 ║ FILENAME ║ COL4 ║ COL5 ║ DATETIME ║ COL7 ║
╠══════╬══════════════╬══════════════╬════════════╬══════╬═════════════════════╬════════════╣
║ 12 ║ generaluser1 ║ shared2.txt ║ Shared ║ XP1 ║ 2013-04-04 19:33:03 ║ Checked In ║
║ 13 ║ generaluser2 ║ restdoc3.txt ║ Restricted ║ XP2 ║ 2013-04-04 19:33:28 ║ Checked In ║
║ 14 ║ generaluser2 ║ restdoc4.txt ║ Restricted ║ XP2 ║ 2013-04-04 19:33:36 ║ Checked In ║
╚══════╩══════════════╩══════════════╩════════════╩══════╩═════════════════════╩════════════╝
答案 1 :(得分:0)
查询:
<强> SQLFIDDLEexample 强>
SELECT a.*
FROM TableName a
WHERE a.DateTime = (SELECT MAX(b.DateTime)
FROM TableName b
WHERE b.FileName = a.FileName)
结果:
| COL1 | COL2 | FILENAME | COL4 | COL5 | DATETIME | COL7 |
------------------------------------------------------------------------------------------------------
| 12 | generaluser1 | shared2.txt | Shared | XP1 | April, 04 2013 19:33:03+0000 | Checked In |
| 13 | generaluser2 | restdoc3.txt | Restricted | XP2 | April, 04 2013 19:33:28+0000 | Checked In |
| 14 | generaluser2 | restdoc4.txt | Restricted | XP2 | April, 04 2013 19:33:36+0000 | Checked In |