自我加入以获得第一个记录并记录在月的最后一天

时间:2016-01-30 20:19:00

标签: mysql aggregate-functions

我有一张看起来像这样的桌子。该表是通过每天ping系统中的每个帐户来生成的,以记录“mrr”。量。 ' mrr'如果帐户未付款,则帐户为0。它会在帐户开始付款时记录一个值。如果帐户取消,则记录的mrr将返回0.

+-----------+------------+------------+-----+
| paylog_id | account_id |    date    | mrr |
+-----------+------------+------------+-----+
|      1001 |         99 | 2016-01-15 |   0 |
|      1002 |         99 | 2016-01-16 |   0 |
|      1003 |         99 | 2016-01-17 |   0 |
|      1004 |         99 | 2016-01-18 |   0 |
|      1005 |         99 | 2016-01-19 |   0 |
|      1006 |         99 | 2016-01-20 |  25 |
|      1007 |         99 | 2016-01-21 |  25 |
|      1008 |         99 | 2016-01-22 |  25 |
|      1009 |         99 | 2016-01-23 |  25 |
|      1010 |         99 | 2016-01-24 |  25 |
|      1011 |         99 | 2016-01-25 |  25 |
|      1012 |         99 | 2016-01-26 |  25 |
|      1013 |         99 | 2016-01-27 |  25 |
|      1014 |         99 | 2016-01-28 |   0 |
|      1015 |         99 | 2016-01-29 |   0 |
|      1016 |         99 | 2016-01-30 |   0 |
|      1017 |         99 | 2016-01-31 |   0 |
|      1018 |         99 | 2016-02-01 |   0 |
|      1019 |         79 | 2016-02-23 | 255 |
|      1020 |         79 | 2016-02-24 | 255 |
|      1021 |         79 | 2016-02-25 | 255 |
|      1022 |         79 | 2016-02-26 | 255 |
|      1023 |         79 | 2016-02-27 | 255 |
|      1024 |         79 | 2016-02-28 |  50 |
|      1025 |         79 | 2016-02-29 |  25 |
|      1026 |         79 | 2016-03-01 |  25 |
|      1027 |         79 | 2016-03-02 |  27 |
+-----------+------------+------------+-----+

我需要知道两条信息 -

  1. 帐户开始付款的第一个日期的金额是多少?
  2. 帐户开始付款的当月最后一天的金额是多少?
  3. 以上示例中的结果应为:

    +-----------+------------+------------+-----+------------+-------------+------------+------+
    | paylog_id | account_id |    date    | mrr | paylog_id1 | account_id1 |   date1    | mrr1 |
    +-----------+------------+------------+-----+------------+-------------+------------+------+
    |      1006 |         99 | 2016-01-20 |  25 |       1017 |          99 | 2016-01-31 |    0 |
    |      1018 |         79 | 2016-02-23 | 255 |       1024 |          79 | 2016-02-29 |   25 |
    +-----------+------------+------------+-----+------------+-------------+------------+------+
    

    我知道我可以使用此查询获得第一个付款日期:

    SELECT
     a.paylog_id,
     a.account_id,
     a.date,
     a.mrr
    FROM
     ds_paylogs a
    INNER JOIN (
     SELECT
      account_id,
      min(date) mdate, mrr
     FROM
      ds_paylogs b
    where b.mrr >0
     GROUP BY
      account_id
    ) b ON a.account_id = b.account_id
    AND a.date = b.mdate
    

    结果:

    +------+----+----------+-----+
    | 1006 | 99 | 20-01-16 | 25  |
    +------+----+----------+-----+
    | 1018 | 79 | 23-02-16 | 255 |
    +------+----+----------+-----+
    

    另外,我可以使用以下方式在第一个付款日期获得当月的最后一天:

    SELECT
     a.paylog_id,
     a.account_id,
     a.date,
     a.mrr
    FROM
     ds_paylogs a
    INNER JOIN (
     SELECT
      account_id,
      LAST_DAY(min(date)) mdate, mrr
     FROM
      ds_paylogs b
    where b.mrr >0
     GROUP BY
      account_id
    ) b ON a.account_id = b.account_id
    AND a.date = b.mdate
    

    结果:

    +------+----+----------+----+
    | 1017 | 99 | 31-01-16 | 0  |
    +------+----+----------+----+
    | 1024 | 79 | 29-02-16 | 25 |
    +------+----+----------+----+
    

    我想将这两个结果合并为一个视图。我被困在怎么办?

    非常感谢有关如何操作或如何简化查询的任何建议。

    更新:执行以下操作可以获得我所需要的内容。但是,似乎应该有一个更优雅的解决方案。

    SELECT
        *
    FROM
        (
            SELECT
                a.paylog_id,
                a.account_id,
                a.date,
                a.mrr
            FROM
                ds_paylogs a
            INNER JOIN (
                SELECT
                    account_id,
                    min(date) mdate,
                    mrr
                FROM
                    ds_paylogs b
                WHERE
                    b.mrr > 0
                GROUP BY
                    account_id
            ) b ON a.account_id = b.account_id
            AND a.date = b.mdate
        ) q
    INNER JOIN (
        SELECT
            a.paylog_id,
            a.account_id,
            a.date,
            a.mrr
        FROM
            ds_paylogs a
        INNER JOIN (
            SELECT
                account_id,
                LAST_DAY(min(date)) mdate,
                mrr
            FROM
                ds_paylogs b
            WHERE
                b.mrr > 0
            GROUP BY
                account_id
        ) b ON a.account_id = b.account_id
        AND a.date = b.mdate
    ) r ON q.account_id = r.account_id
    

0 个答案:

没有答案