选择数据库中的第一条记录

时间:2018-03-25 22:45:40

标签: sql sql-server

如果我没有正确解释,请原谅我。但我有以下记录

yearID    franchID    name
---------------------------
1966      ATL         Atlanta Braves
1883      ATL         Boston Beaneaters
1883      PHI         Philadelphia Quakers
1936      ATL         Boston Bees
1993      PHI         Philadelphia Phillies

但是我想要特许经营的起始年份和原始名称,所以输出应该是这样的:

StartYear    franchI    name
--------------------------------
1883         ATL        Boston Beaneaters
1883         PHI        Philadelphia Quakers

我现在拥有的SQL就是:

SELECT 
    MIN(yearID) as StartYear, franchID, name
FROM dbo.LahmanTeams
GROUP BY franchID, name;

这给了我第一个输出,但我想得到原来的特许经营名称。

5 个答案:

答案 0 :(得分:4)

您可以将WITH TIES子句与Row_Number()

一起使用

示例

Select top 1 with ties *
 From  dbo.LahmanTeams
 Order By Row_Number() over (Partition By franchID  Order By yearID  )

答案 1 :(得分:1)

;WITH CTE
AS
(
    SELECT ROW_NUMBER() OVER (PARTITION BY franchID ORDER BY yearID) [RowNo],  yearID , franchID, name
    FROM dbo.LahmanTeams
)
SELECT A.yearID [StartYear], A.franchID, A.name
FROM
    dbo.LahmanTeams A
INNER JOIN
    CTE A
    ON A.franchID = B.franchID
    AND A.yearID = B.yearID
WHERE
    B.RowNo = 1;

答案 2 :(得分:1)

我会使用CTE(没有理由加入)

CREATE Table #LahmanTeams
(   yearID  INT,
    franchID varchar(255),
    [name] varchar(255)
)

INSERT INTO #LahmanTeams VALUES (1966, 'ATL', 'Atlanta Braves')
INSERT INTO #LahmanTeams VALUES (1883, 'ATL', 'Boston Beaneaters')
INSERT INTO #LahmanTeams VALUES (1883, 'PHI', 'Philadelphia Quakers')
INSERT INTO #LahmanTeams VALUES (1936, 'ATL', 'Boston Bees')
INSERT INTO #LahmanTeams VALUES (1993, 'PHI', 'Philadelphia Phillies');

WITH teams AS
(
SELECT 
   yearID, franchID, name,                 Row_number() 
                  OVER ( 
                    partition BY franchid 
                    ORDER BY yearid asc) AS rn 

FROM #LahmanTeams
GROUP BY franchID, name, yearID
)
SELECT * from teams where rn = 1

输出:

1883    ATL Boston Beaneaters       1
1883    PHI Philadelphia Quakers    1

答案 3 :(得分:1)

我认为每个人都在想这个。你最初的努力几乎就在那里,但对于不断变化的团队名称。

首先,获取每个特许经营权ID的最小年份(即子查询),然后将其加入到完整表中以获取该条目的其余列(并且这是外部查询)。

declare @LahmanTeams table (
yearID int,
franchID char(3),
name varchar(50));

insert @LahmanTeams
values
(1966,'ATL','Atlanta Braves')
,(1883,'ATL','Boston Beaneaters')
,(1883,'PHI','Philadelphia Quakers')
,(1936,'ATL','Boston Bees')
,(1993,'PHI','Philadelphia Phillies');

SELECT 
    t.*
FROM @LahmanTeams AS t
JOIN
    (
        SELECT 
            franchID,
            MIN(yearID) as yearID
        FROM 
            @LahmanTeams 
        GROUP BY 
            franchID
    ) AS s
        ON s.franchID = t.franchID
        AND s.yearID = t.yearID

结果:

+--------+----------+----------------------+
| yearID | franchID |         name         |
+--------+----------+----------------------+
|   1883 | ATL      | Boston Beaneaters    |
|   1883 | PHI      | Philadelphia Quakers |
+--------+----------+----------------------+

答案 4 :(得分:0)

如果我理解正确,那么普通ORDER BY就足够了:

SELECT 
    MIN(yearID) as StartYear, franchID, name
FROM dbo.LahmanTeams
GROUP BY franchID, name
ORDER BY StartYear;