2017-04-10 22 views
0

私は、各顧客が多くのリクエストを提出できるテーブルを用意していますが、最新のリクエストのみが楽しめます。個別の最新の投稿

たとえば、Customer1は1つの要求を提出し、CustomerXは10個の要求を提出している可能性があります。だから私は、レポートを引き出すと、customer1の1リクエストとCustomerXの10リクエストが表示されます。

どうすればいいですか?

顧客ID、FoodSelection1、FoodSelection2、DateSubmitted

+2

あなたでしょうROW_NUMBERを使用します。 https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql –

+0

これまでに何を試しましたか? – lukkea

答えて

1

あなたはROW_NUMBER(と協力しTIES WITH句を使用することができます)

Select Top 1 with ties * 
From YourTable 
Order By Row_Number() over (Partition By CustomerID Order by DateSubmitted Desc) 
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) 
+1

LIMIT 1はSQL Server構文ではありません。 – scsimon

+0

私はlimitを削除してcustomeridコントロールを追加しました – coenni

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 
) 
+0

[Customer Id]とDateSubmittedのインデックスが効率的に動作するようにしたいと考えています。 – jerry

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) 
関連する問題