CREATE TABLE IF NOT EXISTS `projects` ( `idproject` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `date` datetime NOT NULL, `status` enum('new','active','closed') NOT NULL, `priority` enum('low','medium','high') NOT NULL, PRIMARY KEY (`idproject`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=20
Here are some details:
INSERT INTO `projects` (`idproject`, `name`, `date`, `status`, `priority`) VALUES (1, 'iCompany', '2011-03-23 11:41:44', 'new', 'medium'), (2, 'John Doe & Co.', '2011-04-09 14:38:04', 'closed', 'low'), (3, 'ACME, Inc.', '2011-05-21 11:43:11', 'active', 'high'), (4, 'John Doe & Co.', '2011-03-28 15:19:45', 'active', 'low'), (5, 'John Doe & Co.', '2011-03-08 15:16:32', 'new', 'low'), (6, 'ACME, Inc.', '2011-04-05 20:58:42', 'active', 'low'), (7, 'Mega Corp', '2011-04-21 08:08:53', 'new', 'low'), (8, 'iCompany', '2011-04-17 08:40:36', 'active', 'medium'), (9, 'iCompany', '2011-05-18 14:36:48', 'active', 'low'), (10, 'John Doe & Co.', '2011-04-18 19:08:25', 'new', 'medium'), (11, 'ACME, Inc.', '2011-05-19 13:11:04', 'active', 'low'), (12, 'Foo Bars', '2011-03-03 17:19:29', 'new', 'high'), (13, 'ACME, Inc.', '2011-04-23 20:42:33', 'active', 'medium'), (14, 'Foo Bars', '2011-05-13 09:18:15', 'active', 'medium'), (15, 'ACME, Inc.', '2011-03-20 14:37:18', 'new', 'low'), (16, 'Foo Bars', '2011-04-18 13:46:23', 'active', 'high'), (17, 'iCompany', '2011-05-31 07:13:32', 'closed', 'low'), (18, 'Foo Bars', '2011-05-31 15:43:39', 'active', 'low'), (19, 'John Doe & Co.', '2011-05-28 11:28:32', 'active', 'medium')
I would like to have a list of all projects: - with their last (chronological) status and priority, - with the number of days between the first and last record (0, if there is only one record), you can ignore the clock, - sorted by priority (first " high "), then by name, - without projects where the last status is" closed "(excluded from the result).
The conclusion should be:
+---------------+-----------+---------------+----------------+ ¦name ¦total_days ¦latest_status ¦latest_priority ¦ +---------------+-----------+---------------+----------------+ ¦ACME, Inc. ¦62 ¦active ¦high ¦ ¦John Doe & Co. ¦81 ¦active ¦medium ¦ ¦Foo Bars ¦89 ¦active ¦low ¦ ¦Mega Corp ¦0 ¦new ¦low ¦ +---------------+-----------+---------------+----------------+
So far, I had to write this:
SELECT name,status FROM projects group by name order by priority desc,name
please, help?