MySQL在每个组中选择第一个,第二个和最后一个值的最佳方法

时间:2018-06-15 04:39:27

标签: mysql

我正在尝试使用MySQL来获取表中每个组中的第一个,第二个和最后一个值。我的数据行是这样的:

userID    purchaseTime
----------------------
1         2018-01-01
1         2018-01-02
1         2018-01-03
1         2018-01-04
2         2018-02-01
2         2018-02-02
3         2018-03-01

预期结果将是:

userID    first         second        last
------------------------------------------------
1         2018-01-01    2018-01-02    2018-01-04
2         2018-02-01    2018-02-02    2018-02-02
3         2018-03-01    null          2018-03-01

在谷歌搜索半天后,我只能找出执行以下两个查询的愚蠢方式,然后通过我的服务器端代码合并结果:

//get 1st, 2nd values 
SELECT userID, purchaseTime
FROM   purchaseLog t1
WHERE 
    (
        SELECT  COUNT(*) 
        FROM    purchaseLog t2
        WHERE t2.userID = t1.userID AND 
              t2.purchaseTime<= t1.purchaseTime
    ) <= 2 order by t1.userID , t1.purchaseTime;

//get last value
SELECT max(purchaseTime) FROM purchaseTime GROUP BY userID 

我很确定必须有更优雅的方式才能一次性获得结果。任何人都可以帮助我达到我的要求吗?谢谢大家!

2 个答案:

答案 0 :(得分:0)

以下代码未经测试但应该给您一个好主意:

SELECT
    t1.userID,
    t1.purchaseTime AS first,
    t2.purchaseTime AS `second`,
    t4.purchaseTime AS last
FROM purchaseLog t1
LEFT JOIN purchaseLog t0 ON t1.userID = t0.userID AND t0.purchaseTime < t1.purchaseTime
LEFT JOIN purchaseLog t2 ON t1.userID = t2.userID AND t1.purchaseTime < t2.purchaseTime
LEFT JOIN purchaseLog t3 ON t1.userID = t3.userID AND t1.purchaseTime < t3.purchaseTime
                        AND t3.purchaseTime < t2.purchaseTime
JOIN purchaseLog t4 ON t1.userID = t4.userID AND t1.purchaseTime <= t4.purchaseTime
LEFT JOIN purchaseLog t5 ON t1.userID = t5.userID AND t4.purchaseTime < t5.purchaseTime
WHERE t0.purchaseTime IS NULL AND t3.purchaseTime IS NULL AND t5.purchaseTime IS NULL

让我一步一步地解决这个问题:

首先,我得到了同一个userID没有早期行的所有行:

SELECT
    t1.userID,
    t1.purchaseTime AS first
FROM purchaseLog t1
LEFT JOIN purchaseLog t0 ON t1.userID = t0.userID AND t0.purchaseTime < t1.purchaseTime
WHERE t0.purchaseTime IS NULL

接下来,我获得的所有行的purchaseTime大于第一个purchaseTime,其中没有任何行在两者之间有一个purchaseTime:

SELECT
    t1.userID,
    t2.purchaseTime AS `second`
FROM purchaseLog t1
LEFT JOIN purchaseLog t2 ON t1.userID = t2.userID AND t1.purchaseTime < t2.purchaseTime
LEFT JOIN purchaseLog t3 ON t1.userID = t3.userID AND t1.purchaseTime < t3.purchaseTime
                        AND t3.purchaseTime < t2.purchaseTime
WHERE t3.purchaseTime IS NULL

最后,我得到的buyTime大于或等于第一个没有更大buyTime存在的行:

SELECT
    t1.userID,
    t4.purchaseTime AS last
FROM purchaseLog t1
JOIN purchaseLog t4 ON t1.userID = t4.userID AND t1.purchaseTime <= t4.purchaseTime
LEFT JOIN purchaseLog t5 ON t1.userID = t5.userID AND t4.purchaseTime < t5.purchaseTime
WHERE t5.purchaseTime IS NULL

将它们全部合并到一个查询中以获得上述答案。

答案 1 :(得分:0)

嗯,你可以这样做:

选择第一个,第二个和最后一个的个别陈述,然后将它们连接在一起:

SELECT a.userID, a.purchaseTime 
    FROM fsl a WHERE a.purchaseTime = 
    (SELECT MAX(b.purchaseTime) FROM fsl b WHERE b.userID = a.userID)

SELECT a.userID, a.purchaseTime 
    FROM fsl a WHERE a.purchaseTime = 
    (SELECT MAX(c.purchaseTime) FROM fsl c 
        WHERE c.purchaseTime < (SELECT MAX(b.purchaseTime) 
            FROM fsl b WHERE b.userID = a.userID));

SELECT a.userID, a.purchaseTime 
    FROM fsl a WHERE a.purchaseTime = 
    (SELECT MIN(b.purchaseTime) 
        FROM fsl b WHERE b.userID = a.userID)

使用JOINS将其拼凑在一起:

SELECT fst.userID as userID, fst.purchaseTime as first, snd.purchaseTime as snd, trd.purchaseTime as last FROM
    (SELECT a.userID, a.purchaseTime 
    FROM fsl a 
    WHERE a.purchaseTime = 
        (SELECT MAX(b.purchaseTime) FROM fsl b WHERE b.userID = a.userID)) fst
    JOIN (SELECT a.userID, a.purchaseTime FROM fsl a 
            WHERE a.purchaseTime = (SELECT MAX(c.purchaseTime) 
            FROM fsl c WHERE c.purchaseTime < (SELECT MAX(b.purchaseTime) 
            FROM fsl b WHERE b.userID = a.userID))) snd
    ON fst.userID = snd.userID
    JOIN (SELECT a.userID, a.purchaseTime 
            FROM fsl a WHERE a.purchaseTime = 
            (SELECT MIN(b.purchaseTime) FROM fsl b WHERE b.userID = a.userID)) trd
    ON trd.userID = snd.userID;

但是,我无法保证这对于任何生产用途都足够快。