2017-03-27 8 views
-1

残念ながら、初心者はここにあります。SQL:最初の行で値が変更されたレコードのみを取得します。

ここでこれと同様に

:私も非常に最初の行を含める方法を見つけるために望んでいたSQL: retrieve only the records whose value has changed

? *のある行は?

ID description eaudittimestamp 
-- ----------- ----------------------- 
777012 above  2017-03-27 10:09:59.330 * 
777012 above  2017-03-27 10:09:58.550 
777012 below  2017-03-27 10:26:03.560 * 
777012 below  2017-03-27 10:36:02.423 
777012 below  2017-03-27 10:37:15.250 
777012 middle  2017-03-27 10:49:11.680 * 
777012 middle  2017-03-27 10:49:18.870 
777013  something  2017-03-27 11:49:18.870 
777013  something  2017-03-27 12:49:18.870 
777014  nodescription 2017-03-27 12:49:18.870 

謝罪、私はこれに新しいです。私は言い換えることができます。 ID、説明、タイムスタンプを持つテーブルがあります。監査テーブルと同様です。説明が変更された行(最初の行を含む)を見つけるにはどうすればよいですか。

1行しか持たないものを除外するにはどうしたらいいですか?すなわち777013と777014?あなたはプロの持っているデータについては

+0

あなたは料金のためのような使用をしてもらいますトップ1を実行してください。 – maSTAShuFu

+0

今混在しています。 – maSTAShuFu

+0

はいごめんなさい、このサイトを使うことを学ぶ。 :) –

答えて

1
with ct as 
(
    select Code, Date, Rate, 
      row_number() over (partition by Code, Rate order by Code, Date) as rn 
    from your_table 
) 
select Code, Date, Rate 
from ct 
where rn = 1 
order by Code, Date; 

更新

declare @foo table(ID int, description varchar(100), eaudittimestamp datetime); 
insert into @foo values 
(777012, 'above', '2017-03-27 10:09:59.330'), 
(777012, 'above', '2017-03-27 10:09:58.550'), 
(777012, 'below', '2017-03-27 10:26:03.560'), 
(777012, 'below', '2017-03-27 10:36:02.423'), 
(777012, 'below', '2017-03-27 10:37:15.250'), 
(777012, 'middle', '2017-03-27 10:49:11.680'), 
(777012, 'middle', '2017-03-27 10:49:18.870'), 
(777013, 'something', '2017-03-27 11:49:18.870'), 
(777013, 'something', '2017-03-27 12:49:18.870'), 
(777014, 'nodescription', '2017-03-27 12:49:18.870'); 

with ct as 
(
    select ID, description, eaudittimestamp, 
      row_number() over (partition by ID, description order by eaudittimestamp) rn, 
      count(*) over (partition by ID) as ct 
    from @foo 
) 
select ID, Description, eaudittimestamp 
from ct 
where rn = 1 and ct > 1; 

GO 
 
    ID | Description | eaudittimestamp  
-----: | :---------- | :------------------ 
777012 | above  | 27/03/2017 10:09:58 
777012 | below  | 27/03/2017 10:26:03 
777012 | middle  | 27/03/2017 10:49:11 
777013 | something | 27/03/2017 11:49:18 

dbfiddle here

+0

ありがとうございます。申し訳ありませんが、私は質問を編集します。 –

1

vided、row_number()作品:

select t.* 
from (select t.*, 
      row_number() over (partition by id, description order by eaudittimestamp) as seqnum 
     from t 
    ) t 
where seqnum = 1; 

これはid/descriptionペアごとに1つの行を返します。一番小さいものはeaudittimestampです。それはあなたが望むもののようです。

EDIT:

あなたは、1行でグループを除外ちょうど別ウィンドウ関数を使用したい場合は、次の日にか、単にベースの最大または最小を

select t.* 
from (select t.*, 
      row_number() over (partition by id, description order by eaudittimestamp) as seqnum, 
      min(description) over (partition by id) as min_d, 
      max(description) over (partition by id) as max_d 
     from t 
    ) t 
where seqnum = 1 and min_d <> max_d; 
+0

ありがとうございました。倍数があればどうなりますか?私は、それは3行を返す必要がありますか? –

+0

@Kayenこれは、['row_number()'](https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql)を検索する部分です。あなた自身のためにそれを理解しようとすることを開始します。 [SQL Fiddle](http://sqlfiddle.com)でプレイしてください。私はあなたのための[デモ](http://sqlfiddle.com/#!3/b7c80/3)を作成しました。 – canon

+0

これは動作しますが、seq = 1のすべての行を返します。変更がある行だけを検索したいのであればどうでしょうか。 –

関連する問題