[英]Distinct and latest submissions
I have a table where each customer can submit many requests, but only latest requests would be entertained. 我有一张桌子,每个客户都可以提交许多请求,但只有最新的请求可以受理。
For example, Customer1 may have only submitted 1 request and CustomerX may have submitted 10 requests. 例如,Customer1可能仅提交了1个请求,而CustomerX可能已提交了10个请求。 So when I pull the report it would bring the 1 request by customer1 and the 10th request by CustomerX.
因此,当我提取报表时,它将带来customer1的1请求和CustomerX的第十个请求。
How can I do that? 我怎样才能做到这一点?
Customer Id, FoodSelection1, FoodSelection2, DateSubmitted 客户ID,FoodSelection1,FoodSelection2,提交日期
You can use the WITH TIES clause in concert with Row_Number() 您可以将WITH TIES子句与Row_Number()配合使用
Select Top 1 with ties *
From YourTable
Order By Row_Number() over (Partition By CustomerID Order by DateSubmitted Desc)
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)
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
)
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.