0
CREATE PROCEDURE `get_timeline`
(
    IN  in_unit VARCHAR(255)
)
READS SQL DATA
BEGIN
    SELECT
        CASE in_unit
            WHEN 'MICROSECOND' THEN DATE_FORMAT(`ts`, CONCAT("%Y-%m-%d %H:",LPAD(SECOND(`ts`) DIV 6,2,0),":%s"))
            WHEN 'SECOND'      THEN DATE_FORMAT(`ts`, CONCAT("%Y-%m-%d %H:",LPAD(SECOND(`ts`) DIV 6,2,0),":%s"))
            WHEN 'MINUTE'      THEN DATE_FORMAT(`ts`, CONCAT("%Y-%m-%d %H:",LPAD(SECOND(`ts`) DIV 6,2,0),":%s"))
            WHEN 'HOUR'        THEN DATE_FORMAT(`ts`, CONCAT("%Y-%m-%d %H:",LPAD(SECOND(`ts`) DIV 6,2,0),":%s"))
            WHEN 'DAY'         THEN DATE_FORMAT(`ts`, CONCAT("%Y-%m-%d %H:",LPAD(SECOND(`ts`) DIV 6,2,0),":%s"))
            WHEN 'WEEK'        THEN DATE_FORMAT(`ts`, CONCAT("%Y-%m-%d %H:",LPAD(MINUTE(`ts`) DIV 6,2,0),":00"))
            WHEN 'MONTH'       THEN DATE_FORMAT(`ts`, CONCAT("%Y-%m-%d ",LPAD(HOUR(`ts`) DIV 6,2,0),":00:00"))
            WHEN 'YEAR'        THEN DATE_FORMAT(`ts`, "%Y-%m-%d %0:00:00")
        END                 AS `time`,
        COUNT(*)            AS `count`
    FROM
        `session`
    GROUP BY
        CASE in_unit
            WHEN
                'MICROSECOND'
            THEN
            (
                YEAR(`ts`),
                MONTH(`ts`),
                WEEK(`ts`),
                DAY(`ts`),
                HOUR(`ts`),
                MINUTE(`ts`),
                SECOND(`ts`),
                MICROSECOND(`ts`) DIV 100
            )
            WHEN
                'SECOND'
            THEN
            (
                YEAR(`ts`),
                MONTH(`ts`),
                WEEK(`ts`),
                DAY(`ts`),
                HOUR(`ts`),
                MINUTE(`ts`),
                SECOND(`ts`),
                MICROSECOND(`ts`) DIV 100
            )
            WHEN
                'MINUTE'
            THEN
            (
                YEAR(`ts`),
                MONTH(`ts`),
                WEEK(`ts`),
                DAY(`ts`),
                HOUR(`ts`),
                MINUTE(`ts`),
                SECOND(`ts`),
                MICROSECOND(`ts`) DIV 100
            )
            WHEN
                'HOUR'
            THEN
            (
                YEAR(`ts`),
                MONTH(`ts`),
                WEEK(`ts`),
                DAY(`ts`),
                HOUR(`ts`),
                MINUTE(`ts`),
                SECOND(`ts`) DIV 6
            )
            WHEN
                'DAY'
            THEN
            (
                YEAR(`ts`),
                MONTH(`ts`),
                WEEK(`ts`),
                DAY(`ts`),
                HOUR(`ts`),
                MINUTE(`ts`) DIV 6
            )
            WHEN
                'WEEK'
            THEN
            (
                YEAR(`ts`),
                MONTH(`ts`),
                WEEK(`ts`),
                DAY(`ts`),
                HOUR(`ts`)
            )
            WHEN
                'MONTH'
            THEN
            (
                YEAR(`ts`),
                MONTH(`ts`),
                WEEK(`ts`),
                DAY(`ts`)
            )
            WHEN
                'YEAR'
            THEN
            (
                YEAR(`ts`),
                MONTH(`ts`),
                WEEK(`ts`)
            )
        END
;
END
//

我试图在 group by 子句中使用 case 语句来分组 8、7、6、5、4 或 3 列,具体取决于传递给存储过程的单元,但我收到“错误代码:1241 . 操作数应包含 1 列”。我可以做些什么来实现这个结果,而不必通过连接使用准备好的查询?(我正在使用 MySQL)

4

1 回答 1

1

你想做这样的事情:

GROUP BY YEAR(ts),
         (case when in_unit in ('Month', . . .) then MONTH(`ts`) end),
         (case when in_unit in ('Week', . . .) then WEEK(`ts`) end),
         . . .

单位名称和变量选择之间的关系group by对我来说是完全清楚的。但基本上,您只需要为每个可能的变量添加一个条件。

于 2013-11-07T22:38:08.760 回答