SQL group by select

时间:2013-01-01 14:12:56

标签: sql sql-server tsql

我正在使用SQL Server,我有一个包含以下列的表:

SessionId | Date | first name | last name 

我想group by sessionId,然后获取最大日期的行。

例如:

xxx | 21/12/2012 | f1 | l1
xxx | 20/12/2012 | f2 | l2
yyy | 21/12/2012 | f3 | l3
yyy | 20/12/2012 | f4 | l4

我想获得以下行:

xxx | 21/12/2012 | f1 | l1
yyy | 21/12/2012 | f3 | l3

谢谢

5 个答案:

答案 0 :(得分:5)

试试这个:

WITH MAXSessions
AS
(
  SELECT 
    *,
    ROW_NUMBER() OVER(PARTITION BY SessionID ORDER BY Date DESC) rownum
  FROM Sessions
)
SELECT
  SessionId,
  Date,
  firstname,
  lastname 
FROM MAXSessions
WHERE rownum = 1;

SELECT 
  s.SessionId,
  s.Date,
  s.firstname,
  s.lastname 
FROM Sessions s
INNER JOIN
(
   SELECT SessionID, MAX(Date) LatestDate
   FROM sessions
   GROUP BY SessionID
) MAxs  ON maxs.SessionID  = s.SessionID
       AND maxs.LatestDate = s.Date;

更新:要获取会话次数,您可以执行以下操作:

SELECT 
  s.SessionId,
  s.Date,
  s.firstname,
  s.lastname,
  maxs.SessionsCount
FROM Sessions s
INNER JOIN
(
   SELECT SessionID, COUNT(SessionID), SessionsCount, MAX(Date) LatestDate
   FROM sessions
   GROUP BY SessionID
) MAxs  ON maxs.SessionID  = s.SessionID
       AND maxs.LatestDate = s.Date;

答案 1 :(得分:3)

以下是Mahmoud的答案 - SQL Fiddle

的实例

以下是相同的,只使用子查询:

SELECT a.*
FROM 
    #Table a
    INNER JOIN
        (
        SELECT 
            SessionID,
            [mx] = MAX([Date])      
        FROM #Table
        GROUP BY SessionID                  
        ) b
        ON
            a.[SessionId ] = b.SessionID AND
            a.[Date] = b.mx;

HERE IS THE SQL FIDDLE FOR THE ABOVE SUB-QUERY VERSION

你也可以使用EXISTS - 这是我的最爱:

SELECT 
     a.*,
     c.CNT 
FROM 
     #Table a
     INNER JOIN 
        ( --to return a count of sessionIds
        SELECT 
           SessionID,
           [CNT] = COUNT(*)     
        FROM #Table
        GROUP BY SessionID                  
        ) c
          ON a.SessionID = c.SessionID
WHERE 
    EXISTS
        (
        SELECT 1
        FROM #Table b
        WHERE
            a.[SessionId] = b.SessionID AND
            a.[Date] > b.[Date] 
        )

HERE IS THE SQL FIDDLE WITH THE ADDITIONAL COUNT INCLUDED

答案 2 :(得分:0)

一些选项,一种是使用按日期对行进行排名的CTE

已修改为包含会话计数

WITH Sessions AS (
    SELECT SessionId, [Date], FirstName, LastName,
    ROW_NUMBER() OVER (PARTITION BY SessionId ORDER BY [Date] DESC) AS Ord
    FROM YourTable
)
SELECT S.SessionId, S.Date, S.FirstName, S.LastName, X.SessionCount
FROM Sessions S
INNER JOIN (
    SELECT SessionId, COUNT(*) AS SessionCount
    FROM Sessions
    GROUP BY SessionId 
) X ON X.SessionId = S.SessionId  
WHERE S.Ord = 1
在这种情况下,

Ranking Functions是您的朋友,您希望获取满足某些“有序”条件的整行,例如最大日期。

答案 3 :(得分:0)

我们走了,这就是

select a.date1,a.first_name,a.last_name
from(select row_number() 
over(partition by SessionId order by SessionId) rnk,date1,first_name,last_name
from table1) a where a.rnk=1

SQL_FIDDLE_DEMO

答案 4 :(得分:0)

    SELECT b.sessionid, 
       b.date, 
       b.fristname,         b.lastname 
FROM   (SELECT t2.sessionid, 
               Max(t2.date) AS Date 
        FROM   temp1 t2 
        GROUP  BY t2.sessionid) a, 
       temp1 b 
WHERE  a.sessionid = b.sessionid 
       AND a.date = b.date

找到结果的最佳方式是