单个sql查询执行某些组

时间:2012-08-20 13:57:07

标签: mysql sql group-by

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

以下是一些数据:

 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')

我希望获得所有项目的列表: - 以其最新(按时间顺序)状态和优先级, - 第一个和最晚一个条目之间的天数(如果有,则为0)   只有一个条目),你可以忽略小时, - 按优先级排序('高'优先),然后按名称, - 没有最新状态为“关闭”的项目。 (从中省略   结果)。

输出应为:

+---------------+-----------+---------------+----------------+
¦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             ¦
+---------------+-----------+---------------+----------------+

到目前为止我要写这个:

 SELECT name,status FROM projects  group by name order by priority desc,name

请帮帮忙?

2 个答案:

答案 0 :(得分:4)

SELECT *
FROM (
  SELECT name, 
    DATEDIFF(MAX(date), MIN(date)) total_days,
    (SELECT tt.status FROM projects tt 
     WHERE t.name = tt.name AND tt.date = MAX(t.DATE)) latest_status,
    (SELECT tt.priority FROM projects tt 
     WHERE t.name = tt.name AND tt.date = MAX(t.DATE)) latest_priority
  FROM projects t
  GROUP BY name 
  ) t
WHERE latest_status != 'closed'
ORDER BY (CASE latest_priority
          WHEN 'high' THEN 0
          WHEN 'medium' THEN 1
          WHEN 'low' THEN 2 
          END), name;
  • 总天数:获取MAXMIN日期的DATEDIFF,这将为您提供介于两者之间的天数;
  • 最新状态:获取行日期等于MAX日期的状态;
  • 最新优先级:获取行的日期等于MAX日期的优先级;
  • 排序:将每个优先级字符串翻译成数值并按顺序排序。

这是一个sqlfiddle

答案 1 :(得分:4)

试试这个:http://www.sqlfiddle.com/#!2/b3962/1

select p.name, r.total_days, p.status as latest_status, p.priority as latest_priority
from projects p
join 
(
  select name, max(date) as recent_date, datediff(max(date),min(date)) as total_days
  from projects
  group by name
) 
-- recent
r on(r.name,r.recent_date) = (p.name,date)
where p.status not in ('closed')
order by 
         (case latest_priority
          when 'high' then 0
          when 'medium' then 1
          when 'low' THEN 2 
          end), p.name

输出:

|           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 |

如果您想让它更短(使用USING),请将recent_date的别名改为datehttp://www.sqlfiddle.com/#!2/b3962/2

select 

  p.name, r.total_days, p.status as latest_status, p.priority as latest_priority

from projects p
join 
(

  select name, max(date) as date, datediff(max(date),min(date)) as total_days
  from projects
  group by name

) r using(name,date)

where p.status not in ('closed')
order by 
         (case latest_priority
          when 'high' then 0
          when 'medium' then 1
          when 'low' then 2 
          end), p.name

工作原理

从内到外工作。第一步,找到最新的:

  select name, max(date) as recent_date, datediff(max(date),min(date)) as total_days
  from projects
  group by name

输出:

|           NAME |                         DATE | TOTAL_DAYS |
--------------------------------------------------------------
|     ACME, Inc. |   May, 21 2011 11:43:11-0700 |         62 |
|       Foo Bars |   May, 31 2011 15:43:39-0700 |         89 |
|       iCompany |   May, 31 2011 07:13:32-0700 |         69 |
| John Doe & Co. |   May, 28 2011 11:28:32-0700 |         81 |
|      Mega Corp | April, 21 2011 08:08:53-0700 |          0 |

最后一步,将上述结果加入主表:

select 

  p.name, r.total_days, p.status as latest_status, p.priority as latest_priority

from projects p
join 
(

  select name, max(date) as date, datediff(max(date),min(date)) as total_days
  from projects
  group by name

) r using(name,date)

where p.status not in ('closed')
order by 
         (case latest_priority
          when 'high' then 0
          when 'medium' then 1
          when 'low' then 2 
          end), p.name

要覆盖排序,请在ORDER BY子句上使用CASE语句。

输出:

|           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 |