2013-05-11 182 views
0

我有一個日期列,我正在使用查詢來從中獲取一組行。mySQL獲取下一個和前一個日期到當前行的日期

我不存儲任何時間值只是日期和T:00:00:00

我需要鍛鍊的是下一個和前幾天我有數據。

我希望能夠做一個單一的查詢和日期,使用下一個和背部UI導航與查詢選擇當前數據集

故有此開始得到最後一組數據

SELECT * 
FROM `newco_bh_price_data` 
WHERE `manager_id` =960 
AND `currency_id` =0 
AND `class_id` = 'A' 
AND `price_date` 
IN (SELECT MAX( `price_date`) 
FROM `newco_bh_price_data` 
) 

我現在需要知道下一個日期是什麼,所以我可以在上面查詢任何幫助

SELECT * 
FROM `newco_bh_price_data` 
WHERE `manager_id` =960 
AND `currency_id` =0 
AND `class_id` = 'A' 
AND `price_date` = '2002-09-19' 

MANT感謝和評論

下面是數據

id price_date manager_id currency_id class_id value v_change 
554 2002-09-19 960 0 A 8.631 0 
553 2002-09-19 960 0 A 9.5635 0 
552 2002-09-18 960 0 A 9.1068 0 
551 2002-09-18 960 0 A 8.9351 0 
550 2002-09-18 960 0 A 9.61 0 
549 2002-09-17 960 0 A 9.1868 0 
548 2002-09-17 960 0 A 9.0201 0 
547 2002-09-17 960 0 A 9.6425 0 
546 2002-09-16 960 0 A 9.3377 0 
545 2002-09-16 960 0 A 9.3317 0 
544 2002-09-16 960 0 A 9.6902 0 
543 2002-09-13 960 0 A 9.418 0 
542 2002-09-13 960 0 A 9.3669 0 
541 2002-09-13 960 0 A 9.7125 0 
540 2002-09-12 960 0 A 9.4463 0 
539 2002-09-12 960 0 A 9.4383 0 
538 2002-09-12 960 0 A 9.7474 0 
537 2002-09-11 960 0 A 9.5303 0 
536 2002-09-11 960 0 A 9.6592 0 
535 2002-09-11 960 0 A 9.8041 0 
534 2002-09-10 960 0 A 9.4666 0 
533 2002-09-10 960 0 A 9.5878 0 
532 2002-09-10 960 0 A 9.7651 0 
531 2002-09-09 960 0 A 9.3104 0 
530 2002-09-09 960 0 A 9.4841 0 
529 2002-09-09 960 0 A 9.7498 0 
528 2002-09-06 960 0 A 9.2462 0 
527 2002-09-06 960 0 A 9.3823 0 
526 2002-09-06 960 0 A 9.7501 0 
525 2002-09-05 960 0 A 9.0465 0 

回答

0

,你真的想是lag()lead()功能的樣本。但是,唉,這些在MySQL中不可用。我認爲實現它們的最簡單方法是使用相關的子查詢。

下面是一個例子:

SELECT *, 
     (select price_date 
     from newco_bh_price_data pd1 
     where pd1.manager_id = pd.manager_id and 
       pd1.currency_id = pd.currency_id and 
       pd1.class_id = pd.class_id and 
       pd1.price_date > pd.price_date 
     order by price_date 
     limit 1 
     ) as next_price_date, 
     (select price_date 
     from newco_bh_price_data pd1 
     where pd1.manager_id = pd.manager_id and 
       pd1.currency_id = pd.currency_id and 
       pd1.class_id = pd.class_id and 
       pd1.price_date < pd.price_date 
     order by price_date desc 
     limit 1 
     ) as prev_price_date 
FROM `newco_bh_price_data` pd 
WHERE `manager_id` =960 
AND `currency_id` =0 
AND `class_id` = 'A' 
AND `price_date` = '2002-09-19'; 

這是假設你想經理,貨幣和類是一樣的尋找下一個和以前的日期時。

0

查詢:

SQLFIDDLEExample

SELECT t1.*, 
     (SELECT MIN(t2.`price_date`) 
     FROM `newco_bh_price_data` t2 
     WHERE t2.`price_date`> t1.`price_date` 
     AND t2.`manager_id`= t1.`manager_id` 
     AND t2.`currency_id` = t1.`currency_id` 
     AND t2.`class_id` = t1.`class_id` 
     ) AS NextDate, 
     (SELECT MAX(t2.`price_date`) 
     FROM `newco_bh_price_data` t2 
     WHERE t2.`price_date`< t1.`price_date` 
     AND t2.`manager_id`= t1.`manager_id` 
     AND t2.`currency_id` = t1.`currency_id` 
     AND t2.`class_id` = t1.`class_id` 
     ) AS PreviousDate 
FROM `newco_bh_price_data` t1 
WHERE t1.`manager_id` =960 
AND t1.`currency_id` =0 
AND t1.`class_id` = 'A' 

結果:

| ID |      PRICE_DATE | MANAGER_ID | CURRENCY_ID | CLASS_ID | VALUE | V_CHANGE |       NEXTDATE |      PREVIOUSDATE | 
------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
| 554 | September, 19 2002 00:00:00+0000 |  960 |   0 |  A |  9 |  0 |       (null) | September, 18 2002 00:00:00+0000 | 
| 553 | September, 19 2002 00:00:00+0000 |  960 |   0 |  A | 10 |  0 |       (null) | September, 18 2002 00:00:00+0000 | 
| 552 | September, 18 2002 00:00:00+0000 |  960 |   0 |  A |  9 |  0 | September, 19 2002 00:00:00+0000 | September, 17 2002 00:00:00+0000 | 
| 551 | September, 18 2002 00:00:00+0000 |  960 |   0 |  A |  9 |  0 | September, 19 2002 00:00:00+0000 | September, 17 2002 00:00:00+0000 | 
| 550 | September, 18 2002 00:00:00+0000 |  960 |   0 |  A | 10 |  0 | September, 19 2002 00:00:00+0000 | September, 17 2002 00:00:00+0000 | 
| 549 | September, 17 2002 00:00:00+0000 |  960 |   0 |  A |  9 |  0 | September, 18 2002 00:00:00+0000 | September, 16 2002 00:00:00+0000 | 
| 548 | September, 17 2002 00:00:00+0000 |  960 |   0 |  A |  9 |  0 | September, 18 2002 00:00:00+0000 | September, 16 2002 00:00:00+0000 | 
相關問題