如何从SQL组查询获取第一行?

时间:2013-09-11 15:34:25

标签: sql sql-server sql-server-2008 sql-server-2005

我在编写查询时遇到问题。

我想选择分组的每组行的第一行

我的表格为Transactions

userID | Date     | StoreID 
---------------------------
     1 | 8-9-2013 | 10
     1 | 9-9-2013 | 10
     1 | 10-9-2013| 20
     2 | 7-9-2013 | 30
     2 | 8-9-2013 | 10
     2 | 9-9-2013 | 20
     1 | 11-9-2013| 10
     2 | 10-9-2013| 20

我试着这个SQL语句:

Select 
    tr.userID , Min(tr.TransactionDate) FirstDate
From 
    Transactions tr 
Group By 
    tr.userID 

我得到了这个输出:

userID | Date     
------------------
     1 | 8-9-2013 
     2 | 7-9-2013

但我在每次首次交易中都需要Store ID

我需要它像那样

 userID | Date    |  StoreID
-------------------------
     1 | 8-9-2013 |  10
     2 | 7-9-2013 |  30

请任何人帮助我

5 个答案:

答案 0 :(得分:4)

您可以使用Row_Number()。

select UserId, Date, StoreId from  (select row_number() over(partition
by UserId order by date) as RowNumber,   UserId, Date, StoreId from
Transactions  ) as View1 where  RowNumber = 1

http://sqlfiddle.com/#!6/e536a/7

答案 1 :(得分:2)

您可以使用子查询

SELECT  TR1.userID
        ,TR1.TransactionDate
        ,TR1.StoreID
FROM    Transactions tr1
INNER JOIN
        (
        Select 
            tr.userID 
            ,Min(tr.TransactionDate) AS FirstDate
        From 
            Transactions tr 
        Group By 
            tr.userID 
        ) SQ
ON      TR1.userID = SQ.userID 
AND     TR1.TransactionDate = SQ.FirstDate

答案 2 :(得分:2)

SQL Fiddle

MS SQL Server 2008架构设置

CREATE TABLE Transactions
    ([userID] int, [Date] datetime, [StoreID] int)
;

INSERT INTO Transactions
    ([userID], [Date], [StoreID])
VALUES
    (1, '2013-08-09 00:00:00', 10),
    (1, '2013-09-09 00:00:00', 10),
    (1, '2013-10-09 00:00:00', 20),
    (2, '2013-07-09 00:00:00', 30),
    (2, '2013-08-09 00:00:00', 10),
    (2, '2013-09-09 00:00:00', 20),
    (1, '2013-11-09 00:00:00', 10),
    (2, '2013-10-09 00:00:00', 20)
;

查询1

SELECT
    tr.userID , Min(tr.Date) FirstDate , tr2.storeid
FROM
    Transactions tr
inner join Transactions tr2 on tr.userid = tr2.userid and 
                              tr2.date = (select top 1 date 
                                          from transactions t 
                                          where t.userid = tr2.userid
                                          order by date asc)
GROUP BY
    tr.userID, tr2.storeid

<强> Results

| USERID |                     FIRSTDATE | STOREID |
|--------|-------------------------------|---------|
|      1 | August, 09 2013 00:00:00+0000 |      10 |
|      2 |   July, 09 2013 00:00:00+0000 |      30 |

答案 3 :(得分:0)

with user_cte (userid,date)
as(Select tr.userID , Min(tr.TransactionDate) FirstDate
From  Transactions tr 
Group By tr.userID 
)

select b.userid,b.date,a.storeId from Transactions a join user_cte b on a.userID=b.userId and a.Date=b.Date

答案 4 :(得分:0)

您可以使用子查询执行此操作。基本原则是子查询标识每个查询的最小日期,包装查询选择与用户和最小日期匹配的行,也能够返回商店标识。

它看起来像这样:

SELECT
    t.UserID,
    t.Date,
    t.StoreId
FROM
    Transactions t JOIN
    (
        SELECT 
            tr.userID , Min(tr.Date) AS FirstDate
        FROM
            Transactions tr
        GROUP BY 
            tr.userID 
    ) u ON t.UserId = u.UserId AND t.Date = u.FirstDate

您可以在SqlFiddle here中自行查看。