2015-04-01 56 views
0

在這樣的表:
組通過與最大和秩序

code  code1 code2 code3 code4 
FILA841201 123456 481201 654987 NULL 
NULL  123456 481201 1234 NULL 
NULL  123456 481201 789014 324324 

我需要得到只有一條線,今天使用這些查詢

CREATE TABLE TMP2(SELECT MAX(代碼)作爲代碼,code1,max(code2)作爲code2,max(code3)作爲code3,max(code4)作爲code4 FROM tmp1 WHERE code1 IS NOT NULL group by code1); (代碼)作爲代碼,max(code1)作爲代碼1,代碼2,max(代碼3)作爲代碼3,max(代碼4)作爲代碼4來自tmp1 WHERE代碼2 IS NOT NULL由代碼2組)。

ECC ......循環對所有列

我的問題是,如果有一列中不同的值,我不得不讓位給填補了第一列線。

在這個例子中Code3我必須拿出「654987」而不是最大(789014)。

的結果,我應該得到

code  code1 code2 code3 code4 
FILA841201 123456 481201 654987 324324 

感謝您的關注

+0

有沒有這樣的事情作爲「第一行」。你有一列指定行的排序嗎? – 2015-04-01 20:56:05

+0

今天沒有,希望我可以創建它 – user2069604 2015-04-01 21:03:17

回答

0

假設你有一個指定行的排序列,您可以用相關子查詢做到這一點:

select (select code from tmp1 where code is not null order by id limit 1) as code, 
     (select code1 from tmp1 where code1 is not null order by id limit 1) as code1, 
     (select code2 from tmp1 where code2 is not null order by id limit 1) as code2, 
     (select code3 from tmp1 where code3 is not null order by id limit 1) as code3, 
     (select code4 from tmp1 where code4 is not null order by id limit 1) as code4 
+0

在這種情況下,如果我有不同的值,它會創建兩行,我需要我忽略連續值,如果不同.. – user2069604 2015-04-01 21:02:30

+0

@ user2069604。 。 。這個查詢不可能返回多於一行。 – 2015-04-01 22:16:19

+0

該查詢生成了與第一個表幾乎相同的表,但仍然無法使用,因爲沒有分組。我現在使用的系統從95.000到18.500記錄,但我遇到了我之前說過的問題,如果任何一個人的第一列被重視,如果他們不同,必須在他自己的列的其他人身上有prededestza – user2069604 2015-04-02 19:02:55