2015-09-06 105 views
0

首先,請原諒語言,我一直無法將我的問題解析爲真實的英語,所以如果有人可以編輯以使其更清楚,那將有所幫助。獲得每組最後[n,n + t]天

我一直在努力爭取一段時間。我需要每個組從過去N天開始的查詢,跳過最近的一個並檢索下一個T天。這是經典的'LIMIT with GROUP'問題的一個版本,事實上,我嘗試過的其中一個查詢無效,使用了這種形式。

MRE如下:

CREATE TABLE `trying` (id INTEGER PRIMARY KEY AUTO_INCREMENT, types1 TEXT, stuffs INTEGER, dates DATE); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("one",123,'2015-09-06'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("one",67,'2015-09-05'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("one",45,'2015-09-04'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("one",98,'2015-09-03'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("one",89,'2015-09-02'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("two",56,'2015-09-02'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("two",34,'2015-09-01'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("two",98,'2015-08-31'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("two",34,'2015-08-30'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("two",12,'2015-08-29'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("three",3,'2015-09-06'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("three",8,'2015-09-04'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("three",80,'2015-09-02'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("three",9,'2015-09-01'); 
INSERT INTO `trying`(types1, stuffs, dates) VALUES("three",6,'2015-08-31'); 

在表trying有三種types1 '類型': '一個', '2' 和 '3',和有每組5周的觀察。請注意,各組之間的日期不相似,甚至可能存在差距(so no dates BETWEEN, like in this question)。

在這個例子中,我想要得到一個表,每個組有三個中間值。所以跳過第一個和最後一個值,預期的輸出應該是這樣的:

types1 stuffs dates 
one  67  2015-09-05 
one  45  2015-09-04 
one  98  2015-09-03 
two  34  2015-09-01 
two  98  2015-08-31 
two  34  2015-08-30 
three 8  2015-09-04 
three 80  2015-09-02 
three 9  2015-09-01 

一對夫婦的查詢,沒有工作:

可能工作
SELECT types1, stuffs, dates FROM trying GROUP BY types1 LIMIT 2,4; 
/*this returned the following */ 
types1 stuffs dates 
two  56  2015-09-02 


SELECT trying.* FROM (SELECT types1, stuffs, dates FROM trying) GROUP BY trying.types1 OFFSET 2,4; 
/*threw out an error: Every derived table must have its own alias */ 
+0

我討論米的解決方案y groupwise max博客:(mysql.rjweb.org/doc.php/groupwise_max)。 –

回答

1
select types1,stuffs,dates from (
select @rank:=if(@prev_cat=types1,@rank+1,1) as rank, 
    types1,stuffs,dates,@prev_cat:=types1 

from trying,(select @rank:=0, @prev_cat:="")t 
order by types1, dates desc 
) temp 

    where rank between 2 and 4 
2

一種方法是使用用戶變量各組在數量的行,然後限制結果在所需的間隔與行號的行:

SELECT id, types1, stuffs, dates 
FROM (
    SELECT 
    id, types1, stuffs, dates, 
    (
     CASE types1 
     WHEN @type 
     THEN @row := @row + 1 
     ELSE @row := 1 AND @type := types1 END 
    ) + 1 AS row 
    FROM trying p, 
    (SELECT @row := 0, @type := '') r 
    ORDER BY types1, dates asc 
) src 
WHERE row BETWEEN 2 AND 4 
ORDER BY id; 

Sample SQL Fiddle查詢1

或者,如果你總是希望刪除的每個組中的第一個和最後一個行,那麼你可以使用一個左連接到一個派生表,返回各組的最大值和最小值日期:

select t.* from trying t 
left join (
    select types1, min(dates) min_dates, max(dates) max_dates 
    from trying group by types1 
    ) minmax 
    on t.types1 = minmax.types1 
    and t.dates in (minmax.max_dates, minmax.min_dates) 
where minmax.types1 is null; 

Sample SQL Fiddle爲查詢2

與樣品數據這兩個查詢返回相同的結果:

| id | types1 | stuffs |      dates | 
|----|--------|--------|-----------------------------| 
| 2 | one |  67 | September, 05 2015 00:00:00 | 
| 3 | one |  45 | September, 04 2015 00:00:00 | 
| 4 | one |  98 | September, 03 2015 00:00:00 | 
| 7 | two |  34 | September, 01 2015 00:00:00 | 
| 8 | two |  98 | August, 31 2015 00:00:00 | 
| 9 | two |  34 | August, 30 2015 00:00:00 | 
| 12 | three |  8 | September, 04 2015 00:00:00 | 
| 13 | three |  80 | September, 02 2015 00:00:00 | 
| 14 | three |  9 | September, 01 2015 00:00:00 |