查找该结果的相应列的最小值和值

时间:2014-02-10 19:01:37

标签: sql

我的SQL Server数据库中有一个用户数据表,我正在尝试汇总数据。基本上我需要一些min,max和sum值,并按一些列分组

以下是一个示例表:

Member ID | Name  | DateJoined | DateQuit  | PointsEarned | Address
00001     | Leyth | 1/1/2013   | 9/30/2013 | 57           | 123 FirstAddress Way
00002     | James | 2/1/2013   | 7/21/2013 | 34           | 4 street road
00001     | Leyth | 2/1/2013   | 10/15/2013| 32           | 456 LastAddress Way
00003     | Eric  | 2/23/2013  | 4/14/2013 | 15           | 5 street road

我希望汇总表能够显示如下结果:

Member ID | Name  | DateJoined | DateQuit  | PointsEarned | Address
00001     | Leyth | 1/1/2013   | 10/15/2013 | 89           | 123 FirstAddress Way
00002     | James | 2/1/2013   | 7/21/2013  | 34           | 4 street road
00003     | Eric  | 2/23/2013  | 4/14/2013  | 15           | 5 street road

到目前为止,这是我的查询:

选择MemberID,Name,Min(DateJoined),Max(DateQuit),SUM(PointsEarned),Min(Address) 从表格 Group By MemberID

这次Min(地址)工作,它检索与最早的DateJoined对应的地址。但是,如果我们交换原始表中的两个地址,我们将检索123 FirstAddress Way,这与加入的1/1/2013日期不对应。

提前致谢

3 个答案:

答案 0 :(得分:1)

对于几乎所有的东西,你都可以使用一个简单的groupby,但是因为你需要“与最小datejoined所在的行相同的地址”是一个小问题,你可以通过几种方式解决它,一个是查询的子查询每次地址

SELECT
   X.*, 
   (select Address 
    from #tmp t2 
    where t2.MemberID = X.memberID and 
    t2.DateJoined = (select MIN(DateJoined) 
                     from #tmp t3 
                     where t3.memberID = X.MemberID)) 
FROM
   (select MemberID, 
           Name,  
           MIN(DateJoined) as DateJoined, 
           MAX(DateQuit) as DateQuit, 
           SUM(PointsEarned) as PointEarned
from #tmp t1
group by MemberID,Name
) AS X

` 或者其他是带有Join的子查询

SELECT
   X.*, 
   J.Address 
FROM
(select 
         MemberID, 
         Name,  
         MIN(DateJoined) as DateJoined, 
         MAX(DateQuit) as DateQuit, 
         SUM(PointsEarned) as PointEarned
from #tmp t1
group by MemberID,Name
) AS X
JOIN #tmp J ON J.MemberID = X.MemberID AND J.DateJoined = X.DateJoined

答案 1 :(得分:0)

您可以根据日期rank行,并选择最小的行:

SELECT  t.member_id,
        name,
        date_joined,
        date_quit,
        points_earned
        address AS address
FROM   (SELECT member_id
               name, 
               MIN (date_joined) AS date_joined,
               MAX (date_quit) AS date_quit, 
               SUM (points_earned) AS points_earned,
        FROM   my_table
        GROUP BY member_id, name) t
JOIN   (SELECT member_id, 
               address, 
               RANK() OVER (PARTITION BY member_id ORDER BY date_joined) AS rk
        FROM   my_table) addr ON addr.member_id = t.member_id AND rk = 1

答案 2 :(得分:0)

SELECT DISTINCT st.memberid, st.name, m1.datejoined, m2.datequit, SUM(st.pointsearned), m1.Address
from SAMPLEtable st
LEFT JOIN ( SELECT  memberid
                  , name
                  , MIN(datejoined)
                  , datequit
            FROM    sampletable
          ) m1 ON st.memberid = m1.memberid
LEFT JOIN ( SELECT  memberid
                  , name
                  , datejoined
                  , MAX(datequit)
            FROM    sampletable
          ) m2 ON m1.memberid = m2.memberid