我有一个表格,每个客户可以提交许多请求,但只接受最新的请求。
例如,Customer1可能只提交了1个请求,而CustomerX可能已提交了10个请求。因此,当我提取报告时,它将带来customer1的1请求和CustomerX的第10个请求。
我该怎么做?
客户ID,FoodSelection1,FoodSelection2,DateSubmitted
答案 0 :(得分:1)
您可以与 Row_Number()
一起使用 WITH TIES 子句Select Top 1 with ties *
From YourTable
Order By Row_Number() over (Partition By CustomerID Order by DateSubmitted Desc)
答案 1 :(得分: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)
答案 2 :(得分: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
)
答案 3 :(得分: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)