简体   繁体   English

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

[英]Select first record in database

Forgive me if I'm not explaining this properly. 如果我没有正确解释,请原谅我。 But I have the following records 但是我有以下记录

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

But I want the starting year and original name of the franchise, so the output should look like this: 但是我想要特许经营权的起始年份和原始名称,因此输出应如下所示:

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

The SQL I have right now is this: 我现在拥有的SQL是这样的:

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

That gives me the first output, but I'd like to get the original franchise name. 这是我的第一个输出,但我想获得原始的特许经营名称。

You can use the WITH TIES clause in concert with Row_Number() 您可以将WITH TIES子句与Row_Number()配合使用

Example

Select top 1 with ties *
 From  dbo.LahmanTeams
 Order By Row_Number() over (Partition By franchID  Order By yearID  )
;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;

I would use a CTE (no reason for a join) 我将使用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

OUTPUT: 输出:

1883    ATL Boston Beaneaters       1
1883    PHI Philadelphia Quakers    1

I think everybody's over thinking this. 我认为每个人都在考虑这个问题。 Your original effort was almost there, but for the changing team names. 您最初的工作几乎就在那里,但要更改团队名称。

First, get the minimum year for each franchise ID (that's the subquery), then join that to the full table to get the rest of the columns for that entry (and that's the outer query). 首先,获取每个特许经营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

Results: 结果:

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

If I understand you correctly then ordinary ORDER BY would suffice: 如果我对您的理解正确,那么普通的ORDER BY就足够了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM