选择最高日期即使重复

时间:2012-12-06 18:14:04

标签: mysql date

我有一个包含日期列的数据库。这些日期可能有所不同,有些日期相同。如果日期重复,我如何选择最高日期组?

这就是我正在使用的,它只是提出了第一个实例:

    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_LOOKUP_CODE | STATE | COUNTY | CITY |  Zipcode   |           
    +-----------------+-------+--------+------+------------+
    |    123          |   NC  | BLah   | city |  27007     |

2 个答案:

答案 0 :(得分:0)

试试这个::

SELECT data.Company,

MAX(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')
    GROUP BYdata.Eff_Date

答案 1 :(得分:0)

我可能建议使用子查询来检索每个公司的max(eff_date),然后将该子查询加入其余公司:

SELECT d.Company, 
  d.Eff_Date, 
  d.Monthly_Rate, 
  d.Plan,        
  z.ZIP_LOOKUP_CODE AS Expr1, 
  z.State, 
  z.County, 
  z.City, 
  z.Zipcode
FROM data d
INNER JOIN zips z
  ON d.ZIP_LOOKUP_CODE = z.ZIP_LOOKUP_CODE
INNER JOIN 
(
  select max(Eff_Date) Eff_Date, Company, Plan
  from data
  group by company, Plan
) d1
  on d.Eff_Date = d1.Eff_Date
  and d.company = d1.company
  and d.plan = d1.plan
WHERE (z.Zipcode = '27012') AND
    (d.Company_Old LIKE '%CSI%') AND
    (d.Plan IN ('A','C','F','F (High)','G','N')) AND
    (d.Gender = 'Female') AND
    (d.Age = '65') AND
    (d.Tobacco = 'Non-Tobacco') AND
    (d.State = 'NC')
Order by d.Eff_Date;