【问题标题】:How To get the First Row Form SQL Group Query?如何获取第一行形式的 SQL 组查询?
【发布时间】:2013-09-11 15:34:25
【问题描述】:

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

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

我的桌子是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

请任何人帮助我

【问题讨论】:

  • 您的架构将列标识为“日期”,但您的查询使用 tr.TransactionDate。如果在您的代码中全部匹配,显然不是问题。
  • 您意识到每个用户ID 可以有多个StoreID?你想保留哪一个?

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


【解决方案1】:

您可以使用 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

【讨论】:

    【解决方案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】:

      你可以使用子查询

      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
      

      【讨论】:

        【解决方案4】:
        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
        

        【讨论】:

          【解决方案5】:

          您可以使用子查询来做到这一点。基本原理是子查询标识每一个的最小日期,包装查询选择匹配用户和最小日期的行,也可以返回商店ID。

          看起来像这样:

          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 中自行查看。

          【讨论】:

            猜你喜欢
            • 2016-03-14
            • 2021-10-07
            • 2020-04-24
            • 2010-12-25
            • 2018-01-29
            • 1970-01-01
            • 2021-08-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多