如何让它只返回最新生效日期的一行?

时间:2017-05-10 15:36:48

标签: sql sql-server tsql sql-server-2012

Sample 1

这是我得到的结果类型的摘要版本,我想缩小它以仅显示如果Payee和Tractor相同的行,那么只显示具有代码的行最新"生效日期"。生效日期是“守则”实施的时间。因此,我不需要它来向我显示某人的过时代码,我不想进入并删除他们的代码历史记录以便以这种方式消除它们。我无法显示实际结果,因为我正在使用的表格中有一些敏感的客户信息。

*生效日期不会显示在结果中,但我将其插入那里以便您可以看到它。

目前我的代码看起来像这样......我不确定如何使用Seg_Alloc_Detail表中的Max(Effective_Date)来让它做我想做的事情。似乎我需要在select语句中使用某种嵌套条件来获得我想要的东西,但我不确定如何。

select distinct 
    Driver.Payee_ID as 'Payee Code',
    Payee.Name,
    Seg_Alloc_Code as 'Allocation Code',
    max(Effective_Date) as 'Effective Date', 
    Driver1_id as 'Assigned Driver',
    Tractor.ID as 'Tractor Number',
    Fleet_ID as 'Fleet', 
    Fuel_Card.Payee_ID as 'Card Payee',
    Fuel_Expense_ID as 'Expense Code'
from
    Driver
inner join
    Payee on Payee.ID = Driver.Payee_ID
inner join
    Seg_Alloc_Detail on Seg_Alloc_Detail.EQ_Code = Payee.ID
inner join
    Tractor on Tractor.Driver1_id = Seg_Alloc_Detail.EQ_Code
inner join
    Fuel_Card on Fuel_Card.Payee_ID = Tractor.Driver1_id

where Is_Active = 'Y' and Driver.Company_ID = 'TMS' and Seg_Alloc_Detail.Company_ID = 'TMS'

group by
    Tractor.ID,
    Driver.Payee_ID,
    Payee.Name,
    Seg_Alloc_Code,
    Driver1_id,
    Fleet_ID, 
    Fuel_Card.Payee_ID,
    Fuel_Expense_ID

3 个答案:

答案 0 :(得分:2)

我假设这是sql server,因为你有很多sql server标签。您可以利用ROW_NUMBER来处理此类事情。这是实现这一目标的一种方法。

select * --Really you would want to select only the columns from your subquery you want displayed instead of *
from
(
    select distinct 
        Driver.Payee_ID as 'Payee Code',
        Payee.Name,
        Seg_Alloc_Code as 'Allocation Code',
        max(Effective_Date) as 'Effective Date', 
        Driver1_id as 'Assigned Driver',
        Tractor.ID as 'Tractor Number',
        Fleet_ID as 'Fleet', 
        Fuel_Card.Payee_ID as 'Card Payee',
        Fuel_Expense_ID as 'Expense Code'
        , ROW_NUMBER() over (partition by Payee.Name, Tractor.ID order by Effective_Date desc) as RowNum
    from
        Driver
    inner join
        Payee on Payee.ID = Driver.Payee_ID
    inner join
        Seg_Alloc_Detail on Seg_Alloc_Detail.EQ_Code = Payee.ID
    inner join
        Tractor on Tractor.Driver1_id = Seg_Alloc_Detail.EQ_Code
    inner join
        Fuel_Card on Fuel_Card.Payee_ID = Tractor.Driver1_id

    where Is_Active = 'Y' and Driver.Company_ID = 'TMS' and Seg_Alloc_Detail.Company_ID = 'TMS'

    group by
        Tractor.ID,
        Driver.Payee_ID,
        Payee.Name,
        Seg_Alloc_Code,
        Driver1_id,
        Fleet_ID, 
        Fuel_Card.Payee_ID,
        Fuel_Expense_ID
) x
where x.RowNum = 1

答案 1 :(得分:0)

使用top 1 with tiesrow_number()

select * 
from (
  select top 1 with ties
      Driver.Payee_ID as [Payee Code]
    , Payee.Name
    , Seg_Alloc_Code as [Allocation Code]
    , Effective_Date as [Effective Date]
    , Driver1_id as [Assigned Driver]
    , Tractor.ID as [Tractor Number]
    , Fleet_ID as [Fleet]
    , Fuel_Card.Payee_ID as [Card Payee]
    , Fuel_Expense_ID as [Expense Code]
  from Driver 
    inner join Payee
      on Payee.ID = Driver.Payee_ID 
    inner join Seg_Alloc_Detail
      on Seg_Alloc_Detail.EQ_Code = Payee.ID 
    inner join Tractor
      on Tractor.Driver1_id = Seg_Alloc_Detail.EQ_Code 
    inner join Fuel_Card
      on Fuel_Card.Payee_ID = Tractor.Driver1_id
  where Is_Active = 'Y' 
    and Driver.Company_ID = 'TMS' 
    and Seg_Alloc_Detail.Company_ID = 'TMS'
  order by row_number() over (
    partition by Payee.ID, Tractor.ID 
    order by Effective_Date desc
    )
  ) s
order by [Tractor Number]

答案 2 :(得分:0)

您可以使用row_number并获取desc生效日期的第一个值

;with cte as (
    select  
        Driver.Payee_ID as 'Payee Code',
        Payee.Name,
        Seg_Alloc_Code as 'Allocation Code',
        max(Effective_Date) as 'Effective Date', 
        Driver1_id as 'Assigned Driver',
        Tractor.ID as 'Tractor Number',
        Fleet_ID as 'Fleet', 
        Fuel_Card.Payee_ID as 'Card Payee',
        Fuel_Expense_ID as 'Expense Code'
    from
        Driver
    inner join
        Payee on Payee.ID = Driver.Payee_ID
    inner join
        Seg_Alloc_Detail on Seg_Alloc_Detail.EQ_Code = Payee.ID
    inner join
        Tractor on Tractor.Driver1_id = Seg_Alloc_Detail.EQ_Code
    inner join
        Fuel_Card on Fuel_Card.Payee_ID = Tractor.Driver1_id

    where Is_Active = 'Y' and Driver.Company_ID = 'TMS' and Seg_Alloc_Detail.Company_ID = 'TMS'

    group by
        Tractor.ID,
        Driver.Payee_ID,
        Payee.Name,
        Seg_Alloc_Code,
        Driver1_id,
        Fleet_ID, 
        Fuel_Card.Payee_ID,
        Fuel_Expense_ID
), CTE1 as
(
    select *, RowN = Row_Number() over (partition by [Name], [Tractor Number] order by [Effective Date] desc) from CTE 
)
select * from CTE1 where RowN = 1