2013-07-03 40 views
0

我试图从“排队”表以获得每个元素我怎样才能拥有所有值的最大日期?

从表中的日期值(只显示前30名)

element      added     processed 
order-confirmation   2013-07-03 14:12:02 2013-07-03 14:12:03 
virtual-product-splitter 2013-07-03 14:12:02 2013-07-03 14:12:07 
fraud-protect   2013-07-03 14:12:02 2013-07-03 14:12:11 
giftcard-creator   2013-07-03 14:12:02 2013-07-03 14:12:15 
code-dispatcher   2013-07-03 14:12:02 0001-01-01 00:00:00 
order-confirmation   2013-07-03 14:10:01 2013-07-03 14:10:02 
virtual-product-splitter 2013-07-03 14:10:01 2013-07-03 14:10:06 
fraud-protect   2013-07-03 14:10:01 2013-07-03 14:10:10 
giftcard-creator   2013-07-03 14:10:01 2013-07-03 14:10:14 
code-dispatcher   2013-07-03 14:10:01 2013-07-03 14:10:19 
order-confirmation   2013-07-03 14:08:01 2013-07-03 14:08:02 
virtual-product-splitter 2013-07-03 14:08:01 2013-07-03 14:08:05 
fraud-protect   2013-07-03 14:08:01 2013-07-03 14:08:09 
giftcard-creator   2013-07-03 14:08:01 2013-07-03 14:08:13 
code-dispatcher   2013-07-03 14:08:01 2013-07-03 14:08:18 
code-dispatcher   2013-07-03 14:06:02 2013-07-03 14:06:19 
order-confirmation   2013-07-03 14:06:01 2013-07-03 14:06:02 
virtual-product-splitter 2013-07-03 14:06:01 2013-07-03 14:06:06 
fraud-protect   2013-07-03 14:06:01 2013-07-03 14:06:10 
giftcard-creator   2013-07-03 14:06:01 2013-07-03 14:06:14 
order-confirmation   2013-07-03 14:04:02 2013-07-03 14:04:03 
virtual-product-splitter 2013-07-03 14:04:02 2013-07-03 14:04:07 
fraud-protect   2013-07-03 14:04:02 2013-07-03 14:04:11 
giftcard-creator   2013-07-03 14:04:02 2013-07-03 14:04:15 
code-dispatcher   2013-07-03 14:04:02 2013-07-03 14:04:19 
order-confirmation   2013-07-03 14:02:01 2013-07-03 14:02:03 
virtual-product-splitter 2013-07-03 14:02:01 2013-07-03 14:02:06 
fraud-protect   2013-07-03 14:02:01 2013-07-03 14:02:10 
giftcard-creator   2013-07-03 14:02:01 2013-07-03 14:02:14 
code-dispatcher   2013-07-03 14:02:01 2013-07-03 14:02:19 

select distinct element from sr_queue我得到

'c5-code-integration' 
'c5-debitor-integration' 
'c5-order-integration' 
'c5-product-integration' 
'code-dispatcher' 
'fraud-protect' 
'giftcard-creator' 
'order-confirmation' 
'packaged-confirmation' 
'virtual-product-splitter' 

我试图附加最大日期(因为有几个)到每个元素,但做

select element, max(added), processed 
from sr_queue 
where element in (
    'c5-code-integration', 
    'c5-debitor-integration', 
    'c5-order-integration', 
    'c5-product-integration', 
    'code-dispatcher', 
    'fraud-protect', 
    'giftcard-creator', 
    'order-confirmation', 
    'packaged-confirmation', 
    'virtual-product-splitter') 

我该如何创建选择?

这样我就可以得到:

order-confirmation   2013-07-03 14:12:02 2013-07-03 14:12:03 
virtual-product-splitter 2013-07-03 14:12:02 2013-07-03 14:12:07 
fraud-protect            2013-07-03 14:12:02 2013-07-03 14:12:11 
giftcard-creator            2013-07-03 14:12:02 2013-07-03 14:12:15 
code-dispatcher          2013-07-03 14:12:02 0001-01-01 00:00:00 
c5-code-integration   2013-07-03 14:12:02 2013-07-03 14:12:15 
... 

我完全空白后第3天代码:(

+1

的MySQL,MS SQL Server的? – Justin

回答

3
select element, max(added), max(processed) 
from sr_queue 
group by element 

编辑工作:

select element, added, processed 
from sr_queue q 
where added = (SELECT MAX(added) FROM sr_queue s WHERE s.element = q.element) 

或者另一种可能性:

SELECT element, added, processed 
FROM sr_queue s 
INNER JOIN (
    SELECT element, MAX(added) 
    FROM sr_queue 
    GROUP BY element 
) q ON s.element = q.element 
+0

我想获得'max(added)'同一行的'processed'值,这可能吗? – balexandre

+0

当然,请参阅我编辑的答案。 – fancyPants

+0

这在流程方面非常昂贵......需要找到更好的方法......在425000行中,需要超过10分钟:/ – balexandre

3

使用GROUP BY代替DISTINCT

SELECT 
    element, 
    MAX(added) 
FROM 
    sr_queue 
GROUP BY 
    element 
+0

看,真的很累......“GROUP BY”arrghhhhhh!什么,等等......这样我就永远不会得到'MAX(已添加)'的'已处理'行,我该怎么做? – balexandre

+0

你使用什么数据库引擎? SQL Server,MySQL,另外一个? – MarcinJuraszek

+0

目前的MySQL,但任何关系数据库应该工作... – balexandre

0

假设一个最新版本的MS SQL Server的

select element, added, processed from 
(select *, row_number() over (partition by element order by added desc) as ranker 
from sr_queue q) Z 
where ranker = 1 
相关问题