2013-07-03 82 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 
相關問題