我无法弄清楚我在做什么错。我正在查询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。知道我在这里做错了吗?
答案 0 :(得分:1)
问题应该与列类型有关。从SQLite文档about date time,
SQLite没有预留存储类来存储日期和/或时间。相反,SQLite内置的日期和时间功能可以将日期和时间存储为TEXT,REAL或INTEGER值:
我不知道创建表时为什么没有警告或错误,但是比较似乎是使用字符串比较而不是日期。该列更改为TEXT
,并且在我的sql提琴中应用了日期格式'YYYY-MM-DD'。然后在查询中使用date
函数,一切似乎正常。
答案 1 :(得分:-2)
您需要按最大日期分组。试试这个-
SELECT *
FROM Status_Table
WHERE Date IN (SELECT MAX(Date)
FROM Status_Table
GROUP BY College);