从每个客户发票中选择最新的开票日期,并从同一客户处选择多个发票

时间:2017-04-26 20:27:06

标签: sql sql-server ssms

我只是需要它从表中拉出最后的“最后开票日期”而不是一切。任何想法如何做到这一点?或者任何人都可以指出我正确的方向。我是sql查询的新手。我正在使用MS sql sever 2016。

Vendor_Number   Vendor_Name       VendorInvoiceDate     DaysLastInvoiced
    3568    a INDUSTRIES INC    2016-11-12 07:02:41.743   165
    3568    a INDUSTRIES INC    2016-10-08 07:03:31.170   200
    3568    a INDUSTRIES INC    2016-10-06 07:09:46.880   202
    3568    a INDUSTRIES INC    2016-09-21 07:04:30.267   217
    3568    a INDUSTRIES INC    2016-04-26 07:04:27.570   365
    3215    o Outdoors, Inc.    2016-03-02 07:39:30.137   420
    3215    o Outdoors, Inc.    2016-02-25 07:24:04.207   426
3215    o Outdoors, Inc.    2016-02-22 07:02:51.787 429  

在这里输入代码

Select distinct
      REF02_VR as Vendor_Number
    , N102_RI as Vendor_Name
    , VendorInvoiceDate
    , DATEDIFF(day,(select VendorInvoiceDate),Getdate()) AS DaysLastInvoiced
From 
     INSCIO_EDI810 a
WHERE
     1 = 1   
and 
    a.VendorInvoiceDate >= '2016-01-01 00:37:21.370'
order by
    N102_ri, a.VendorInvoiceDate desc

1 个答案:

答案 0 :(得分:3)

使用 top with ties row_number() 获取每个供应商的最新发票:

select top 1 with ties
    ref02_vr as Vendor_Number
  , N102_ri  as Vendor_Name
  , VendorInvoiceDate
  , datediff(day,VendorInvoiceDate,getdate()) as DaysLastInvoiced
from inscio_edi810 a
where a.VendorInvoiceDate >= '2016-01-01 00:37:21.370'
order by row_number() over (partition by ref02_vr order by VendorInvoiceDate desc)

要添加额外的order by,请将上述内容放在子查询中:

select *
from (
  select top 1 with ties
      ref02_vr as Vendor_Number
    , N102_ri  as Vendor_Name
    , VendorInvoiceDate
    , datediff(day,VendorInvoiceDate,getdate()) as DaysLastInvoiced
  from inscio_edi810 a
  where a.VendorInvoiceDate >= '2016-01-01 00:37:21.370'
  order by row_number() over (partition by ref02_vr order by VendorInvoiceDate desc)
  ) s
order by DaysLastInvoiced desc

with common_table_expression as ()使用 row_number() 版本:

;with cte as (
  select  *
      , rn = row_number() over (
              partition by ref02_vr
              order by VendorInvoiceDate desc
            )
    from inscio_edi810
)
select 
    ref02_vr as Vendor_Number
  , N102_ri  as Vendor_Name
  , VendorInvoiceDate
  , datediff(day,VendorInvoiceDate,getdate()) as DaysLastInvoiced
from cte
where rn = 1
order by DaysLastInvoiced desc

cross apply 版本:

select distinct
   a.ref02_vr as Vendor_Number
 , a.N102_ri  as Vendor_Name
 , x.VendorInvoiceDate
 , datediff(day,x.VendorInvoiceDate,getdate()) as DaysLastInvoiced
from inscio_edi810 a
  cross apply (
    select top 1 VendorInvoiceDate
      from inscio_edi810 as i
      where i.ref02_vr =a.ref02_vr
      order by i.VendorInvoiceDate desc
     ) as x;
order by DaysLastInvoiced desc

三个版本中的每个版本的rextester演示:http://rextester.com/UBX8146

每个都返回:

+---------------+------------------+---------------------+------------------+
| Vendor_Number |   Vendor_Name    |  VendorInvoiceDate  | DaysLastInvoiced |
+---------------+------------------+---------------------+------------------+
|          3568 | a INDUSTRIES INC | 2016-11-12 07:02:41 |              165 |
|          3215 | o Outdoors, Inc. | 2016-03-02 07:39:30 |              420 |
+---------------+------------------+---------------------+------------------+