Sql 使用SELECT语句中的相应值创建新类别列

Sql 使用SELECT语句中的相应值创建新类别列,sql,postgresql,pivot,Sql,Postgresql,Pivot,我试图创建一个SELECT语句,其中“category”和“category2”列下的值将成为实际列,并在它们下面显示相应的“url”和“url2” 基本上我有下表: names | category | url | category2 | url2 ------------------+----------------+------------------------

我试图创建一个SELECT语句,其中“category”和“category2”列下的值将成为实际列,并在它们下面显示相应的“url”和“url2”

基本上我有下表:

      names       |    category    |            url            |   category2  |         url2             
------------------+----------------+---------------------------+--------------+------------------------------
  Jennifer        | Homepage       | http://www.example1.com   |              | 
  Jacob           | Github         | http://www.github.com     |              |  
  Tom             | Homepage       | http://www.example2.com   |    Github    | http://www.github2.com
  Nal             | Facebook Page  | http://www.facebook.com   |              | 
  Matt            | Homepage       | http://www.example3.com   |              |
  Daniel          |                |                           |    Homepage  | http://www.example4.com
  Sarah           |                |                           |    Other     | http://www.other_example.com
…我希望最终结果如下所示:

         names     |        Homepage          |         Github         |     Facebook Page       |        Other           
-------------------+--------------------------+------------------------+-------------------------+---------------------
    Jennifer       | http://www.example1.com  |                        |                         |
    Jacob          |                          | http://www.github.com  |                         |
    Tom            | http://www.example2.com  | http://www.github2.com |                         |
    Nal            |                          |                        | http://www.facebook.com |
    Matt           | http://www.example3.com  |                        |                         |
    Daniel         | http://www.example4.com  |                        |                         | 
    Sarah          |                          |                        |                         | http://www.other_example.com
有什么想法吗?我都不知道该找哪种选择? 我正在使用PostgreSQL


对于这三个类别,您可以使用条件聚合:

select
  names,
  max(case category when 'Homepage' then url end) Homepage,
  max(case category when 'Github' then url end) GitHub,
  max(case category when 'Facebook Page' then url end) "Facebook Page"
from tablename
group by names
请参阅。
结果:


编辑

select
  t.names,
  max(case t.category when 'Homepage' then t.url end) Homepage,
  max(case t.category when 'Github' then t.url end) GitHub,
  max(case t.category when 'Facebook Page' then t.url end) "Facebook Page",
  max(case t.category when 'Other' then t.url end) Other
from (
  select names, category, url from tablename
  union all
  select names, category2, url2 from tablename
) t     
group by t.names
请参阅。
结果:


只有这三个类别吗?不,还有更多,我只是为了简单起见把它们删掉了。一旦我了解了如何为这3个应用程序添加它们,我就会想办法添加它们。这在应用程序中做得更好。SQL不太适合这样做,非常感谢,这正是我所需要的。不幸的是,我没有把整个画面展示出来,因为我认为我能弄清楚。我想在我的结果列表中很好地组织另一组类别和url。我编辑了我的原始帖子以反映这一点,并将这些值添加到我帖子中的Demo V3链接中。非常感谢,非常感谢您的帮助!
select
  t.names,
  max(case t.category when 'Homepage' then t.url end) Homepage,
  max(case t.category when 'Github' then t.url end) GitHub,
  max(case t.category when 'Facebook Page' then t.url end) "Facebook Page",
  max(case t.category when 'Other' then t.url end) Other
from (
  select names, category, url from tablename
  union all
  select names, category2, url2 from tablename
) t     
group by t.names
| names    | homepage                | github                 | Facebook Page           | other                        |
| -------- | ----------------------- | ---------------------- | ----------------------- | ---------------------------- |
| Jennifer | http://www.example1.com |                        |                         |                              |
| Sarah    |                         |                        |                         | http://www.other_example.com |
| Tom      | http://www.example2.com | http://www.github2.com |                         |                              |
| Jacob    |                         | http://www.github.com  |                         |                              |
| Nal      |                         |                        | http://www.facebook.com |                              |
| Daniel   | http://www.example4.com |                        |                         |                              |
| Matt     | http://www.example3.com |                        |                         |                              |