2015-10-15 77 views
0

我的要求:SQL返回一組的第一排

SELECT this, annonceur, campagne, date FROM campagne ORDER BY id ASC 

回報:

this annonceur campagne date 
A_d20151006174516_rBLA_175030 Bla Bla(001) 2015-10-07 17:51:35.290706 
A_d20151006174516_rBLA_175030 Bla Bla(002) 2015-10-07 17:51:35.488242 
A_d20151006174516_rBLA_175030 Bla Bla(003) 2015-10-07 17:51:35.530307 
A_d20151006174516_rBLA_175030 Bla Bla(004) 2015-10-07 17:51:35.559641 

A_d20151008124248_rBLA_124409 Bla Bla(001) 2015-10-09 12:44:13.612378 
A_d20151008124248_rBLA_124409 Bla Bla(002) 2015-10-09 12:44:13.708588 
A_d20151008124248_rBLA_124409 Bla Bla(003) 2015-10-09 12:44:13.717071 
A_d20151008124248_rBLA_124409 Bla Bla(004) 2015-10-09 12:44:13.724947 
A_d20151008124248_rBLA_124409 Bla Bla(005) 2015-10-09 12:44:13.731896 
A_d20151008124248_rBLA_124409 Bla Bla(006) 2015-10-09 12:44:13.738579 

A_d20151011190614_rTest_190714 Test Test 12102015 - v2(001) 2015-10-12 19:07:30.682369 
A_d20151011190614_rTest_190714 Test Test 12102015 - v2(002) 2015-10-12 19:07:30.744372 
A_d20151011190614_rTest_190714 Test Test 12102015 - v2(003) 2015-10-12 19:07:30.787934 

A_d20151013175114_rBLA-VIA-HELLO_175240 Bla via Hello Bla 14102015(001) 2015-10-14 17:56:23.97971 
A_d20151013175114_rBLA-VIA-HELLO_175240 Bla via Hello Bla 14102015(002) 2015-10-14 17:56:24.183737 
A_d20151013175114_rBLA-VIA-HELLO_175240 Bla via Hello Bla 14102015(003) 2015-10-14 17:56:24.269622 
A_d20151013175114_rBLA-VIA-HELLO_175240 Bla via Hello Bla 14102015(004) 2015-10-14 17:56:24.32696 

我怎麼只能返回各個 「塊」 的第一行,如:

this annonceur campagne date 
A_d20151006174516_rBLA_175030 Bla Bla(001) 2015-10-07 17:51:35.290706 
A_d20151008124248_rBLA_124409 Bla Bla(001) 2015-10-09 12:44:13.612378 
A_d20151011190614_rTest_190714 Test Test 12102015 - v2(001) 2015-10-12 19:07:30.682369 
A_d20151013175114_rBLA-VIA-HELLO_175240 Bla via Hello Bla 14102015(001) 2015-10-14 17:56:23.97971 

在單個請求中?

感謝您的幫助!

+0

是什麼讓一個組? 「這個」列? –

+0

哪一列決定「塊」? –

回答

1

在Postgres中,最有效的方法是distinct on。我認爲你可以做你想做的事:

SELECT DISTINCT ON (this) this, annonceur, campagne, date 
FROM campagne 
ORDER BY this, id ASC; 
+0

謝謝!真棒 – Macbernie

1
SELECT this, annonceur, campagne, date 
FROM (select *, row_number() over(partition by this order by date) as rn 
     from campagne) t 
where rn = 1 

您可以使用row_number函數。

+0

謝謝!真棒 – Macbernie