我无法弄清楚我在这里做错了什么。我正在查询sqlite表以获取每个学院的最新记录,但结果并不完全准确。以下是源表:
College Status Date
"UTAR "CERTIFIED" "07/30/2018"
"UTA" "CERTIFIED" "03/19/2018"
"UTD" "CERTIFIED" "06/08/2018"
"UTEL" "CERTIFIED" "03/13/2018"
"UTSA" "CERTIFIED" "02/06/2018"
"UTT" "CERTIFIED" "03/14/2018"
"UTPB" "CERTIFIED" "07/23/2018"
"UTRG" "CERTIFIED" "04/12/2018"
"UTAR" "CERTIFIED" "05/31/2017"
"UTA" "CERTIFIED" "03/02/2017"
"UTD" "CERTIFIED" "04/13/2017"
"UTEL" "CERTIFIED" "03/10/2017"
"UTSA" "CERTIFIED" "02/13/2017"
"UTT" "CERTIFIED" "03/11/2017"
"UTPB" "CERTIFIED" "03/29/2017"
"UTRG" "CERTIFIED" "04/12/2017"
这是我的疑问:
SELECT *
FROM Status_Table AS S1
WHERE Date = (
SELECT MAX(Date)
FROM Status_Table AS S2
WHERE S1.College = S2.College
);
结果是:
College Status Date
"UTAR" "CERTIFIED" "07/30/2018"
"UTA" "CERTIFIED" "03/19/2018"
"UTD" "CERTIFIED" "06/08/2018"
"UTEP" "CERTIFIED" "03/13/2018"
"UTT" "CERTIFIED" "03/14/2018"
"UTPB" "CERTIFIED" "07/23/2018"
"UTRG" "CERTIFIED" "04/12/2018"
"UTSA" "CERTIFIED" "02/13/2017"
如果您在UTSA的情况下发现它没有获取最新的02/06/2018日期。知道我在这里做错了什么吗?
问题应该与列类型相关。从SQLite文档about date time,
SQLite没有为存储日期和/或时间而预留的存储类。相反,SQLite的内置日期和时间函数能够将日期和时间存储为TEXT,REAL或INTEGER值:
我不知道为什么创建表时没有警告或错误,但似乎比较是使用字符串比较而不是日期。该列更改为TEXT
,日期格式'YYYY-MM-DD'应用于我的sql小提琴。然后在查询中使用date
函数,一切似乎都有效。
您需要按最大日期分组。试试这个 -
SELECT *
FROM Status_Table
WHERE Date IN (SELECT MAX(Date)
FROM Status_Table
GROUP BY College);