首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql 2使用groupby rollup设置sum

mysql 2使用groupby rollup设置sum
EN

Stack Overflow用户
提问于 2011-05-06 17:20:10
回答 1查看 319关注 0票数 0

我有一个问题

select user_id,sum(hours),date, task_id from table where used_id = 'x' and date >='' and date<= '' group by user_id, date, task_id with roll up

查询运行得很好。但我还需要找到更改了group by order的第二个和(小时)。

select user_id,sum(hours),date, task_id from table where used_id = 'x' group by user_id,task_id

(实际where条件要长得多。)

由于where条件几乎相同,是否有可能在单个查询中获得这两个和?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-06 18:30:09

代码语言:javascript
复制
SELECT * FROM (
  SELECT 1 AS list_id  
    , user_id
    , sum(hours) AS total_hours
    , `date`
    , task_id 
  FROM table WHERE used_id = 'x' AND `date` BETWEEN @thisdate AND @thatdate 
  GROUP BY user_id, `date`, task_id /*WITH ROLLUP*/
UNION ALL
  SELECT 2 AS list_id 
    , user_id
    , sum(hours) AS total_hours
    , `date`
    , task_id 
  FROM table 
  WHERE used_id = 'x' 
  GROUP BY user_id,task_id WITH ROLLUP ) q
/*ORDER BY q.list_id, q.user_id, q.`date`, q.task_id*/

根据您的需要,您应该只需要一个或两个with rollup

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5909314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档