存储过程的Group By子句如何具有不同的列数?

时间:2013-11-07 22:32:45

标签: mysql sql stored-procedures

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)

1 个答案:

答案 0 :(得分: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变量的选择之间的关系对我来说是完全清楚的。但基本上,您只需要为每个可能的变量添加一个条件。