MYSQL - 返回GROUP BY

时间:2016-07-07 21:36:59

标签: mysql sql datetime group-by

我已经阅读了很多关于非常类似问题的问题和答案,主要问题是他们使用LIMIT作为返回最近记录的解决方案查询,因为我需要几个结果。

逻辑如下:

  • 当记录值生效时,每条记录都有effective_date
  • 仅撤回今天effective_date已成为或已经过去的记录
  • 在我撤回的记录中,根据&{39; date_entered'给我最新记录。该记录

到目前为止,我已经能够使用此查询完成除最后一点逻辑之外的所有操作。我无法在ORDER BY之后使用GROUP BY,因为结果已经分组,因此我无法使用MAX()日期。我需要在WHERE语句中执行逻辑。

SELECT
    aos_products.`name`,
    cac_customize_agent_comp_cstm.commission_percentage_c,
    cac_customize_agent_comp.date_entered
FROM
    wn_writing_number
LEFT OUTER JOIN wn_writing_number_cac_customize_agent_comp_1_c ON wn_writing_number.id = wn_writing_number_cac_customize_agent_comp_1_c.wn_writing946b_number_ida
LEFT OUTER JOIN cac_customize_agent_comp_cstm ON wn_writing_number_cac_customize_agent_comp_1_c.wn_writing3148nt_comp_idb = cac_customize_agent_comp_cstm.id_c
LEFT OUTER JOIN cac_customize_agent_comp ON cac_customize_agent_comp_cstm.id_c = cac_customize_agent_comp.id
LEFT OUTER JOIN aos_products_cac_customize_agent_comp_1_c ON cac_customize_agent_comp_cstm.id_c = aos_products_cac_customize_agent_comp_1_c.aos_produca2b8nt_comp_idb
LEFT OUTER JOIN aos_products ON aos_products_cac_customize_agent_comp_1_c.aos_products_cac_customize_agent_comp_1aos_products_ida = aos_products.id
WHERE
    wn_writing_number.id = 'b556d816-f2e6-b78f-c776-576c0542585c'
AND wn_writing_number_cac_customize_agent_comp_1_c.deleted = '0'
AND cac_customize_agent_comp_cstm.effective_date_c <= CURDATE()
GROUP BY aos_products.`name`

没有GROUP BY,它返回:

     (varchar)     (varchar)                 (datetime)
    +---------+-------------------------+----------------------+
    | name    | commission_percentage_c |     date_entered     |
    +---------+-------------------------+----------------------+
    | Val 1   | 15.25                   | 2016-07-06 23:57:28  |
    +---------+-------------------------+----------------------+
    | Val 1   | 15.75                   | 2016-07-07 00:03:03  |
    +---------+-------------------------+----------------------+
    | Val 1   | 16                      | 2016-07-07 00:31:08  |
    +---------+-------------------------+----------------------+
    | Val 2   | 14.75                   | 2016-07-07 15:04:02  |
    +---------+-------------------------+----------------------+

WITH GROUP BY返回

    +---------+-------------------------+----------------------+
    | name    | commission_percentage_c |     date_entered     |
    +---------+-------------------------+----------------------+
    | Val 2   | 14.75                   | 2016-07-07 15:04:02  |
    +---------+-------------------------+----------------------+
    | Val 1   | 15.25                   | 2016-07-06 23:57:28  |
    +---------+-------------------------+----------------------+

我想回来的是:

    +---------+-------------------------+----------------------+
    | name    | commission_percentage_c |     date_entered     |
    +---------+-------------------------+----------------------+
    | Val 2   | 14.75                   | 2016-07-07 15:04:02  |
    +---------+-------------------------+----------------------+
    | Val 1   | 16                      | 2016-07-07 00:31:08  |
    +---------+-------------------------+----------------------+

因为我需要最新的VAL 1记录,比如

WHERE the datediff BETWEEN NOW() and date_entered = the smallest amount of time compared with other records

2 个答案:

答案 0 :(得分:3)

试试这个

SELECT *
FROM
    (SELECT
        aos_products.`name` as name,
        cac_customize_agent_comp_cstm.commission_percentage_c as commission_percentage_c,
        cac_customize_agent_comp.date_entered as date_entered
    FROM
        wn_writing_number
    LEFT OUTER JOIN wn_writing_number_cac_customize_agent_comp_1_c ON wn_writing_number.id = wn_writing_number_cac_customize_agent_comp_1_c.wn_writing946b_number_ida
    LEFT OUTER JOIN cac_customize_agent_comp_cstm ON wn_writing_number_cac_customize_agent_comp_1_c.wn_writing3148nt_comp_idb = cac_customize_agent_comp_cstm.id_c
    LEFT OUTER JOIN cac_customize_agent_comp ON cac_customize_agent_comp_cstm.id_c = cac_customize_agent_comp.id
    LEFT OUTER JOIN aos_products_cac_customize_agent_comp_1_c ON cac_customize_agent_comp_cstm.id_c = aos_products_cac_customize_agent_comp_1_c.aos_produca2b8nt_comp_idb
    LEFT OUTER JOIN aos_products ON aos_products_cac_customize_agent_comp_1_c.aos_products_cac_customize_agent_comp_1aos_products_ida = aos_products.id
    WHERE
        wn_writing_number.id = 'b556d816-f2e6-b78f-c776-576c0542585c'
    AND wn_writing_number_cac_customize_agent_comp_1_c.deleted = '0'
    AND cac_customize_agent_comp_cstm.effective_date_c <= CURDATE()
    ORDER BY cac_customize_agent_comp.date_entered DESC) t
GROUP BY t.name

这样做的想法是使用date_enteredgroup by 对记录进行排序。从this post开始,groupby将使用与正常SELECT相同顺序的第一个选择。

答案 1 :(得分:0)

使用max()获取最新日期时间

SELECT
aos_products.`name`,
cac_customize_agent_comp_cstm.commission_percentage_c,
max(cac_customize_agent_comp.date_entered) as  date_entered     
FROM
    wn_writing_number
LEFT OUTER JOIN wn_writing_number_cac_customize_agent_comp_1_c ON     wn_writing_number.id = wn_writing_number_cac_customize_agent_comp_1_c.wn_writing946b_number_ida
LEFT OUTER JOIN cac_customize_agent_comp_cstm ON wn_writing_number_cac_customize_agent_comp_1_c.wn_writing3148nt_comp_idb = cac_customize_agent_comp_cstm.id_c
LEFT OUTER JOIN cac_customize_agent_comp ON cac_customize_agent_comp_cstm.id_c = cac_customize_agent_comp.id
LEFT OUTER JOIN aos_products_cac_customize_agent_comp_1_c ON cac_customize_agent_comp_cstm.id_c = aos_products_cac_customize_agent_comp_1_c.aos_produca2b8nt_comp_idb
LEFT OUTER JOIN aos_products ON aos_products_cac_customize_agent_comp_1_c.aos_products_cac_customize_agent_comp_1aos_products_ida = aos_products.id
WHERE
    wn_writing_number.id = 'b556d816-f2e6-b78f-c776-576c0542585c'
AND wn_writing_number_cac_customize_agent_comp_1_c.deleted = '0'
AND cac_customize_agent_comp_cstm.effective_date_c <= CURDATE()
GROUP BY aos_products.`name`