SQL-切勿选择其他ID按日期排序的记录

时间:2019-05-09 03:35:28

标签: sql sql-server

所以我只想按日期对结果中的每个ID选择仅前1条记录,这样就不会再有具有相同ID的记录了。

这是一个更详细的示例:日期格式为YYYY-MM-DD

+----------+-------+-------------+
| Id       | name  | date        |
+----------+-------+-------------+
| 1        | a     | 2019-01-01  |
| 1        | a2    | 2019-01-02  |
| 2        | b     | 2019-01-01  |
| 3        | c     | 2019-01-02  |
| 3        | c2    | 2019-01-01  |
| 4        | d     | 2019-01-01  |
+----------+-------+-------------+

我想要的结果是这样的:

+----------+-------+-------------+
| Id       | name  | date        |
+----------+-------+-------------+
| 1        | a2    | 2019-01-02  |
| 2        | b     | 2019-01-01  |
| 3        | c     | 2019-01-02  |
| 4        | d     | 2019-01-01  |
+----------+-------+-------------+

所以我只希望每个ID获得一条记录,这是最新记录。

如何在SQL中实现此目标?

感谢那些会帮助您的人!

3 个答案:

答案 0 :(得分:5)

使用row_number()Id进行分区并按date desc进行排序

select *
from   (
          select *, rn = row_number() over (partition by [Id] order by [date] desc)
          from   yourtable
       ) d
where  d.rn = 1

答案 1 :(得分:2)

对于以下每个ID,我们也可以对SELECT a.Id, b.name, a.date FROM (SELECT Id, MAX(date) AS max_date FROM <your_table_name> GROUP BY Id) a INNER JOIN <your_table_name> b ON a.Id = b.Id AND a.max_date = b.date GROUP BY使用子查询

注意:请不要使用关键字作为列名

MAX

输出:

declare @table as table(Id INT, [name] varchar(50), [date] date)
insert into @table values
(1,'a',     '2019-01-01'),
(1,'a2',    '2019-01-02'),
(2,'b',     '2019-01-01'),
(3,'c',     '2019-01-02'),
(3,'c2',    '2019-01-01'),
(4,'d',     '2019-01-01')

SELECT t.*
FROM @table t
INNER JOIN (SELECT id, MAX([date]) AS [date] FROM @table GROUP BY id) t1 ON t1.[date] = t.[date]
    AND t1.id = t.id
ORDER BY t.id

答案 2 :(得分:0)

您还可以使用Debugger entered--Lisp error: (void-function record) record(yas--table "antlr-mode" #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ( ...)) #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ( ...)) nil (keymap)) yas--table-get-create(antlr-mode) yas-define-snippets(antlr-mode (("target" "<target name=\"${1:compile}\" ${2:other}>\n $0\n</target>" "target" nil nil nil "e:/baidu_cloud/new_home/.emacs.d/elpa/yasnippet-snippets-20190422.1416/snippets/antlr-mode/target" nil nil) ("prop" "<property name=\"${1:name}\" value=\"${2:value}\" />\n$0" "property" nil nil nil "e:/baidu_cloud/new_home/.emacs.d/elpa/yasnippet-snippets-20190422.1416/snippets/antlr-mode/property" nil nil) ("proj" "<project name=\"${1:test}\" default=\"${2:compile}\" basedir=\"${3:.}\">\n\n$0\n</project>" "project" nil nil nil "e:/baidu_cloud/new_home/.emacs.d/elpa/yasnippet-snippets-20190422.1416/snippets/antlr-mode/project" nil nil))) yas--load-directory-2("e:/baidu_cloud/new_home/.emacs.d/elpa/yasnippet-snippets-20190422.1416/snippets/antlr-mode" antlr-mode) yas--load-directory-1("e:/baidu_cloud/new_home/.emacs.d/elpa/yasnippet-snippets-20190422.1416/snippets/antlr-mode" antlr-mode) apply(yas--load-directory-1 ("e:/baidu_cloud/new_home/.emacs.d/elpa/yasnippet-snippets-20190422.1416/snippets/antlr-mode" antlr-mode)) #[128 "\302\300\303\301\"\"\207" [yas--load-directory-1 ("e:/baidu_cloud/new_home/.emacs.d/elpa/yasnippet-snippets-20190422.1416/snippets/antlr-mode" antlr-mode) apply append] 6 "\n\n(fn &rest ARGS2)"]() yas/load-directory("~/.emacs.d/elpa/yasnippet-snippets-20190422.1416/snippets/") eval-buffer(#<buffer *load*-583469> nil "e:/baidu_cloud/new_home/emacs/myautocomplete.el" nil t) ; Reading at buffer position 3100 load-with-code-conversion("e:/baidu_cloud/new_home/emacs/myautocomplete.el" "e:/baidu_cloud/new_home/emacs/myautocomplete.el" nil t) load("myautocomplete.el" nil t t) eval-buffer(#<buffer *load*> nil "e:/baidu_cloud/new_home/.emacs" nil t) ; Reading at buffer position 1429 load-with-code-conversion("e:/baidu_cloud/new_home/.emacs" "e:/baidu_cloud/new_home/.emacs" t t) load("~/.emacs" t t) #[0 "\205\266 并选择GROUP BY ID来获取此信息:

MAX(date)