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 に答える