我有一个包含日期列的数据库。这些日期可能有所不同,有些日期相同。即使它们是重复的,我如何选择最高的日期组?
这就是我正在使用的,它只是提出第一个实例:
SELECT data.Company,data.Eff_Date, data.Monthly_Rate, data.Plan,
zips.ZIP_LOOKUP_CODE AS Expr1, zips.State, zips.County, zips.City, zips.Zipcode
FROM data INNER JOIN
zips ON data.ZIP_LOOKUP_CODE = zips.ZIP_LOOKUP_CODE
WHERE (zips.Zipcode = '27012') AND
(data.Company_Old LIKE '%CSI%') AND
(data.Plan IN ('A','C','F','F (High)','G','N')) AND
(data.Gender = 'Female') AND
(data.Age = '65') AND
(data.Tobacco = 'Non-Tobacco') AND
(data.State = 'NC')
Order by max(data.Eff_Date);
数据库日期示例:
+------+------------+-----------+------------+------------+---------+-----
| Company_Old | Plan | State | Tobacco | Eff_Date | Age | Gender | ZIP_LOOKUP_CODE |
+------+------------+-----------+------------+------------+---------+-----
| CSI | A | NC | No | 2012-12-01 | 65 | Male | 123
| CSI | C | NC | No | 2012-12-01 | 65 | Male | 123
| CSI | F | NC | No | 2012-12-01 | 65 | Male | 123
| CSI | FH | NC | No | 2012-12-01 | 65 | Male | 123
| CSI | G | NC | No | 2012-12-01 | 65 | Male | 123
| CSI | N | NC | No | 2012-12-01 | 65 | Male | 123
| CSI | A | NC | No | 2011-12-01 | 65 | Male | 123
| CSI | C | NC | No | 2011-12-01 | 65 | Male | 123
| CSI | F | NC | No | 2011-12-01 | 65 | Male | 123
| CSI | FH | NC | No | 2011-12-01 | 65 | Male | 123
| CSI | G | NC | No | 2011-12-01 | 65 | Male | 123
| CSI | N | NC | No | 2011-12-01 | 65 | Male | 123
数据库 zip 的示例数据:
+-----------------+-------+--------+------+------------+
| ZIP_LOOKUP_CODE | STATE | COUNTY | CITY | Zipcode |
+-----------------+-------+--------+------+------------+
| 123 | NC | BLah | city | 27007 |