选择按日期排序的每组的前 N ​​行

Posted

技术标签:

【中文标题】选择按日期排序的每组的前 N ​​行【英文标题】:Selecting the first N rows of each group ordered by date 【发布时间】:2021-07-09 01:11:18 【问题描述】:

我正在尝试列出按 DateTime 分组(如 Master Detail)排序的前 N ​​行(前 100 行)。

USE [Test]
Create Table [dbo].[Masters] (
    [MasterId] [nchar](36) NOT NULL PRIMARY KEY,
    [Tags] [nchar](100) NULL,
    [Numbers] [int] NOT NULL
);

Create Table [dbo].[Details] (
    [DetailId] [nchar](36) NOT NULL PRIMARY KEY,
    [MasterId] [nchar](36) FOREIGN KEY REFERENCES Masters(MasterId),
    [Date_Time] [datetime2](7) NOT NULL,
    [Value] [int] NOT NULL
);


INSERT INTO Masters (MasterId, Tags, Numbers) VALUES ('M0', 'Tag0,Tag1', 6);
INSERT INTO Masters (MasterId, Tags, Numbers) VALUES ('M1', 'Tag1,Tag2', 5);
INSERT INTO Masters (MasterId, Tags, Numbers) VALUES ('M2', 'Tag0,Tag2', 6);

INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D0', 'M0', '20190101 00:00:00 AM', 0);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D1', 'M0', '20200101 11:00:00 AM', 1);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D2', 'M0', '20200701 01:00:00 AM', 2);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D3', 'M0', '20210715 10:00:00 AM', 3);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D4', 'M0', '20210715 11:00:00 AM', 4);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D5', 'M0', '20210715 11:00:00 AM', 5);

INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D10', 'M1', '20190101 00:00:00 AM', 6);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D11', 'M1', '20200101 01:00:00 AM', 7);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D12', 'M1', '20200701 09:00:00 AM', 8);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D13', 'M1', '20210101 10:00:00 AM', 9);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D14', 'M1', '20210701 10:00:00 AM', 10);

INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D20', 'M2', '20190101 00:00:00 AM', 11);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D21', 'M2', '20190101 01:30:00 AM', 12);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D22', 'M2', '20200101 01:30:00 AM', 13);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D23', 'M2', '20200701 08:30:00 AM', 14);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D24', 'M2', '20210101 01:30:00 AM', 15);
INSERT INTO Details (DetailId, MasterId, Date_Time, Value) VALUES ('D25', 'M2', '20210701 01:30:00 AM', 16);

Select * from Masters;
Select * from Details;
--

现在我的部分查询:

SELECT m.MasterId, d.DetailId, m.Numbers, d.Date_Time, d.Value from Details AS d
INNER JOIN Masters AS m ON m.MasterId = d.MasterId
WHERE 
m.Tags LIKE '%Tag2%' AND 
d.Date_Time >= Convert(datetime, '2020-01-01' ) 
ORDER BY m.MasterId DESC, d.Date_Time;

但是,如何在这个例子中为我的查询引入Top 3(实际情况下可能是50或100)?我只想获得3行每MasterId

根据图像,我们只会得到六行。 请帮我解决我的问题。

【问题讨论】:

根据问题指南,请不要发布代码、数据、错误消息等的图像 - 复制或键入问题中的文本。请保留将图像用于图表或演示渲染错误,无法通过文本准确描述的事情。 【参考方案1】:

您可以在此处使用row_number() 窗口函数。

SELECT x.masterid,
       x.detailid,
       x.numbers,
       x.date_time,
       x.value
       FROM (SELECT m.masterid,
                    d.detailid,
                    m.numbers,
                    d.date_time,
                    d.value,
                    row_number() OVER (PARTITION BY m.masterid
                                       ORDER BY d.date_time ASC) AS rn
                    FROM details AS d
                         INNER JOIN masters AS m
                                    ON m.masterid = d.masterid
                    WHERE m.tags LIKE '%Tag2%'
                          AND d.date_time >= '2020-01-01') AS x
       WHERE x.rn <= 3 -- change to whatever your n is
       ORDER BY x.masterid DESC,
                x.date_time ASC;

【讨论】:

【参考方案2】:

除了row_number 解决方案,另一个选项是CROSS APPLY(SELECT TOP

SELECT m.masterid,
       d.detailid,
       m.numbers,
       d.date_time,
       d.value
    FROM masters AS m
    CROSS APPLY (
        SELECT TOP (3) *
        FROM details AS d
        WHERE d.date_time >= '2020-01-01'
        AND m.masterid = d.masterid
    ) AS d
    WHERE m.tags LIKE '%Tag2%'
    ORDER BY m.masterid DESC,
             d.date_time;

这可能比row_number 更快或更慢,主要取决于基数(行数)和索引。

如果索引良好且行数较少,通常会更快。如果内表需要排序,或者您要选择大多数行,请使用row_number

【讨论】:

以上是关于选择按日期排序的每组的前 N ​​行的主要内容,如果未能解决你的问题,请参考以下文章

获取每组的前 n 个结果 [重复]

MySQL取每组的前N条记录

Oracle分组查询取每组排序后的前N条记录

Rank 获取排序的序号

mysql分组后,取每组的前3条数据(并且有顺序)

oracle开展分组后,取出每组的前几条数据