简体   繁体   English

在SQL查询中使用MAX函数过滤数据

[英]Use of MAX function in SQL query to filter data

The code below joins two tables and I need to extract only the latest date per account, though it holds multiple accounts and history records. 下面的代码连接了两个表,尽管每个帐户包含多个帐户和历史记录,但我只需要提取每个帐户的最新日期。 I wanted to use the MAX function, but not sure how to incorporate it for this case. 我想使用MAX函数,但不确定在这种情况下如何合并它。 I am using My SQL server. 我正在使用我的SQL服务器。

Appreciate any help ! 感谢任何帮助!

select 
    PROP.FileName,PROP.InsName, PROP.Status,
    PROP.FileTime, PROP.SubmissionNo, PROP.PolNo, 
    PROP.EffDate,PROP.ExpDate, PROP.Region,  
    PROP.Underwriter, PROP_DATA.Data , PROP_DATA.Label
from
    Property.dbo.PROP 
inner join 
    Property.dbo.PROP_DATA on Property.dbo.PROP.FileID = Actuarial.dbo.PROP_DATA.FileID
where 
    (PROP_DATA.Label in ('Occupancy' , 'OccupancyTIV')) 
    and (PROP.EffDate >= '42278' and PROP.EffDate <= '42643')
    and (PROP.Status = 'Bound')
    and (Prop.FileTime = Max(Prop.FileTime))
order by
    PROP.EffDate DESC 

Assuming your DBMS supports windowing functions and the with clause, a max windowing function would work: 假设您的DBMS支持窗口功能和with子句,则可以使用max窗口功能:

with all_data as (
  select
   PROP.FileName,PROP.InsName, PROP.Status,
   PROP.FileTime, PROP.SubmissionNo, PROP.PolNo, 
   PROP.EffDate,PROP.ExpDate, PROP.Region,  
   PROP.Underwriter, PROP_DATA.Data , PROP_DATA.Label,
   max (PROP.EffDate) over (partition by PROP.PolNo) as max_date

  from Actuarial.dbo.PROP 
      inner join Actuarial.dbo.PROP_DATA 
      on Actuarial.dbo.PROP.FileID = Actuarial.dbo.PROP_DATA.FileID
  where (PROP_DATA.Label in ('Occupancy' , 'OccupancyTIV')) 
      and (PROP.EffDate >= '42278' and PROP.EffDate <= '42643')
      and (PROP.Status = 'Bound')
      and (Prop.FileTime = Max(Prop.FileTime))
)
select
  FileName, InsName, Status, FileTime, SubmissionNo,
  PolNo, EffDate, ExpDate, Region, UnderWriter, Data, Label
from all_data
where EffDate = max_date
ORDER BY EffDate DESC 

This also presupposes than any given account would not have two records on the same EffDate. 这还假设任何给定帐户在同一EffDate上都不会有两个记录。 If that's the case, and there is no other objective means to determine the latest account, you could also use row_numer to pick a somewhat arbitrary record in the case of a tie. 如果是这样,并且没有其他客观的方法来确定最新的帐户,则在出现平局的情况下,您还可以使用row_numer来选择某种任意记录。

Using straight SQL, you can use a self-join in a subquery in your where clause to eliminate values smaller than the max, or smaller than the top n largest, and so on. 使用直接SQL,可以在where子句的子查询中使用自联接,以消除小于max或小于前n个最大值的值,依此类推。 Just set the number in <= 1 to the number of top values you want per group. 只需将<= 1中的数字设置为每个组所需的最大值。

Something like the following might do the trick, for example: 例如,类似以下内容的方法可能会解决问题:

select
    p.FileName
    , p.InsName
    , p.Status
    , p.FileTime
    , p.SubmissionNo
    , p.PolNo
    , p.EffDate
    , p.ExpDate
    , p.Region
    , p.Underwriter
    , pd.Data
    , pd.Label
from Actuarial.dbo.PROP p
inner join Actuarial.dbo.PROP_DATA pd
    on p.FileID = pd.FileID
where (
    select count(*)
    from Actuarial.dbo.PROP p2
    where p2.FileID = p.FileID
    and p2.EffDate <= p.EffDate
    ) <= 1
and (
    pd.Label in ('Occupancy' , 'OccupancyTIV')
    and p.Status = 'Bound'
)
ORDER BY p.EffDate DESC 

Have a look at this stackoverflow question for a full working example. 请查看此stackoverflow问题以获取完整的工作示例。

Not tested 未测试

with temp1 as
(
select foo
from bar
whre xy = MAX(xy)
)
                select PROP.FileName,PROP.InsName, PROP.Status,
                 PROP.FileTime, PROP.SubmissionNo, PROP.PolNo, 
                 PROP.EffDate,PROP.ExpDate, PROP.Region,  
                 PROP.Underwriter, PROP_DATA.Data , PROP_DATA.Label

                from Actuarial.dbo.PROP 
                    inner join temp1 t
                    on Actuarial.dbo.PROP.FileID = t.dbo.PROP_DATA.FileID

                    ORDER BY PROP.EffDate DESC 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM