繁体   English   中英

使用函数时在 Sequelize 中分组

[英]Group by in Sequelize when using a function

我想按函数返回的值分组,如下所示:

    let noticesOpened = NoticeOpened.aggregate(
        "id",
        "COUNT", {
            plain: false,
            where: noticeWhere,
            group: [sequelize.fn("date_trunc", "day", sequelize.col("created_at"))],
            order: [
                ["created_at", "ASC"]
            ],
        });
    return noticesOpened;

当我尝试这个查询时,我得到:

SequelizeDatabaseError: column "notice_opened_tbl.created_at" must appear in the GROUP BY clause or be used in an aggregate function

有没有办法对返回的分组值进行升序?

您需要根据 group 子句中的函数结果进行排序,例如:

    let noticesOpened = NoticeOpened.aggregate(
        "id",
        "COUNT", {
            plain: false,
            where: noticeWhere,
            group: [sequelize.fn("date_trunc", "day", sequelize.col("created_at"))],
            order: [
                [sequelize.fn("date_trunc", "day", sequelize.col("created_at")), "ASC"]
            ],
        });
    return noticesOpened;

我想记住你可以使用 sequelize 别名来使这更容易,但没有语法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM