0

我这里有3张桌子,即:

person (
  id int PRIMARY KEY
 ,fullname text)

phonenumber (
  id int PRIMARY KEY
 ,personid int REFERENCES person(id)
 ,phonetypeid REFERENCES phonetype(id)
 ,number text)

phonetype (
 id int PRIMARY KEY
,phonetype text) -- phonetype  'Home', 'Cell', 'Fax', 'Main' etc.

每个都可以在表person中存储多个号码,并且在phonenumber表中具有不同的电话类型phonetype

| fullname         | number        | phonetype |
| Erwin Macale     | (671)632-3909 | Home      |
| Erwin Macale     | (671)632-3909 | Cell      |
| Erwin Macale     | (671)632-3909 | Main      |

我只想显示具有以下条件的每个人的电话号码:

  • 如果他有一个“家庭”电话类型号码,则只显示家庭类型删除他所有其他电话类型号码。
  • 如果他没有“家庭”号码,则只显示他的“手机”号码,然后删除他所有的其他(“传真”、“主要”等)号码。
  • 一个人可以没有电话号码。

我创建了一个临时表,其中包含fullname, number, phonetype命名testable然后从这里我分隔所有电话类型号码,例如:

CREATE TEMP TABLE home AS SELECT * FROM testable WHERE phonetype ILIKE 'home';
CREATE TEMP TABLE cell AS SELECT * FROM testable WHERE phonetype ilike 'cell';
CREATE TEMP TABLE main AS SELECT * FROM testable WHERE phonetype ilike 'main';
CREATE TEMP TABLE fax AS SELECT * FROM testable WHERE phonetype ilike 'fax';
CREATE TEMP TABLE work AS SELECT * FROM testable WHERE phonetype ilike 'work';
CREATE TEMP TABLE neighbor AS SELECT * FROM testable WHERE phonetype ilike 'neighbor';
CREATE TEMP TABLE other AS SELECT * FROM testable WHERE phonetype ilike 'other';
CREATE TEMP TABLE unknown AS SELECT * FROM testable WHERE phonetype ilike 'unknown';

然后创建另一组我认为满足上述条件的临时表:

CREATE TEMP TABLE all_cell AS SELECT * FROM cell EXCEPT SELECT * FROM home;
CREATE TEMP TABLE all_main AS (SELECT * FROM main) EXCEPT (SELECT * FROM home UNION SELECT * FROM cell);
CREATE TEMP TABLE all_fax AS (SELECT * FROM fax) EXCEPT (SELECT * FROM home UNION SELECT * FROM cell UNION SELECT * FROM main);
CREATE TEMP TABLE all_work AS (SELECT * FROM work) EXCEPT (SELECT * FROM home UNION SELECT * FROM cell UNION SELECT * FROM main UNION SELECT * FROM fax);
CREATE TEMP TABLE all_neighbor AS (SELECT * FROM neighbor) EXCEPT (SELECT * FROM home UNION SELECT * FROM cell UNION SELECT * FROM main UNION SELECT * FROM fax UNION SELECT * FROM work);
CREATE TEMP TABLE all_other AS (SELECT * FROM other) EXCEPT (SELECT * FROM home UNION SELECT * FROM cell UNION SELECT * FROM main UNION SELECT * FROM fax UNION SELECT * FROM work UNION SELECT * FROM neighbor);
CREATE TEMP TABLE all_unknown AS (SELECT * FROM unknown) EXCEPT (SELECT * FROM home UNION SELECT * FROM cell UNION SELECT * FROM main UNION SELECT * FROM fax UNION SELECT * FROM work UNION SELECT * FROM neighbor UNION SELECT * FROM other);

最后选择最后一组临时表的所有并集:

SELECT fullname, number, phonetype FROM (
    SELECT * FROM home
    UNION 
    SELECT * FROM all_cell
    UNION 
    SELECT * FROM all_main
    UNION 
    SELECT * FROM all_fax
    UNION 
    SELECT * FROM all_work
    UNION 
    SELECT * FROM all_neighbor
    UNION 
    SELECT * FROM all_other
    UNION 
    SELECT * FROM all_unknown
) AS t1
ORDER BY t1.fullname, t1.phonetype;

满足我的条件的步骤是否正确?我仍然从一个人那里得到不同的电话类型值。

4

1 回答 1

3

You went to a lot of trouble to get what this single query can give you:

SELECT DISTINCT ON (p.fullname, p.id)
       p.fullname, n.number, t.phonetype
FROM   phonenumber n 
JOIN   person      p ON p.id = n.personid
JOIN   phonetype   t ON t.id = n.phonetypeid
LEFT   JOIN (
   VALUES
     (1, 'home')
    ,(2, 'cell')
    ,(3, 'main')
    ,(4, 'fax')
    ,(5, 'work')
    ,(6, 'neighbor')
    ,(7, 'other')
    ,(8, 'unknown')
   ) r(rnk, phonetype) USING (phonetype)
ORDER  BY p.fullname, p.id, r.rnk;

DISTINCT ON (p.fullname, p.id) because fullname is probably not unique. I use it anyway to get the sort order you seem to be looking for in a single query level.

Details about DISTINCT ON in this related answer:
Select first row in each GROUP BY group?

I appended the ranking (r.rnk) information with a VALUES expression to make it work with your presented table layout. Better, though, you add to the table phonetype permanently:

ALTER TABLE phonetype ADD COLUMN rnk int;

UPDATE phonetype t
SET    rnk = r.rnk
FROM  (
       VALUES
         (1, 'home')
        ,(2, 'cell')
        ,(3, 'main')
        ,(4, 'fax')
        ,(5, 'work')
        ,(6, 'neighbor')
        ,(7, 'other')
        ,(8, 'unknown')
       ) r(rnk, phonetype)
WHERE   r.phonetype = t.phonetype;

Then your query becomes even simpler:

SELECT DISTINCT ON (p.fullname, p.id)
       p.fullname, n.number, t.phonetype
FROM   phonenumber n 
JOIN   person      p ON p.id = n.personid
JOIN   phonetype   t ON t.id = n.phonetypeid
ORDER  BY p.fullname, p.id, t.rnk    -- add more columns to break ties (if any)
于 2013-10-23T03:01:41.593 回答