1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Access/SQL 同じグループのうち最新の日付のレコードを取得する

Last updated at Posted at 2019-03-08

以下のデータのうち
・Typeでグループ分けしたものの中で、最新(MAX)のDateのレコードを取得する
・Dateが空白のものは無視する

テーブル1

Name Model Qty Date
A AAA 1 2016/12/19
A BBB 1 2016/12/19
A BBB 1 2016/12/21
A BBB 2 2018/10/29
A CCC 2 2018/10/29
A CCC 2

※以下の3つになるように

Name Model Qty Date
A AAA 1 2016/12/19
A BBB 2 2018/10/29
A CCC 2 2018/10/29
qiita.rb

insert into テーブル2 (Name,Model,Number_of_Qty,Date) 
select n.Name,n.Model,n.Number_of_Qty,n.Date
FROM テーブル1 As n
INNER JOIN (SELECT Name,Model,MAX(Date) AS maxdate
FROM テーブル1 GROUP BY Name,Model) as s
ON n.Name = s.Name  AND n.Model= s.ModelAND n.Date = s.maxdate
GROUP BY n.Name, n.Model, n.Number_of_Qty,n.Date

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?