0

我有一张桌子,每个客户都可以提交许多请求,但只会接受最新的请求。

例如,Customer1 可能只提交了 1 个请求,而 CustomerX 可能已经提交了 10 个请求。因此,当我提取报告时,它会带来 customer1 的第 1 个请求和 CustomerX 的第 10 个请求。

我怎样才能做到这一点?

客户 ID、FoodSelection1、FoodSelection2、提交日期

4

4 回答 4

1

您可以将WITH TIES子句与Row_Number()一起使用

Select Top 1 with ties *
 From YourTable
 Order By Row_Number() over (Partition By CustomerID Order by DateSubmitted Desc)
于 2017-04-10T20:07:42.983 回答
0
select 
    t.Customer, 
    t.id, 
    t.FoodSelection1, 
    t.FoodSelection2, 
    t.DateSubmitted 
from MyTable T1 as t 
where t.datesubmited in 
     (select max(r.datesubmited) 
      from table 1 as r
      where t.idCustomer = r.idCustomer
      group by r.idcustomer)
于 2017-04-10T20:17:49.617 回答
0
SELECT m.CustomerId, m.FoodSelection1, m.FoodSelection2, m.DateSubmitted FROM tblpm m 
  WHERE m.DateSubmitted =(SELECT max(n.DateSubmitted ) FROM tblpm n 
                       where n.CustomerId=m.CustomerIdORDER)
于 2017-04-10T20:09:28.387 回答
0
select * from MyTable T1 
where not exists --exclude records where 
(
select 1 from MyTable T2 
where T1.[Customer Id]=T2.[Customer Id] --there is matching rcd for customer
and T1.DateSubmitted<T2.DateSubmitted --a newer one exists
)
于 2017-04-10T20:09:44.290 回答