2016-09-16 63 views
0

在我的表我有這種情況返回行的連接點與PostgreSQL的

| id |  book      | reg | cif      | nm | end  | nr |aa | bb | type  | 
|12134357|"Lv.2-Registro Geral (matrícula)"|"10001"|"1002414002024400386-90705793"|  |"Ataulfo Alves"|1877|"G"|"102"|    | 
|12134356|"Lv.2-Registro Geral (matrícula)"|"10001"|""       |"RUA"|"Ataulfo Alves"|1877|"G"|"102"|"Apartamento"| 
|12134358|"Lv.2-Registro Geral (matrícula)"|"10001"|""       |"" |"Ataulfo Alves"| |"" |"" |"Apartamento"| 
  

它單曲可能與書和reg一個選擇(組我跟所有信息只返回一個行嗎?如果我用DISTINCT她將組的每個同等價值

結果的例子:

|"Lv.2-Registro Geral (matrícula)"|"10001"|"1002414002024400386-90705793"|"RUA|"Ataulfo Alves"|1877|"G"|"102"|"Apartamento"| 

TKS

回答

0

我不知道究竟是你試圖完成的任務,但以下(真難看)查詢將找出每個領域的第一個非空值,並返回所有這些一次:

SELECT 
    (SELECT "id" FROM mytable WHERE coalesce(length("id" || '') > 0, false) ORDER BY id LIMIT 1), 
    (SELECT "book" FROM mytable WHERE coalesce(length("book" || '') > 0, false) ORDER BY id LIMIT 1), 
    (SELECT "reg" FROM mytable WHERE coalesce(length("reg" || '') > 0, false) ORDER BY id LIMIT 1), 
    (SELECT "cif" FROM mytable WHERE coalesce(length("cif" || '') > 0, false) ORDER BY id LIMIT 1), 
    (SELECT "nm" FROM mytable WHERE coalesce(length("nm" || '') > 0, false) ORDER BY id LIMIT 1), 
    (SELECT "end" FROM mytable WHERE coalesce(length("end" || '') > 0, false) ORDER BY id LIMIT 1), 
    (SELECT "nr" FROM mytable WHERE coalesce(length("nr" || '') > 0, false) ORDER BY id LIMIT 1), 
    (SELECT "aa" FROM mytable WHERE coalesce(length("aa" || '') > 0, false) ORDER BY id LIMIT 1), 
    (SELECT "bb" FROM mytable WHERE coalesce(length("bb" || '') > 0, false) ORDER BY id LIMIT 1), 
    (SELECT "type" FROM mytable WHERE coalesce(length("type" || '') > 0, false) ORDER BY id LIMIT 1); 
1

DEMO

SELECT book, 
     MAX("reg"), 
     MAX("cif"), 
     MAX("nm"), 
     MAX("end"), 
     MAX("nr"), 
     MAX("aa"), 
     MAX("bb"), 
     MAX("type") 
FROM table1 
GROUP BY book; 

輸出

|        book |      max |       max | max |    max | max | max | max |   max | 
|-----------------------------------|----------------------------|--------------------------------|-------|-----------------|------|-----|-------|---------------| 
| "Lv.2-Registro Geral (matrícula)" | January, 01 10001 00:00:00 | "1002414002024400386-90705793" | "RUA" | "Ataulfo Alves" | 1877 | "G" | "102" | "Apartamento" |