2012-05-02 18 views
7

ben kovanı böyle bir tablo vardır:SQL'de alan değeri başına satır sayısını nasıl sınırlarım? Örneğin

1 1 
1 4 
1 8 
2 1 
2 5 
3 1 
3 2 

ve sadece ilk kolonun her eşsiz değerin ilk iki sıra iade etmek istiyorum. Bunun, raporlama amacıyla Hive'den MySQL'e aktardığım veri miktarını sınırlayabilmesini istiyorum. Bana bu veren tek HiveQL sorgusu istiyorum:

1 1 
1 4 
2 1 
2 5 
3 1 
3 2 
+1

ne göre sıralanır olacaktır: bir autoincrement alanı var ne dersiniz? – Matthew

+0

Bu tabloların ve columsn'un adları yok mu? –

+1

Bu siteyi ['en büyük-n-grup '+' mysql '] ile aramayı deneyin (http://stackoverflow.com/questions/tagged/greatest-n-per-group+mysql?sort=votes&pagesize=50) etiket kombinasyonu ve durumunuza uyan bir çözüm bulunup bulunmadığını görün. –

cevap

6

yazık MySQL Analitik Fonksiyonlar yoktur. Yani değişkenlerle oynamak zorundasın.

mysql> create table mytab (
    -> id int not null auto_increment primary key, 
    -> first_column int, 
    -> second_column int 
    ->) engine = myisam; 
Query OK, 0 rows affected (0.05 sec) 

mysql> insert into mytab (first_column,second_column) 
    -> values 
    -> (1,1),(1,4),(2,10),(3,4),(1,4),(2,5),(1,6); 
Query OK, 7 rows affected (0.00 sec) 
Records: 7 Duplicates: 0 Warnings: 0 

mysql> select * from mytab order by id; 
+----+--------------+---------------+ 
| id | first_column | second_column | 
+----+--------------+---------------+ 
| 1 |   1 |    1 | 
| 2 |   1 |    4 | 
| 3 |   2 |   10 | 
| 4 |   3 |    4 | 
| 5 |   1 |    4 | 
| 6 |   2 |    5 | 
| 7 |   1 |    6 | 
+----+--------------+---------------+ 
7 rows in set (0.00 sec) 

mysql> select 
    -> id, 
    -> first_column, 
    -> second_column, 
    -> row_num 
    -> from (
    -> select *, 
    -> @num := if(@first_column = first_column, @num:= @num + 1, 1) as row_num, 
    -> @first_column:=first_column as c 
    -> from mytab order by first_column,id) as t,(select @first_column:='',@num: 
=0) as r; 
+----+--------------+---------------+---------+ 
| id | first_column | second_column | row_num | 
+----+--------------+---------------+---------+ 
| 1 |   1 |    1 |  1 | 
| 2 |   1 |    4 |  2 | 
| 5 |   1 |    4 |  3 | 
| 7 |   1 |    6 |  4 | 
| 3 |   2 |   10 |  1 | 
| 6 |   2 |    5 |  2 | 
| 4 |   3 |    4 |  1 | 
+----+--------------+---------------+---------+ 
7 rows in set (0.00 sec) 

mysql> select 
    -> id, 
    -> first_column, 
    -> second_column, 
    -> row_num 
    -> from (
    -> select *, 
    -> @num := if(@first_column = first_column, @num:= @num + 1, 1) as row_num, 
    -> @first_column:=first_column as c 
    -> from mytab order by first_column,id) as t,(select @first_column:='',@num: 
=0) as r 
    -> having row_num<=2; 
+----+--------------+---------------+---------+ 
| id | first_column | second_column | row_num | 
+----+--------------+---------------+---------+ 
| 1 |   1 |    1 |  1 | 
| 2 |   1 |    4 |  2 | 
| 3 |   2 |   10 |  1 | 
| 6 |   2 |    5 |  2 | 
| 4 |   3 |    4 |  1 | 
+----+--------------+---------------+---------+ 
5 rows in set (0.02 sec) 
+0

1) SİPARİŞ BY cümlesi olmadan çalışmaz. 2) 'SİPARİŞ BY' sütununda saydığınız bir sütun olmalı. Aksi takdirde bu işe yaramıyor. – Green

3

bir kovan çözümü

SELECT S.col1, S.col2 
FROM 
(SELECT col1, col2, row_number() over (partition by col1) as r FROM mytable) S 
WHERE S.r < 3 
İlgili konular