【问题标题】:Remove column duplicates with union join postgresql使用 union join postgresql 删除列重复项
【发布时间】:2016-04-05 15:04:15
【问题描述】:

我想在我的 postgresql 选择语句中隐藏重复的列。 下面是sql代码:

SELECT name, amount, cpu, id 
FROM computersystem, stock 
WHERE cpu=id 
UNION 
SELECT name, amount, ram, id 
FROM computersystem, stock 
WHERE ram=id 
ORDER BY name, amount

以及当前的输出:

Name:AMD Ultimate Overkill amount:2
Name:AMD Ultimate Overkill amount:10
Name:CPU Heavy, Low Graphics amount:2
Name:CPU Heavy, Low Graphics amount:10
Name:Graphics Heavy, Low CPU amount:8
Name:Graphics Heavy, Low CPU amount:10

我需要得到的只是每个名字中的一个

Name:AMD Ultimate Overkill amount:2
Name:CPU Heavy, Low Graphics amount:2
Name:Graphics Heavy, Low CPU amount:8

有没有快速解决办法?

【问题讨论】:

    标签: sql postgresql duplicates


    【解决方案1】:

    DISTINCT ON 可能会解决您的问题。

     SELECT DISTINCT ON (name)
            name,
            amount,
            cpu,
            id
     FROM computersystem, stock 
     WHERE cpu=id 
     UNION SELECT name, amount, ram, id FROM computersystem, stock 
     WHERE ram=id
     GROUP BY name
     ORDER BY name, amount
    

    找到官方文档here

    【讨论】:

      猜你喜欢
      • 2020-07-21
      • 2017-02-17
      • 2020-08-04
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      相关资源
      最近更新 更多