我有一个表格,附有3列X,日期和Qtr。
我需要获得附件中提到的输出(仅8个季度的数据)
create table mQtr
( X varchar(5), XDate date, Qtr int);
insert into mQtr
values ('X1', '2011-11-01', 4), ('X2', '2011-10-01', 4),
('X3', '2011-09-01', 3), ('X4', '2011-08-01', 3),
('X5', '2011-07-01', 3), ('X6', '2011-06-01', 2),
('X7','2011-05-01',2), ('X8','2011-04-01',2),
('X9','2011-03-01',1), ('X10','2011-02-01',1),
('X11','2011-01-01',1), ('X12','2010-12-01',4),
('X13','2010-11-01',4), ('X14','2010-10-01',4),
('X15','2010-09-01',3), ('X16','2010-08-01',3),
('X17','2010-07-01',3), ('X18','2010-06-01',2),
('X19','2010-05-01',2), ('X20','2010-04-01',2),
('X21','2010-03-01',1), ('X22','2010-02-01',1),
('X23','2010-01-01',1), ('X24','2009-12-01',4)
我期望基于Qtr值的以下结果集,我想生成X值的总和。输出应该是这样的:
Qtr mOutput
----------------
1 x1+x2
2 x3+x4+x5
3 x6+x7+x8
4 x9+x10+x11
5 x12+x13+x14
6 x15+x16+x17
7 x18+x19+x20
8 x21+x22+x23
请帮我构建查询。
谢谢, 纳温
答案 0 :(得分:0)
您可以在声明中使用SELECT
type,
GROUP_CONCAT(DISTINCT uid ORDER BY id DESC) AS users,
COUNT(type) AS typeCount
FROM
(SELECT * FROM `test2` ORDER BY id DESC) test2
GROUP BY
type
和JOIN
GROUP BY
。 This Link可能很有用