使用GROUP BY子句从MS Access中删除SELECT查询的相反

时间:2017-11-08 23:23:52

标签: sql ms-access

我在MS Access中有一个冗长的SELECT查询。我想基本上删除查询未选中的表中的所有内容。谢谢!

    SELECT [Benefit Refused Cancellation Report].BENEFIT_PERIOD, 
        [Benefit Refused Cancellation Report].BENEFIT_TYPE, 
        [Benefit Refused Cancellation Report].CARRIER_NAME, 
        [Benefit Refused Cancellation Report].PLAN_NAME, 
        [Benefit Refused Cancellation Report].PERSON_TYPE, 
        [Benefit Refused Cancellation Report].LAST_NAME, 
        [Benefit Refused Cancellation Report].FIRST_NAME, 
        [Benefit Refused Cancellation Report].MIDDLE_NAME, 
        [Benefit Refused Cancellation Report].SUFFIX, 
        [Benefit Refused Cancellation Report].GENDER, 
        [Benefit Refused Cancellation Report].DATE_OF_BIRTH, 
        [Benefit Refused Cancellation Report].DATE_OF_HIRE, 
        [Benefit Refused Cancellation Report].EFFECTIVE_DATE, 
        Max([Benefit Refused Cancellation Report].END_DATE) AS MaxOfEND_DATE, 
        [Benefit Refused Cancellation Report].SSN

FROM [Benefit Refused Cancellation Report]

GROUP BY [Benefit Refused Cancellation Report].BENEFIT_PERIOD, 
        [Benefit Refused Cancellation Report].BENEFIT_TYPE, 
        [Benefit Refused Cancellation Report].CARRIER_NAME, 
        [Benefit Refused Cancellation Report].PLAN_NAME, 
        [Benefit Refused Cancellation Report].PERSON_TYPE, 
        [Benefit Refused Cancellation Report].LAST_NAME, 
        [Benefit Refused Cancellation Report].FIRST_NAME, 
        [Benefit Refused Cancellation Report].MIDDLE_NAME, 
        [Benefit Refused Cancellation Report].SUFFIX, 
        [Benefit Refused Cancellation Report].GENDER, 
        [Benefit Refused Cancellation Report].DATE_OF_BIRTH, 
        [Benefit Refused Cancellation Report].DATE_OF_HIRE, 
        [Benefit Refused Cancellation Report].EFFECTIVE_DATE, 
        [Benefit Refused Cancellation Report].SSN;

1 个答案:

答案 0 :(得分:0)

您可以使用相关子查询执行此操作:

P(1,2)

这假设delete from [Benefit Refused Cancellation Report] where END_DATE < (select max(brdr2.End_Date) from [Benefit Refused Cancellation Report] as brcr2 where brcr2.SSN = [Benefit Refused Cancellation Report].SSN ); 唯一地定义了您想要一行的每一行。要真正匹配SSN,您需要包含更多字段 - 甚至包括所有字段。

一个警告:这不会考虑GROUP BY值,因此用于匹配的密钥应为非NULL