最小分组,但我可以这样做吗?

时间:2013-04-19 21:35:12

标签: mysql

我已经创建了一个查询(感谢stackoverflow上的所有提示)。 表结构和数据:

CREATE TABLE IF NOT EXISTS `transactions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `actid` int(11) NOT NULL,
  `memberid` int(11) NOT NULL,
  `description` varchar(30) NOT NULL,
  `amount` decimal(10,2) NOT NULL,
  `date` datetime NOT NULL,
  `type` varchar(3) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=38 ;

数据:

INSERT INTO `transactions` (`id`, `actid`, `memberid`, `description`,
 `amount`, `date`, `type`) VALUES
(1, 601, 1, '', '4.00', '2008-10-31 00:00:00', 'REG'),
(2, 603, 1, '', '5.00', '2010-10-26 00:00:00', 'REG'),
(3, 604, 1, '', '8.00', '2011-10-21 00:00:00', 'REG'),
(4, 1040, 1, '3/5', '2.00', '2009-02-18 00:00:00', 'REG'),
(5, 1042, 1, '8/19', '2.00', '2010-08-19 00:00:00', 'REG'),
(6, 1243, 1, '', '3.00', '2008-01-23 00:00:00', 'REG'),
(7, 1301, 1, '', '1.00', '2000-10-11 00:00:00', 'REG'),
(8, 1306, 1, '', '1.00', '1996-05-17 00:00:00', 'REG'),
(9, 1379, 1, '', '2.00', '2007-11-01 00:00:00', 'REG'),
(10, 1380, 1, '', '2.00', '2008-12-09 00:00:00', 'REG'),
(11, 1381, 1, '', '2.00', '2009-10-20 00:00:00', 'REG'),
(12, 1382, 1, '', '2.00', '2010-10-21 00:00:00', 'REG'),
(13, 1383, 1, '', '2.00', '2011-10-20 00:00:00', 'REG'),
(14, 1384, 1, 'tkt #56', '2.00', '2012-10-12 00:00:00', 'REG'),
(15, 1396, 1, '', '2.00', '2006-10-12 00:00:00', 'REG'),
(16, 1491, 1, '', '20.00', '2007-01-03 00:00:00', 'REG'),
(17, 1494, 1, '', '40.00', '2006-01-09 00:00:00', 'REG'),
(18, 1498, 1, 'GUEST', '32.00', '2004-01-29 00:00:00', 'REG'),
(19, 601, 1, 'cash', '4.00', '2007-11-01 00:00:00', 'CHK'),
(20, 603, 1, 'cash', '5.00', '2009-10-26 00:00:00', 'CHK'),
(21, 604, 1, 'cash', '8.00', '2010-10-21 00:00:00', 'CHK'),
(22, 1040, 1, '2/19', '2.00', '2009-02-18 00:00:00', 'CHK'),
(23, 1040, 1, 'cash 3/5', '2.00', '2009-03-05 00:00:00', 'CHK'),
(24, 1042, 1, 'cash', '2.00', '2010-08-19 00:00:00', 'CHK'),
(25, 1243, 1, 'cash', '3.00', '2008-01-23 00:00:00', 'CHK'),
(26, 1301, 1, 'cash', '1.00', '2000-10-11 00:00:00', 'CHK'),
(27, 1306, 1, 'CASH', '1.00', '1996-05-17 00:00:00', 'CHK'),
(28, 1379, 1, 'cash', '2.00', '2007-11-01 00:00:00', 'CHK'),
(29, 1380, 1, 'cash', '2.00', '2008-12-09 00:00:00', 'CHK'),
(30, 1381, 1, 'cash', '2.00', '2009-10-20 00:00:00', 'CHK'),
(31, 1382, 1, 'cash', '2.00', '2010-10-21 00:00:00', 'CHK'),
(32, 1383, 1, 'cash', '2.00', '2011-10-20 00:00:00', 'CHK'),
(33, 1384, 1, 'cash tkt# 56', '2.00', '2012-10-12 00:00:00', 'CHK'),
(34, 1396, 1, '325', '2.00', '2006-10-12 00:00:00', 'CHK'),
(35, 1491, 1, '', '20.00', '2007-01-03 00:00:00', 'CHK'),
(36, 1494, 1, '252', '40.00', '2006-01-09 00:00:00', 'CHK'),
(37, 1498, 1, '9223-143', '32.00', '2004-01-29 00:00:00', 'CHK');

查询:

SELECT actid, SUM( IF( TYPE =  'REG', amount, 0 ) ) AS  `charge`, 
SUM( IF( TYPE =  'CHK', amount, 0 ) ) AS  `payment`, 
min(date) as firstdate, max(date) as lastdate
FROM transactions
WHERE memberid =1
GROUP BY actid
ORDER BY firstdate 

结果:

actid   charge  payment firstdate           lastdate
1306    1.00    1.00    1996-05-17 00:00:00 1996-05-17 00:00:00
1301    1.00    1.00    2000-10-11 00:00:00 2000-10-11 00:00:00
1498    32.00   32.00   2004-01-29 00:00:00 2004-01-29 00:00:00
1494    40.00   40.00   2006-01-09 00:00:00 2006-01-09 00:00:00
1396    2.00    2.00    2006-10-12 00:00:00 2006-10-12 00:00:00
1491    20.00   20.00   2007-01-03 00:00:00 2007-01-03 00:00:00
601     4.00    4.00    2007-11-01 00:00:00 2008-10-31 00:00:00
1379    2.00    2.00    2007-11-01 00:00:00 2007-11-01 00:00:00
1243    3.00    3.00    2008-01-23 00:00:00 2008-01-23 00:00:00
1380    2.00    2.00    2008-12-09 00:00:00 2008-12-09 00:00:00
1040    2.00    4.00    2009-02-18 00:00:00 2009-03-05 00:00:00
1381    2.00    2.00    2009-10-20 00:00:00 2009-10-20 00:00:00
603     5.00    5.00    2009-10-26 00:00:00 2010-10-26 00:00:00
1042    2.00    2.00    2010-08-19 00:00:00 2010-08-19 00:00:00
1382    2.00    2.00    2010-10-21 00:00:00 2010-10-21 00:00:00
604     8.00    8.00    2010-10-21 00:00:00 2011-10-21 00:00:00
1383    2.00    2.00    2011-10-20 00:00:00 2011-10-20 00:00:00
1384    2.00    2.00    2012-10-12 00:00:00 2012-10-12 00:00:00

这非常好用但是我想在查询中添加REG类型事务的描述,我似乎无法使其工作。

3 个答案:

答案 0 :(得分:0)

我认为您需要将description添加到select,然后将description添加到group by子句

SELECT actid, description, SUM( IF( TYPE = 'REG', amount, 0 ) ) AS charge, SUM( IF( TYPE = 'CHK', amount, 0 ) ) AS payment, min(date) as firstdate, max(date) as lastdate 
FROM transactions 
WHERE memberid =1 
GROUP BY actid, description 
ORDER BY firstdate 

答案 1 :(得分:0)

   SELECT actid, 
          SUM( IF( TYPE = 'REG', amount, 0 ) ) AS charge, 
          SUM( IF( TYPE = 'CHK', amount, 0 ) ) AS payment, 
          min(date) as firstdate, 
          max(date) as lastdate,
          description 
   FROM transactions 
   WHERE memberid =1 
   GROUP BY actid,description ORDER BY firstdate

答案 2 :(得分:0)

我不知道为什么你不能在查询中添加描述,但它可以毫无困难地完成。

 SELECT actid ,  SUM( IF( TYPE =  'REG', amount, 0 ) ) AS  `charge`, 
 SUM( IF( TYPE =  'CHK', amount, 0 ) ) AS  `payment`, 
 min(date) as firstdate, max(date) as lastdate , `description`
 FROM transactions
 WHERE memberid =1
 GROUP BY actid
 ORDER BY firstdate 

DEMO