0

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

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

    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     |
4

2 回答 2

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
于 2012-12-06T18:15:24.363 回答
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;
于 2012-12-06T18:18:48.870 回答