SQL组但仅显示特定列的最高记录

时间:2011-12-08 11:27:21

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

SELECT dbo.dPlaysPerDay.fkPlayerId AS Expr1, 
       sum(dbo.dPlaysPerDay.plays), 
       dbo.dWinners.pkWinnerId AS Expr2, 
       dbo.tbl_kissImages.kissImg AS Expr3, 
       dbo.dWinners.email AS Expr4, 
       dbo.tbl_kissImages.email
FROM dbo.dPlaysPerDay 
INNER JOIN dbo.dWinners ON dbo.dPlaysPerDay.fkPlayerId = dbo.dWinners.pkWinnerId 
INNER JOIN dbo.tbl_kissImages ON dbo.dWinners.email = dbo.tbl_kissImages.email
GROUP BY  dWinners.email, 
          dPlaysPerDay.fkPlayerId,
          dWinners.pkWinnerId, 
          tbl_kissImages.kissImg, 
          tbl_kissImages.email
ORDER BY dWinners.email

我有上面的sql,它返回一个这样的列表:

54938|14|54938|output/15124.png|156|eg@googlemail.com
54938|4|54938|output/15103.png|156|eg@googlemail.com
54939|10|54939|output/15104.png|156|eg2@googlemail.com
54939|9|54939|output/15124.png|156|eg2@googlemail.com
54940|10|54940|output/15114.png|156|eg3@googlemail.com
54940|9|54940|output/15109.png|156|eg3@googlemail.com

我需要通过电子邮件将此结果分组,为该电子邮件添加所有播放(第二列),并仅显示该电子邮件地址的第一条记录中的吻图。

感谢您提前提供任何帮助

1 个答案:

答案 0 :(得分:1)

以下假定您指的是给定电子邮件/播放器中最早的.png数量。

SELECT dbo.dPlaysPerDay.fkPlayerId AS Expr1, 
  sum(dbo.dPlaysPerDay.plays), 
  dbo.dWinners.pkWinnerId AS Expr2, 
  min(dbo.tbl_kissImages.kissImg) AS Expr3, 
  dbo.dWinners.email AS Expr4, 
  dbo.tbl_kissImages.email
FROM dbo.dPlaysPerDay 
INNER JOIN dbo.dWinners 
  ON dbo.dPlaysPerDay.fkPlayerId = dbo.dWinners.pkWinnerId 
INNER JOIN dbo.tbl_kissImages 
  ON dbo.dWinners.email = dbo.tbl_kissImages.email
GROUP BY  dWinners.email, 
          dPlaysPerDay.fkPlayerId,
          dWinners.pkWinnerId, 
          tbl_kissImages.email
ORDER BY dWinners.email