如何选择仅在SQL中返回多行时返回第一行

时间:2019-04-01 15:33:54

标签: sql sql-server tsql

我有以下数据:

Id                                      Week1   Week2   Date
-------------------------------------------------------------------------------
C0935336-B424-E911-8117-005056A82772    201906  201904  2019-02-02 00:00:00.000
18D809B1-8725-E911-8117-005056A82772    201907  201904  2019-02-09 00:00:00.000
C95855A0-9428-E911-8117-005056A82772    201908  201905  2019-02-16 00:00:00.000
5ABE80F6-2531-E911-8117-005056A82772    201909  201905  2019-02-23 00:00:00.000
6B520DE4-9445-E911-8118-005056A82772    201910  201906  2019-03-02 00:00:00.000
ADD0A8D0-EE2E-E911-8117-005056A82772    201911  201906  2019-03-09 00:00:00.000

正如您所看到的,Week2作为重复的条目,我需要返回所返回的每一对行的第一行,以便最终得到与此类似的内容。

Id                                      Week1   Week2   Date
-------------------------------------------------------------------------------
C0935336-B424-E911-8117-005056A82772    201906  201904  2019-02-02 00:00:00.000
C95855A0-9428-E911-8117-005056A82772    201908  201905  2019-02-16 00:00:00.000
6B520DE4-9445-E911-8118-005056A82772    201910  201906  2019-03-02 00:00:00.000

我在SQL中使用以下内容:

SELECT DISTINCT 
    ROW_NUMBER() OVER (PARTITION BY Weeks.Week2 ORDER BY Weeks.Week2) AS Row#, 
    Data.Id, Weeks.Week1, Weeks.Week2, Weeks.Date 
FROM
    Data
INNER JOIN 
    Weeks ON Data.WeekN = Weeks.Week1
INNER JOIN
    Users ON Data.UserId = Users.UserId
WHERE 
    Weeks.Week2 IN (SELECT DISTINCT Weeks.Week2
                    FROM Data
                    INNER JOIN Weeks ON Data.Week = Weeks.Week1
                    INNER JOIN Users ON Data.UserId = Users.UserId
                    WHERE Data.UserId = 1234 AND Weeks.Week1 >= 201907)
ORDER BY 
    Weeks.Week2

其中为每组或每行返回引入行号:

Row# Id                                     Week1   Week2   Date
-----------------------------------------------------------------------------------
1    C0935336-B424-E911-8117-005056A82772   201906  201904  2019-02-02 00:00:00.000
2    18D809B1-8725-E911-8117-005056A82772   201907  201904  2019-02-09 00:00:00.000
1    C95855A0-9428-E911-8117-005056A82772   201908  201905  2019-02-16 00:00:00.000
2    5ABE80F6-2531-E911-8117-005056A82772   201909  201905  2019-02-23 00:00:00.000
1    6B520DE4-9445-E911-8118-005056A82772   201910  201906  2019-03-02 00:00:00.000
2    ADD0A8D0-EE2E-E911-8117-005056A82772   201911  201906  2019-03-09 00:00:00.000

我的问题是如何选择Row#为1的所有行?

1 个答案:

答案 0 :(得分:2)

如@stickybit所述,您可以使用:

SELECT
    Id
    , Week1
    , Week2
    , Date
FROM
    (
        SELECT
            ROW_NUMBER() OVER (PARTITION BY Weeks.Week2 ORDER BY Weeks.Week2) AS Row#
            , Data.Id
            , Weeks.Week1
            , Weeks.Week2
            , Weeks.Date
        FROM
            Data
            INNER JOIN Weeks ON Data.WeekN = Weeks.Week1
            INNER JOIN Users ON Data.UserId = Users.UserId
        WHERE Weeks.Week2 IN
            (
                SELECT DISTINCT Weeks.Week2
                FROM
                    Data
                    INNER JOIN Weeks ON Data.Week = Weeks.Week1
                    INNER JOIN Users ON Data.UserId = Users.UserId
                WHERE
                    Data.UserId = 1234
                    AND Weeks.Week1 >= 201907
            )
    ) Q
WHERE Row# = 1

您不必担心ORDER BY,因为ROW_NUMBER()函数在其OVER()子句中为您解决了这一问题。

您也不需要DISTINCT,因为ROW_NUMBER()函数将阻止它发挥任何作用。