English 中文(简体)
如何根据MySQL中唯一ID的多个结果选择MAX记录
原标题:How to select a MAX record based on multiple results from a unique ID in MySQL

我在mysql中运行了一个报告,它返回所有活动成员及其相关的计划选择。如果一个成员被终止,这也会显示在报告中。然而,我面临的问题是,当一位成员只是决定改变计划时。我们的系统有效地终止了原始计划,并以新计划重新开始,同时保留会员的原始注册信息。

示例报告数据可能如下所示:

MemberID | Last    | First | Plan Code |  Original Effective | Change Date   |   Term Date
--------------------------------------------------------------------------------------------
12345    | Smith   | John  |     A     |   05-01-2011        |               |   06-01-2011
12345    | Smith   | John  |     B     |   06-01-2011        |               |      

在上面的例子中,一名成员在2011年5月1日至2011年6月1日期间拥有计划代码a。2011年6月1日,该成员将其保险范围更改为B计划(由于没有任期或变更数据,因此仍处于活动状态)。

最终,我需要我的报告生成以下内容,而不是上面的2行项目:

MemberID | Last    | First | Plan Code |  Original Effective | Change Date   |   Term Date
--------------------------------------------------------------------------------------------
12345    | Smith   | John  |     B     |   05-01-2011        |  06-01-2011   | 

显示其原始计划生效日期、变更日期,并将终止字段留空。

我知道我可以通过使用Group by the Member ID进入一行,我甚至可以用(IF COUNT()>;1) 声明,但我不知道如何显示正确的计划代码(C),也不知道如何将期限留空以保持原始生效日期。

有什么想法吗?

最佳回答

尽管我讨厌带有嵌入空格的列,并且不得不<code>勾选</code>,但这应该对你有用。PreQuery将检测有多少策略条目,并保留第一个和最后一个,按成员分组。一旦完成,它可以重新加入同一成员的计划注册,但只匹配该人的最后一个“原始生效”日期。。。这将为您提供正确的计划代码。此外,如果此人被解雇,则会为您填写其日期。如果仍被雇佣,则该记录将为空白。

select STRAIGHT_JOIN
      pe2.MemberID,
      pe2.Last,
      pe2.First,
      pe2.`Plan Code`
      PreQuery.PlanStarted `Original Effective`,
      case when PreQuery.PlanEntries > 1 then PreQuery.LastChange end `Change Date`,
      pe2.`Term Date`
   from
      ( select 
              pe.MemberID,
              count(*) as PlanEntries,
              min( `pe`.`Original Effective` ) PlanStarted,
              max( `pe`.`Original Effective`) LastChange
           from
              PlanEnrollment pe
           group by
              pe.MemberID ) PreQuery

     join PlanEnrollment pe2
        on PreQuery.MemberID = pe2.MemberID
        AND PreQuery.LastChange = pe2.`Original Effective`
问题回答

I remember having the problem, but not coming up with a GROUP BY-based solution ;) Instead, I think you can do something like

SELECT memberid, plancode 
FROM members 
INNER JOIN plans ON members.id=plans.member_id 
WHERE plans.id IN 
  (SELECT id FROM plans 
   WHERE member_id = members.id 
   ORDER BY date DESC 
   LIMIT 0,1)




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签